Skip to content

Commit

Permalink
Fix eslint-plugin-khan tests so that they can be run using jest (#1090)
Browse files Browse the repository at this point in the history
## Summary:
#1085 caused an issue where tests were only running for one of the eslint plugin rules.  Thanks @beaesguerra for noticing this.  This PR updates our testing setup so that we can use jest to run the eslint-plugin-khan tests instead of mocha.  It also fixes a few failures that are due to changes in eslint and RuleTester's behaviour.

Issue: None

## Test plan:
- yarn test

Author: kevinb-khan

Reviewers: jeresig, kevinb-khan

Required Reviewers:

Approved By: jeresig

Checks: ✅ Test (macos-latest, 20.x), ✅ codecov/project, ✅ CodeQL, ✅ Lint, typecheck, and coverage check (ubuntu-latest, 20.x), ✅ Analyze (javascript), ✅ Prime node_modules cache for primary configuration (ubuntu-latest, 20.x), ✅ gerald, ⏭️  dependabot

Pull Request URL: #1090
  • Loading branch information
kevinb-khan authored Dec 9, 2024
1 parent 919c72c commit 4cba5ba
Show file tree
Hide file tree
Showing 16 changed files with 197 additions and 257 deletions.
2 changes: 2 additions & 0 deletions .changeset/rotten-spiders-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
4 changes: 0 additions & 4 deletions .github/workflows/node-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,3 @@ jobs:
full: yarn test
limited-trigger: ${{ steps.js-ts-files.outputs.filtered }}
limited: yarn test --findRelatedTests --passWithNoTests {}

- name: Mocha (eslint-plugin-khan)
working-directory: ./packages/eslint-plugin-khan
run: yarn test
9 changes: 8 additions & 1 deletion config/jest/test-setup.js
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
// No setup to perform
// Adapted from https://typescript-eslint.io/packages/rule-tester/#vitest
import {afterAll, it, describe} from "@jest/globals";
import {RuleTester} from "@typescript-eslint/rule-tester";

RuleTester.afterAll = afterAll;
RuleTester.describe = describe;
RuleTester.it = it;
RuleTester.itOnly = it.only;
1 change: 0 additions & 1 deletion config/jest/test.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ module.exports = {
resetMocks: true,
testEnvironment: "jest-environment-node",
testMatch: ["<rootDir>/**/*.test.ts"],
testPathIgnorePatterns: ["/eslint-plugin-khan/"],
setupFilesAfterEnv: [
"jest-extended/all",
"<rootDir>/config/jest/test-setup.js",
Expand Down
4 changes: 1 addition & 3 deletions packages/eslint-plugin-khan/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@
"@swc-node/register": "^1.6.8",
"@swc/core": "^1.3.93",
"eslint": "^8.52.0",
"mocha": "^10.2.0",
"prettier": "^2.8.8",
"pretty-quick": "^3.1.3"
},
"scripts": {
"prettier": "prettier --write '{lib,test}/**/*.{js,ts}'",
"test": "mocha"
"prettier": "prettier --write '{lib,test}/**/*.{js,ts}'"
},
"dependencies": {
"@babel/types": "^7.23.0",
Expand Down
26 changes: 17 additions & 9 deletions packages/eslint-plugin-khan/test/rules/array-type-style.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import {RuleTester} from "@typescript-eslint/rule-tester";

import {rules} from "../../src/index";
import {RuleTester} from "../RuleTester";

const ruleTester = new RuleTester({
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 6,
sourceType: "module",
ecmaFeatures: {},
languageOptions: {
parserOptions: {
ecmaVersion: 6,
sourceType: "module",
ecmaFeatures: {},
},
},
linterOptions: {
// NOTE(kevinb): Avoids 'TypeError: Expected a Boolean' error
// when running the tests.
reportUnusedDisableDirectives: true,
},
});

Expand All @@ -30,9 +37,10 @@ ruleTester.run(ruleName, rule, {
// Two errors are reported because there are two array types,
// they just happen to be nested.
errors: [{messageId: "errorString"}, {messageId: "errorString"}],
// This is a partial fix. Multiple runs of eslint --fix are needed
// to fix nested (in the AST) array types completely.
output: "type foo = { bar: Array<number>[] }",
output: [
"type foo = { bar: Array<number>[] }",
"type foo = { bar: Array<Array<number>> }",
],
},
],
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import {RuleTester} from "@typescript-eslint/rule-tester";

import {rules} from "../../src/index";
import {RuleTester} from "../RuleTester";

const ruleTester = new RuleTester({
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 6,
sourceType: "module",
ecmaFeatures: {},
languageOptions: {
parserOptions: {
ecmaVersion: 6,
sourceType: "module",
ecmaFeatures: {},
},
},
linterOptions: {
// NOTE(kevinb): Avoids 'TypeError: Expected a Boolean' error
// when running the tests.
reportUnusedDisableDirectives: true,
},
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import {RuleTester} from "@typescript-eslint/rule-tester";

import {rules} from "../../src/index";
import {RuleTester} from "../RuleTester";

const ruleTester = new RuleTester({
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 6,
sourceType: "module",
ecmaFeatures: {},
languageOptions: {
parserOptions: {
ecmaVersion: 6,
sourceType: "module",
ecmaFeatures: {},
},
},
linterOptions: {
// NOTE(kevinb): Avoids 'TypeError: Expected a Boolean' error
// when running the tests.
reportUnusedDisableDirectives: true,
},
});

Expand Down Expand Up @@ -85,14 +92,6 @@ ruleTester.run("jest-await-async-matchers", rule, {
],
output: 'await expect(promise).rejects.not.toThrow(new Error("foo"));',
},
{
code: `expect(promise).resolves.not.toBe(true);`,

errors: [
{messageId: "asyncMatchers", data: {matchers: ["resolves"]}},
],
output: "await expect(promise).resolves.not.toBe(true);",
},
{
code: `expect(promise).toResolve();`,

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import {RuleTester} from "eslint";
import {RuleTester} from "@typescript-eslint/rule-tester";

import {rules} from "../../src/index";

const parserOptions = {
parser: require.resolve("@babel/eslint-parser"),
};
const ruleTester = new RuleTester({
languageOptions: {
parserOptions: {
ecmaVersion: 6,
sourceType: "module",
ecmaFeatures: {},
},
},
linterOptions: {
// NOTE(kevinb): Avoids 'TypeError: Expected a Boolean' error
// when running the tests.
reportUnusedDisableDirectives: true,
},
});

const ruleTester = new RuleTester(parserOptions);
const rule = rules["jest-enzyme-matchers"];

ruleTester.run("jest-real-timers", rule, {
Expand Down
26 changes: 17 additions & 9 deletions packages/eslint-plugin-khan/test/rules/no-one-tuple.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import {RuleTester} from "@typescript-eslint/rule-tester";

import {rules} from "../../src/index";
import {RuleTester} from "../RuleTester";

const ruleTester = new RuleTester({
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 6,
sourceType: "module",
ecmaFeatures: {},
languageOptions: {
parserOptions: {
ecmaVersion: 6,
sourceType: "module",
ecmaFeatures: {},
},
},
linterOptions: {
// NOTE(kevinb): Avoids 'TypeError: Expected a Boolean' error
// when running the tests.
reportUnusedDisableDirectives: true,
},
});

Expand All @@ -33,9 +40,10 @@ ruleTester.run(ruleName, rule, {
// Two errors are reported because there are two one-tuples,
// they just happen to be nested.
errors: [{messageId: "errorString"}, {messageId: "errorString"}],
// This is a partial fix. Multiple runs of eslint --fix are needed
// to fix nested 1-tuples completely.
output: "type foo = { bar: Array<[number]> }",
output: [
"type foo = { bar: Array<[number]> }",
"type foo = { bar: Array<Array<number>> }",
],
},
],
});
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import {RuleTester} from "@typescript-eslint/rule-tester";

import {rules} from "../../src/index";
import {RuleTester} from "../RuleTester";

const ruleTester = new RuleTester({
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 6,
sourceType: "module",
ecmaFeatures: {
jsx: true,
languageOptions: {
parserOptions: {
ecmaVersion: 6,
sourceType: "module",
ecmaFeatures: {
jsx: true,
},
},
},
linterOptions: {
// NOTE(kevinb): Avoids 'TypeError: Expected a Boolean' error
// when running the tests.
reportUnusedDisableDirectives: true,
},
});

const ruleName = "react-no-method-jsx-attribute";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import {RuleTester} from "@typescript-eslint/rule-tester";

import {rules} from "../../src/index";
import {RuleTester} from "../RuleTester";

const ruleTester = new RuleTester({
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 6,
sourceType: "module",
ecmaFeatures: {},
languageOptions: {
parserOptions: {
ecmaVersion: 6,
sourceType: "module",
ecmaFeatures: {},
},
},
linterOptions: {
// NOTE(kevinb): Avoids 'TypeError: Expected a Boolean' error
// when running the tests.
reportUnusedDisableDirectives: true,
},
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import {RuleTester} from "@typescript-eslint/rule-tester";

import {rules} from "../../src/index";
import {RuleTester} from "../RuleTester";

const ruleTester = new RuleTester({
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 6,
sourceType: "module",
ecmaFeatures: {
jsx: true,
languageOptions: {
parserOptions: {
ecmaVersion: 6,
sourceType: "module",
ecmaFeatures: {
jsx: true,
},
},
},
linterOptions: {
// NOTE(kevinb): Avoids 'TypeError: Expected a Boolean' error
// when running the tests.
reportUnusedDisableDirectives: true,
},
});

const ruleName = "react-svg-path-precision";
Expand Down
26 changes: 12 additions & 14 deletions packages/eslint-plugin-khan/test/rules/sync-tag.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import {RuleTester} from "@typescript-eslint/rule-tester";

import {rules} from "../../src/index";
import * as util from "../../src/util";

import {RuleTester} from "../RuleTester";

const ruleTester = new RuleTester({
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 6,
sourceType: "module",
ecmaFeatures: {
jsx: true,
languageOptions: {
parserOptions: {
ecmaVersion: 6,
sourceType: "module",
ecmaFeatures: {},
},
},
linterOptions: {
// NOTE(kevinb): Avoids 'TypeError: Expected a Boolean' error
// when running the tests.
reportUnusedDisableDirectives: true,
},
});

beforeEach(() => {
Expand Down Expand Up @@ -188,12 +192,6 @@ ruleTester.run("sync-tag", rule, {
},
},
],
// Prettier is removing whitespace within backticks, so I had to add
// the newlines manually.
output: `
// sync-start:foo-bar 1424803960 filey
const FooBar = 'foobar';
// sync-end:foo-bar`,
options: [
{
ignoreFiles: ["lint_blacklist.txt"],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import {RuleTester} from "@typescript-eslint/rule-tester";

import {rules} from "../../src/index";
import {RuleTester} from "../RuleTester";

const ruleTester = new RuleTester({
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 6,
sourceType: "module",
ecmaFeatures: {
jsx: true,
languageOptions: {
parserOptions: {
ecmaVersion: 6,
sourceType: "module",
ecmaFeatures: {
jsx: true,
},
},
},
linterOptions: {
// NOTE(kevinb): Avoids 'TypeError: Expected a Boolean' error
// when running the tests.
reportUnusedDisableDirectives: true,
},
});

const ruleName = "ts-no-error-suppressions";
Expand Down
Loading

0 comments on commit 4cba5ba

Please sign in to comment.