Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix eslint-plugin-khan tests so that they can be run using jest #1090

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);",
},
Comment on lines -88 to -95
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a duplicated test case which jest doesn't like.

{
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
Loading