Skip to content

Commit 74bf10b

Browse files
committed
feat(cli): Support Jest project initialization
1 parent e5f074a commit 74bf10b

4 files changed

Lines changed: 122 additions & 6 deletions

File tree

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,79 @@
11
import {IInitCmdContext} from "@tsed/cli";
2-
import {Inject, Injectable, OnExec, ProjectPackageJson, Renderer} from "@tsed/cli-core";
2+
import {
3+
Inject,
4+
Injectable,
5+
OnExec,
6+
ProjectPackageJson,
7+
RootRendererService,
8+
ScriptsRendererService,
9+
SrcRendererService
10+
} from "@tsed/cli-core";
11+
import {join} from "path";
12+
import {TEMPLATE_DIR} from "../utils/templateDir";
313

414
@Injectable()
515
export class JestInitHook {
616
@Inject()
717
protected packageJson: ProjectPackageJson;
818

19+
@Inject()
20+
protected srcRenderer: SrcRendererService;
21+
22+
@Inject()
23+
protected rootRenderer: RootRendererService;
24+
25+
@Inject()
26+
protected scriptsRenderer: ScriptsRendererService;
27+
928
@OnExec("init")
10-
onInitExec(ctx: IInitCmdContext) {}
29+
onInitExec(ctx: IInitCmdContext) {
30+
this.addScripts(ctx);
31+
this.addDependencies(ctx);
32+
this.addDevDependencies(ctx);
33+
34+
return [
35+
{
36+
title: "Generate files for mocha",
37+
task: (ctx: any) => {
38+
return this.rootRenderer.renderAll([
39+
"init/jest.config.js.hbs"
40+
], ctx, {
41+
templateDir: TEMPLATE_DIR
42+
});
43+
}
44+
},
45+
{
46+
title: "Generate scripts files for mocha",
47+
task: (ctx: any) => {
48+
return this.scriptsRenderer.renderAll(
49+
[
50+
"init/setup.jest.js.hbs"
51+
], ctx, {
52+
templateDir: TEMPLATE_DIR,
53+
rootDir: join(this.scriptsRenderer.rootDir, "jest")
54+
});
55+
}
56+
}
57+
];
58+
}
1159

1260
addScripts(ctx: IInitCmdContext) {
13-
this.packageJson.addScripts({});
61+
this.packageJson.addScripts({
62+
test: "yarn test:lint && yarn test:coverage",
63+
"test:unit": "cross-env NODE_ENV=test jest",
64+
"test:coverage": "yarn test:unit"
65+
});
1466
}
1567

1668
addDependencies(ctx: IInitCmdContext) {
1769
this.packageJson.addDependencies({}, ctx);
1870
}
1971

2072
addDevDependencies(ctx: IInitCmdContext) {
21-
this.packageJson.addDevDependencies({}, ctx);
73+
this.packageJson.addDevDependencies({
74+
"@types/jest": "latest",
75+
"jest": "latest",
76+
"ts-jest": "latest"
77+
}, ctx);
2278
}
2379
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// For a detailed explanation regarding each configuration property, visit:
2+
// https://jestjs.io/docs/en/configuration.html
3+
4+
module.exports = {
5+
// Automatically clear mock calls and instances between every test
6+
clearMocks: true,
7+
8+
// Indicates whether the coverage information should be collected while executing the test
9+
collectCoverage: true,
10+
11+
// An array of glob patterns indicating a set of files for which coverage information should be collected
12+
// collectCoverageFrom: undefined,
13+
14+
// The directory where Jest should output its coverage files
15+
coverageDirectory: 'coverage',
16+
17+
// An array of regexp pattern strings used to skip coverage collection
18+
coveragePathIgnorePatterns: [
19+
'index.ts',
20+
'/node_modules/'
21+
],
22+
23+
// An object that configures minimum threshold enforcement for coverage results
24+
coverageThreshold: {
25+
global: {
26+
'branches': 70,
27+
'functions': 70,
28+
'lines': 70,
29+
'statements': 70
30+
}
31+
},
32+
33+
// An array of file extensions your modules use
34+
moduleFileExtensions: [
35+
'js',
36+
'json',
37+
'jsx',
38+
'ts',
39+
'tsx',
40+
'node'
41+
],
42+
43+
// The test environment that will be used for testing
44+
testEnvironment: 'node',
45+
46+
// The glob patterns Jest uses to detect test files
47+
testMatch: [
48+
'**/__tests__/**/*.[jt]s?(x)',
49+
'**/?(*.)+(spec|test).[tj]s?(x)'
50+
],
51+
// A map from regular expressions to paths to transformers
52+
transform: {
53+
'\\.(ts)$': 'ts-jest'
54+
},
55+
56+
// The paths to modules that run some code to configure or set up the testing environment before each test
57+
setupFiles: [
58+
'<rootDir>/scripts/jest/setup.jest.js'
59+
]
60+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
process.env.TS_TEST = true

packages/cli/src/services/Features.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,7 @@ registerProvider({
164164
devDependencies: {
165165
"@tsed/cli-plugin-jest": cliVersion
166166
}
167-
},
168-
disabled: true
167+
}
169168
}
170169
]
171170
},

0 commit comments

Comments
 (0)