Skip to content

Commit

Permalink
Merge pull request #434 from tsedio/fix-generated-test-command
Browse files Browse the repository at this point in the history
Fix generated test command
  • Loading branch information
Romakita authored Jan 18, 2025
2 parents 939bffa + aac04a4 commit 0024682
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 66 deletions.
7 changes: 4 additions & 3 deletions packages/cli-plugin-eslint/src/hooks/EslintInitHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
);
Expand Down
7 changes: 3 additions & 4 deletions packages/cli-plugin-jest/src/CliPluginJestModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,16 @@ 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")} `
});
}

addDevDependencies() {
this.packageJson.addDevDependencies({
"@types/jest": "latest",
jest: "latest",
"ts-jest": "latest"
"@swc/jest": "latest",
jest: "latest"
});
}
}
2 changes: 1 addition & 1 deletion packages/cli-plugin-jest/src/hooks/JestInitHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
});
}
Expand Down
55 changes: 0 additions & 55 deletions packages/cli-plugin-jest/templates/init/jest.config.js.hbs

This file was deleted.

71 changes: 71 additions & 0 deletions packages/cli-plugin-jest/templates/init/jest.config.mjs.hbs
Original file line number Diff line number Diff line change
@@ -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)/)"]
};
1 change: 0 additions & 1 deletion packages/cli-plugin-vitest/src/CliPluginVitestModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
10 changes: 10 additions & 0 deletions packages/cli/src/commands/init/InitCmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/templates/init/tsconfig.spec.json.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
"vitest/globals",
"vitest/importMeta",
"vite/client",
"vitest",{{/if}}
{{#if jest}}
"vitest",{{/if}}{{#if jest}}
"jest",{{/if}}
"node"
]
Expand Down

0 comments on commit 0024682

Please sign in to comment.