diff --git a/packages/cli-plugin-eslint/src/hooks/EslintInitHook.ts b/packages/cli-plugin-eslint/src/hooks/EslintInitHook.ts index 8e6e1de7e..1ce3bbc7b 100644 --- a/packages/cli-plugin-eslint/src/hooks/EslintInitHook.ts +++ b/packages/cli-plugin-eslint/src/hooks/EslintInitHook.ts @@ -79,8 +79,8 @@ export class EslintInitHook { addScripts(ctx: InitCmdContext) { this.packageJson.addScripts({ - "test:lint": "eslint '**/*.{ts,js}'", - "test:lint:fix": "eslint '**/*.{ts,js}' --fix" + "test:lint": "eslint", + "test:lint:fix": "eslint --fix" }); if (ctx.prettier) { @@ -102,7 +102,8 @@ export class EslintInitHook { eslint: "latest", "eslint-config-prettier": "latest", "eslint-plugin-prettier": "latest", - "eslint-plugin-simple-import-sort": "latest" + "eslint-plugin-simple-import-sort": "latest", + globals: "latest" }, ctx ); diff --git a/packages/cli-plugin-jest/src/CliPluginJestModule.ts b/packages/cli-plugin-jest/src/CliPluginJestModule.ts index 56ff5dd66..af82b009a 100644 --- a/packages/cli-plugin-jest/src/CliPluginJestModule.ts +++ b/packages/cli-plugin-jest/src/CliPluginJestModule.ts @@ -21,8 +21,7 @@ export class CliPluginJestModule { const runtime = this.runtimes.get(); this.packageJson.addScripts({ - test: `${runtime.run("test:lint")} && ${runtime.run("test:coverage")} `, - "test:unit": "cross-env NODE_ENV=test jest", + "test:unit": "cross-env NODE_OPTIONS=--experimental-vm-modules NODE_ENV=test jest", "test:coverage": `${runtime.run("test:unit")} ` }); } @@ -30,8 +29,8 @@ export class CliPluginJestModule { addDevDependencies() { this.packageJson.addDevDependencies({ "@types/jest": "latest", - jest: "latest", - "ts-jest": "latest" + "@swc/jest": "latest", + jest: "latest" }); } } diff --git a/packages/cli-plugin-jest/src/hooks/JestInitHook.ts b/packages/cli-plugin-jest/src/hooks/JestInitHook.ts index b7d17d048..441d773f6 100644 --- a/packages/cli-plugin-jest/src/hooks/JestInitHook.ts +++ b/packages/cli-plugin-jest/src/hooks/JestInitHook.ts @@ -30,7 +30,7 @@ export class JestInitHook { { title: "Generate files for jest", task: (ctx: any) => { - return this.rootRenderer.renderAll(["jest.config.js.hbs"], ctx, { + return this.rootRenderer.renderAll(["jest.config.ts.hbs"], ctx, { templateDir: `${TEMPLATE_DIR}/init` }); } diff --git a/packages/cli-plugin-jest/templates/init/jest.config.js.hbs b/packages/cli-plugin-jest/templates/init/jest.config.js.hbs deleted file mode 100644 index be212580b..000000000 --- a/packages/cli-plugin-jest/templates/init/jest.config.js.hbs +++ /dev/null @@ -1,55 +0,0 @@ -// For a detailed explanation regarding each configuration property, visit: -// https://jestjs.io/docs/en/configuration.html - -module.exports = { - // Automatically clear mock calls and instances between every test - clearMocks: true, - - // Indicates whether the coverage information should be collected while executing the test - collectCoverage: true, - - // An array of glob patterns indicating a set of files for which coverage information should be collected - // collectCoverageFrom: undefined, - - // The directory where Jest should output its coverage files - coverageDirectory: 'coverage', - - // An array of regexp pattern strings used to skip coverage collection - coveragePathIgnorePatterns: [ - 'index.ts', - '/node_modules/' - ], - - // An object that configures minimum threshold enforcement for coverage results - coverageThreshold: { - global: { - 'branches': 70, - 'functions': 70, - 'lines': 70, - 'statements': 70 - } - }, - - // An array of file extensions your modules use - moduleFileExtensions: [ - 'js', - 'json', - 'jsx', - 'ts', - 'tsx', - 'node' - ], - - // The test environment that will be used for testing - testEnvironment: 'node', - - // The glob patterns Jest uses to detect test files - testMatch: [ - '**/src/**/__tests__/**/*.[jt]s?(x)', - '**/src/**/?(*.)+(spec|test).[tj]s?(x)' - ], - // A map from regular expressions to paths to transformers - transform: { - '\\.(ts)$': 'ts-jest' - } -} diff --git a/packages/cli-plugin-jest/templates/init/jest.config.mjs.hbs b/packages/cli-plugin-jest/templates/init/jest.config.mjs.hbs new file mode 100644 index 000000000..eac29b91e --- /dev/null +++ b/packages/cli-plugin-jest/templates/init/jest.config.mjs.hbs @@ -0,0 +1,71 @@ +// For a detailed explanation regarding each configuration property, visit: +// https://jestjs.io/docs/en/configuration.html + +/** @type {import('jest').Config} */ +export default { + // Automatically clear mock calls and instances between every test + clearMocks: true, + + // Indicates whether the coverage information should be collected while executing the test + collectCoverage: true, + + // An array of glob patterns indicating a set of files for which coverage information should be collected + // collectCoverageFrom: undefined, + + // The directory where Jest should output its coverage files + coverageDirectory: "coverage", + + // An array of regexp pattern strings used to skip coverage collection + coveragePathIgnorePatterns: ["index.ts", "/node_modules/"], + + // An object that configures minimum threshold enforcement for coverage results + coverageThreshold: { + global: { + branches: 70, + functions: 70, + lines: 70, + statements: 70 + } + }, + + // An array of file extensions your modules use + moduleFileExtensions: ["js", "json", "jsx", "ts", "tsx", "node"], + extensionsToTreatAsEsm: [".ts", ".tsx"], + // The test environment that will be used for testing + testEnvironment: "node", + + // The glob patterns Jest uses to detect test files + testMatch: ["**/src/**/__tests__/**/*.[jt]s?(x)", "**/src/**/?(*.)+(spec|test).[tj]s?(x)"], + // A map from regular expressions to paths to transformers + transform: { + "^.+\\.(j|t)sx?$": [ + "@swc/jest", + { + jsc: { + parser: { + syntax: "typescript", + tsx: true, + decorators: true, + dynamicImport: true, + importMeta: true, + preserveAllComments: true + }, + target: "esnext", + transform: { + useDefineForClassFields: false, + legacyDecorator: true, + decoratorMetadata: true + } + }, + module: { + type: "es6" + } + } + ] + }, + moduleNameMapper: { + "^(\\.{1,2}/.*)\\.js$": "$1" + }, + testPathIgnorePatterns: ["/node_modules/", "/dist/"], + transformIgnorePatterns: ["/node_modules/(?!(module-name|another-module)/)"] +}; diff --git a/packages/cli-plugin-vitest/src/CliPluginVitestModule.ts b/packages/cli-plugin-vitest/src/CliPluginVitestModule.ts index e5ad4d2e4..7fa724819 100644 --- a/packages/cli-plugin-vitest/src/CliPluginVitestModule.ts +++ b/packages/cli-plugin-vitest/src/CliPluginVitestModule.ts @@ -21,7 +21,6 @@ export class CliPluginVitestModule { const runtime = this.runtimes.get(); this.packageJson.addScripts({ - test: `${runtime.run("test:lint")} && ${runtime.run("test:coverage")}`, "test:unit": "cross-env NODE_ENV=test vitest run", "test:watch": "cross-env NODE_ENV=test vitest", "test:coverage": `cross-env NODE_ENV=test vitest run --coverage` diff --git a/packages/cli/src/commands/init/InitCmd.ts b/packages/cli/src/commands/init/InitCmd.ts index 604d807a1..e5ecd920d 100644 --- a/packages/cli/src/commands/init/InitCmd.ts +++ b/packages/cli/src/commands/init/InitCmd.ts @@ -342,6 +342,16 @@ export class InitCmd implements CommandProvider { addScripts(ctx: InitCmdContext): void { this.packageJson.addScripts(this.runtimes.scripts(ctx)); + + if (ctx.eslint || ctx.testing) { + const runtime = this.runtimes.get(); + + const scripts = { + test: [ctx.eslint && runtime.run("test:lint"), ctx.testing && runtime.run("test:coverage")].filter(Boolean).join("&&") + }; + + this.packageJson.addScripts(scripts); + } } addDependencies(ctx: InitCmdContext) { diff --git a/packages/cli/templates/init/tsconfig.spec.json.hbs b/packages/cli/templates/init/tsconfig.spec.json.hbs index 26fb19f6b..a7a893336 100644 --- a/packages/cli/templates/init/tsconfig.spec.json.hbs +++ b/packages/cli/templates/init/tsconfig.spec.json.hbs @@ -8,8 +8,7 @@ "vitest/globals", "vitest/importMeta", "vite/client", - "vitest",{{/if}} - {{#if jest}} + "vitest",{{/if}}{{#if jest}} "jest",{{/if}} "node" ]