Skip to content

Commit

Permalink
refactor: ♻️ group testing rules into one config object
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmy-guzman committed Apr 8, 2024
1 parent 00fa347 commit 1c9bd20
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 131 deletions.
30 changes: 18 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,10 @@ export default jimmyDotCodes({
*/
imports: true,
/**
* Are Jest rules are enabled?
* Are testing rules are enabled?
* @default false
*/
jest: false,
/**
* Are Vitest rules are enabled?
* @default false
*/
vitest: false,
/**
* Are Testing Library rules are enabled?
* @default false
*/
testingLibrary: false,
testing: false,
});
```

Expand All @@ -95,6 +85,22 @@ export default jimmyDotCodes({
});
```

#### Testing

By default [vitest](https://vitest.dev) is used as the testing framework but you can override and add some utilities:

```js
// eslint.config.mjs
import jimmyDotCodes from "@jimmy.codes/eslint-config";

export default jimmyDotCodes({
testing: {
framework: "jest",
utilities: ["testing-library"],
},
});
```

#### Overrides

Or if you want to extend or override the configuration:
Expand Down
5 changes: 3 additions & 2 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ const jimmyDotCodes = jiti("./src").default;

export default jimmyDotCodes({
typescript: true,
vitest: true,
react: true,
testingLibrary: true,
testing: {
utilities: ["testing-library"],
},
overrides: [
{
ignores: ["fixtures"],
Expand Down
24 changes: 12 additions & 12 deletions src/__snapshots__/factory.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8191,7 +8191,7 @@ exports[`jimmyDotCodes > should create configuration w/ jest & react & testing l
"xtest": false,
},
},
"name": "jimmy.codes/jest",
"name": "jimmy.codes/testing",
"plugins": {
"jest": {
"configs": {
Expand Down Expand Up @@ -9802,7 +9802,7 @@ If your function does not access \`this\`, you can annotate it with \`this: void
"**/e2e/**/*.spec.?([cm])[jt]s?(x)",
"**/e2e/**/*.test.?([cm])[jt]s?(x)",
],
"name": "jimmy.codes/jest/disabled",
"name": "jimmy.codes/testing/disabled",
"rules": {
"jest/require-hook": "off",
},
Expand All @@ -9815,7 +9815,7 @@ If your function does not access \`this\`, you can annotate it with \`this: void
"**/*.bench.?([cm])[jt]s?(x)",
"**/*.benchmark.?([cm])[jt]s?(x)",
],
"name": "jimmy.codes/testing-library",
"name": "jimmy.codes/testing/testing-library",
"plugins": {
"jest-dom": {
"configs": {
Expand Down Expand Up @@ -11801,7 +11801,7 @@ If your function does not access \`this\`, you can annotate it with \`this: void
"**/e2e/**/*.spec.?([cm])[jt]s?(x)",
"**/e2e/**/*.test.?([cm])[jt]s?(x)",
],
"name": "jimmy.codes/testing-library/disabled",
"name": "jimmy.codes/testing/testing-library/disabled",
"rules": {
"testing-library/prefer-screen-queries": "off",
},
Expand Down Expand Up @@ -13991,7 +13991,7 @@ exports[`jimmyDotCodes > should create configuration w/ jest 1`] = `
"xtest": false,
},
},
"name": "jimmy.codes/jest",
"name": "jimmy.codes/testing",
"plugins": {
"jest": {
"configs": {
Expand Down Expand Up @@ -15602,7 +15602,7 @@ If your function does not access \`this\`, you can annotate it with \`this: void
"**/e2e/**/*.spec.?([cm])[jt]s?(x)",
"**/e2e/**/*.test.?([cm])[jt]s?(x)",
],
"name": "jimmy.codes/jest/disabled",
"name": "jimmy.codes/testing/disabled",
"rules": {
"jest/require-hook": "off",
},
Expand Down Expand Up @@ -55807,7 +55807,7 @@ exports[`jimmyDotCodes > should create configuration w/ vitest & react & testing
"xtest": false,
},
},
"name": "jimmy.codes/vitest",
"name": "jimmy.codes/testing",
"plugins": {
"jest": {
"configs": {
Expand Down Expand Up @@ -57418,7 +57418,7 @@ If your function does not access \`this\`, you can annotate it with \`this: void
"**/e2e/**/*.spec.?([cm])[jt]s?(x)",
"**/e2e/**/*.test.?([cm])[jt]s?(x)",
],
"name": "jimmy.codes/vitest/disabled",
"name": "jimmy.codes/testing/disabled",
"rules": {
"jest/require-hook": "off",
},
Expand All @@ -57431,7 +57431,7 @@ If your function does not access \`this\`, you can annotate it with \`this: void
"**/*.bench.?([cm])[jt]s?(x)",
"**/*.benchmark.?([cm])[jt]s?(x)",
],
"name": "jimmy.codes/testing-library",
"name": "jimmy.codes/testing/testing-library",
"plugins": {
"jest-dom": {
"configs": {
Expand Down Expand Up @@ -59417,7 +59417,7 @@ If your function does not access \`this\`, you can annotate it with \`this: void
"**/e2e/**/*.spec.?([cm])[jt]s?(x)",
"**/e2e/**/*.test.?([cm])[jt]s?(x)",
],
"name": "jimmy.codes/testing-library/disabled",
"name": "jimmy.codes/testing/testing-library/disabled",
"rules": {
"testing-library/prefer-screen-queries": "off",
},
Expand Down Expand Up @@ -61607,7 +61607,7 @@ exports[`jimmyDotCodes > should create configuration w/ vitest 1`] = `
"xtest": false,
},
},
"name": "jimmy.codes/vitest",
"name": "jimmy.codes/testing",
"plugins": {
"jest": {
"configs": {
Expand Down Expand Up @@ -63218,7 +63218,7 @@ If your function does not access \`this\`, you can annotate it with \`this: void
"**/e2e/**/*.spec.?([cm])[jt]s?(x)",
"**/e2e/**/*.test.?([cm])[jt]s?(x)",
],
"name": "jimmy.codes/vitest/disabled",
"name": "jimmy.codes/testing/disabled",
"rules": {
"jest/require-hook": "off",
},
Expand Down
34 changes: 0 additions & 34 deletions src/configs/jest.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/configs/testing-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { GLOB_E2E, GLOB_TESTS } from "../constants";
const testingLibraryConfig = () => {
return [
{
name: "jimmy.codes/testing-library",
name: "jimmy.codes/testing/testing-library",
files: GLOB_TESTS,
plugins: {
"testing-library": testingLibrary,
Expand All @@ -18,7 +18,7 @@ const testingLibraryConfig = () => {
},
},
{
name: "jimmy.codes/testing-library/disabled",
name: "jimmy.codes/testing/testing-library/disabled",
files: GLOB_E2E,
rules: {
"testing-library/prefer-screen-queries": "off",
Expand Down
23 changes: 9 additions & 14 deletions src/configs/vitest.ts → src/configs/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,31 @@ import jest from "eslint-plugin-jest";

import { GLOB_E2E, GLOB_TESTS } from "../constants";
import { jestRules } from "../rules/jest";
import { type TestingOptions } from "../types";
import testingLibraryConfig from "./testing-library";

interface VitestConfigOptions {
testingLibrary?: boolean;
react?: boolean;
}

const vitestConfig = ({
testingLibrary = false,
react = false,
}: VitestConfigOptions = {}) => {
const testingConfig = ({ framework = "vitest", utilities }: TestingOptions) => {
return [
{
name: "jimmy.codes/vitest",
name: "jimmy.codes/testing",
files: GLOB_TESTS,
...jest.configs["flat/recommended"],
rules: {
...jestRules,
"jest/no-deprecated-functions": "off",
...(framework === "vitest" && {
"jest/no-deprecated-functions": "off",
}),
},
},
{
name: "jimmy.codes/vitest/disabled",
name: "jimmy.codes/testing/disabled",
files: GLOB_E2E,
rules: {
"jest/require-hook": "off",
},
},
...(testingLibrary && react ? testingLibraryConfig() : []),
...(utilities?.includes("testing-library") ? testingLibraryConfig() : []),
];
};

export default vitestConfig;
export default testingConfig;
14 changes: 10 additions & 4 deletions src/factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,28 @@ describe("jimmyDotCodes", () => {
});

it("should create configuration w/ jest", () => {
expect(jimmyDotCodes({ jest: true })).toMatchSnapshot();
expect(jimmyDotCodes({ testing: { framework: "jest" } })).toMatchSnapshot();
});

it("should create configuration w/ vitest", () => {
expect(jimmyDotCodes({ vitest: true })).toMatchSnapshot();
expect(jimmyDotCodes({ testing: true })).toMatchSnapshot();
});

it("should create configuration w/ jest & react & testing library", () => {
expect(
jimmyDotCodes({ jest: true, react: true, testingLibrary: true }),
jimmyDotCodes({
react: true,
testing: { framework: "jest", utilities: ["testing-library"] },
}),
).toMatchSnapshot();
});

it("should create configuration w/ vitest & react & testing library", () => {
expect(
jimmyDotCodes({ vitest: true, react: true, testingLibrary: true }),
jimmyDotCodes({
react: true,
testing: { utilities: ["testing-library"] },
}),
).toMatchSnapshot();
});
});
53 changes: 5 additions & 48 deletions src/factory.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,27 @@
import { type Linter } from "eslint";
import eslintConfigPrettier from "eslint-config-prettier";

import importsConfig from "./configs/imports";
import jestConfig from "./configs/jest";
import reactConfig from "./configs/react";
import testingConfig from "./configs/testing";
import typescriptConfig from "./configs/typescript";
import vitestConfig from "./configs/vitest";
import { GLOB_IGNORES } from "./constants";
import { baseRules } from "./rules/base";
import { type TypescriptOptions } from "./types";
import { getTypescriptOptions } from "./utils";

interface Options {
/**
* Are TypeScript rules are enabled?
* @default false
*/
typescript?: boolean | TypescriptOptions;
/**
* Are React rules are enabled?
* @default false
*/
react?: boolean;
/**
* Are imports rules are enabled?
* @default true
*/
imports?: boolean;
/**
* Are Jest rules are enabled?
* @default false
*/
jest?: boolean;
/**
* Are Vitest rules are enabled?
* @default false
*/
vitest?: boolean;
/**
* Are Testing Library rules are enabled?
* @default false
*/
testingLibrary?: boolean;
/**
* Additional flat configs to either extend or overrides configurations
* @default []
*/
overrides?: Linter.FlatConfig[];
}
import { type Options } from "./types";
import { getTestingOptions, getTypescriptOptions } from "./utils";

export const jimmyDotCodes = ({
typescript = false,
react = false,
imports = true,
jest = false,
vitest = false,
testingLibrary = false,
testing = false,
overrides = [],
}: Options = {}) => {
return [
{ name: "jimmy.codes/base", rules: baseRules },
...(imports ? importsConfig({ typescript }) : []),
...(typescript ? typescriptConfig(getTypescriptOptions(typescript)) : []),
...(react ? reactConfig() : []),
...(jest ? jestConfig({ testingLibrary, react }) : []),
...(vitest ? vitestConfig({ testingLibrary, react }) : []),
...(testing ? testingConfig(getTestingOptions(testing)) : []),
{ name: "jimmy.codes/disabled", ...eslintConfigPrettier },
{
ignores: GLOB_IGNORES,
Expand Down
Loading

0 comments on commit 1c9bd20

Please sign in to comment.