Skip to content

Commit

Permalink
feat: ✨ dynamically import jest plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmy-guzman committed Nov 9, 2024
1 parent b261977 commit 409d9ee
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 38 deletions.
8 changes: 4 additions & 4 deletions src/configs/testing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { ALLOWED_VITEST_FUNCS } from "../constants";
import { testingConfig } from "./testing";

describe("testingConfig", () => {
it("should create default config w/ vitest overrides", () => {
const [, vitest] = testingConfig({}, false);
it("should create default config w/ vitest overrides", async () => {
const [, vitest] = await testingConfig({}, false);

expect(vitest?.rules).toStrictEqual(
expect.objectContaining({
Expand All @@ -18,8 +18,8 @@ describe("testingConfig", () => {
);
});

it("should create default config w/o vitest overrides", () => {
const [, jest] = testingConfig({ framework: "jest" }, false);
it("should create default config w/o vitest overrides", async () => {
const [, jest] = await testingConfig({ framework: "jest" }, false);

expect(jest?.rules).toStrictEqual(
expect.not.objectContaining({
Expand Down
13 changes: 7 additions & 6 deletions src/configs/testing.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
import jest from "eslint-plugin-jest";

import type { Rules, TestingOptions } from "../types";

import { ALLOWED_VITEST_FUNCS, GLOB_E2E, GLOB_TESTS } from "../constants";
import { hasJest, hasVitest } from "../has-dep";
import { jestRules } from "../rules/jest";
import { interopDefault } from "../utils";

export const testingConfig = (
export const testingConfig = async (
{ framework = "vitest" }: TestingOptions = {},
autoDetect = true,
) => {
const jestPlugin = await interopDefault(import("eslint-plugin-jest"));

const isVitest = autoDetect ? hasVitest() : framework === "vitest";
const isJest = framework === "jest" || (autoDetect && hasJest());

return [
{
files: GLOB_TESTS,
name: "jimmy.codes/testing",
...jest.configs["flat/recommended"],
...jestPlugin.configs["flat/recommended"],
},
...(isVitest
? [
{
files: GLOB_TESTS,
name: "jimmy.codes/testing/vitest",
...jest.configs["flat/recommended"],
...jestPlugin.configs["flat/recommended"],
rules: {
...jestRules,
"jest/no-deprecated-functions": "off",
Expand All @@ -43,7 +44,7 @@ export const testingConfig = (
{
files: GLOB_TESTS,
name: "jimmy.codes/testing/jest",
...jest.configs["flat/recommended"],
...jestPlugin.configs["flat/recommended"],
rules: jestRules,
},
]
Expand Down
20 changes: 12 additions & 8 deletions src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ import {
hasTypescript,
} from "./has-dep";
import {
includeTanstackQuery,
includeTestingLibrary,
testingOptions,
typescriptOptions,
addTanstackQuery,
addTestingLibrary,
getReactOptions,
getTestingOptions,
getTypescriptOptions,
} from "./utils";

export const jimmyDotCodes = async (
Expand All @@ -44,14 +45,17 @@ export const jimmyDotCodes = async (
}: Options = {},
...moreConfigs: Linter.Config[] | TypedConfigItem[]
) => {
const reactOptions = getReactOptions(react);
const testingOptions = getTestingOptions(testing);
const typescriptOptions = getTypescriptOptions(typescript);
const isTypescriptEnabled = typescript || (autoDetect && hasTypescript());
const isReactEnabled = react || (autoDetect && hasReact());
const isTestingEnabled = testing || (autoDetect && hasTesting());
const isAstroEnabled = astro || (autoDetect && hasAstro());
const isTanstackQueryEnabled =
includeTanstackQuery(react) || (autoDetect && hasReactQuery());
addTanstackQuery(reactOptions) || (autoDetect && hasReactQuery());
const isTestingLibraryEnabled =
includeTestingLibrary(testing) || (autoDetect && hasTestingLibrary());
addTestingLibrary(testingOptions) || (autoDetect && hasTestingLibrary());

return [
javascriptConfig(),
Expand All @@ -60,11 +64,11 @@ export const jimmyDotCodes = async (
unicornConfig(),
eslintCommentsConfig(),
importsConfig({ typescript: isTypescriptEnabled }),
isTypescriptEnabled ? typescriptConfig(typescriptOptions(typescript)) : [],
isTypescriptEnabled ? typescriptConfig(typescriptOptions) : [],
isReactEnabled ? await reactConfig() : [],
isTanstackQueryEnabled ? await tanstackQuery() : [],
isAstroEnabled ? await astroConfig() : [],
isTestingEnabled ? testingConfig(testingOptions(testing), autoDetect) : [],
isTestingEnabled ? await testingConfig(testingOptions, autoDetect) : [],
isTestingLibraryEnabled ? await testingLibrary() : [],
prettierConfig(),
commonjsConfig(),
Expand Down
18 changes: 11 additions & 7 deletions src/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { reactOptions, testingOptions, typescriptOptions } from "./utils";
import {
getReactOptions,
getTestingOptions,
getTypescriptOptions,
} from "./utils";

describe("getTypescriptOptions", () => {
it("should return default options when a boolean is provided", () => {
expect(typescriptOptions(true)).toStrictEqual({
expect(getTypescriptOptions(true)).toStrictEqual({
project: "./tsconfig.json",
});
});
it("should return override options when an object is provided", () => {
expect(
typescriptOptions({
getTypescriptOptions({
project: "./tsconfig.eslint.json",
}),
).toStrictEqual({
Expand All @@ -19,13 +23,13 @@ describe("getTypescriptOptions", () => {

describe("getTestingOptions", () => {
it("should return default options when a boolean is provided", () => {
expect(testingOptions(true)).toStrictEqual({
expect(getTestingOptions(true)).toStrictEqual({
framework: "vitest",
});
});
it("should return override options when an object is provided", () => {
expect(
testingOptions({
getTestingOptions({
framework: "jest",
utilities: ["testing-library"],
}),
Expand All @@ -38,11 +42,11 @@ describe("getTestingOptions", () => {

describe("getReactOptions", () => {
it("should return default options when a boolean is provided", () => {
expect(reactOptions(true)).toStrictEqual({ utilities: [] });
expect(getReactOptions(true)).toStrictEqual({ utilities: [] });
});
it("should return override options when an object is provided", () => {
expect(
reactOptions({
getReactOptions({
utilities: ["@tanstack/query"],
}),
).toStrictEqual({
Expand Down
20 changes: 7 additions & 13 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
import type { ReactOptions, TestingOptions, TypescriptOptions } from "./types";

export const typescriptOptions = (options: boolean | TypescriptOptions) => {
export const getTypescriptOptions = (options: boolean | TypescriptOptions) => {
return typeof options === "object"
? options
: {
project: "./tsconfig.json",
};
};

export const testingOptions = (options: boolean | TestingOptions) => {
export const getTestingOptions = (options: boolean | TestingOptions) => {
return typeof options === "object"
? options
: {
framework: "vitest" as const,
};
};

export const reactOptions = (options: boolean | ReactOptions) => {
export const getReactOptions = (options: boolean | ReactOptions) => {
return typeof options === "object" ? options : { utilities: [] };
};

export const includeTanstackQuery = (options: boolean | ReactOptions) => {
return (
typeof options === "object" &&
Boolean(options.utilities?.includes("@tanstack/query"))
);
export const addTanstackQuery = (options: ReactOptions) => {
return Boolean(options.utilities?.includes("@tanstack/query"));
};

export const includeTestingLibrary = (options: boolean | TestingOptions) => {
return (
typeof options === "object" &&
Boolean(options.utilities?.includes("testing-library"))
);
export const addTestingLibrary = (options: TestingOptions) => {
return Boolean(options.utilities?.includes("testing-library"));
};

/* v8 ignore start */
Expand Down

0 comments on commit 409d9ee

Please sign in to comment.