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 8, 2024
1 parent b0cbda2 commit 10f49ab
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 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
4 changes: 3 additions & 1 deletion src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ export const jimmyDotCodes = async (
isReactEnabled ? await reactConfig() : [],
isTanstackQueryEnabled ? await tanstackQuery() : [],
isAstroEnabled ? await astroConfig() : [],
isTestingEnabled ? testingConfig(testingOptions(testing), autoDetect) : [],
isTestingEnabled
? await testingConfig(testingOptions(testing), autoDetect)
: [],
isTestingLibraryEnabled ? await testingLibrary() : [],
prettierConfig(),
commonjsConfig(),
Expand Down

0 comments on commit 10f49ab

Please sign in to comment.