-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
jest.test.ts
148 lines (129 loc) · 4.46 KB
/
jest.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { stripIndents } from '@angular-devkit/core/src/utils/literals';
import {
newProject,
runCLI,
runCLIAsync,
uniq,
updateFile,
expectJestTestsToPass,
} from '@nrwl/e2e/utils';
describe('Jest', () => {
beforeAll(() => {
newProject({ name: uniq('proj-jest') });
});
it('should be able test projects using jest', async () => {
await expectJestTestsToPass('@nrwl/workspace:lib');
await expectJestTestsToPass('@nrwl/js:lib');
}, 500000);
it('should merge with jest config globals', async () => {
const testGlobal = `'My Test Global'`;
const mylib = uniq('mylib');
const utilLib = uniq('util-lib');
runCLI(`generate @nrwl/workspace:lib ${mylib} --unit-test-runner jest`);
runCLI(
`generate @nrwl/workspace:lib ${utilLib} --importPath=@global-fun/globals`
);
updateFile(
`libs/${utilLib}/src/index.ts`,
stripIndents`
export function setup() {console.log('i am a global setup function')}
export function teardown() {console.log('i am a global teardown function')}
`
);
updateFile(`libs/${mylib}/src/lib/${mylib}.ts`, `export class Test { }`);
updateFile(
`libs/${mylib}/src/lib/${mylib}.spec.ts`,
`
test('can access jest global', () => {
expect((global as any).testGlobal).toBe(${testGlobal});
});
`
);
updateFile(
`libs/${mylib}/setup.ts`,
stripIndents`
const { registerTsProject } = require('nx/src/utils/register');
const cleanup = registerTsProject('.', 'tsconfig.base.json');
import {setup} from '@global-fun/globals';
export default async function() {setup();}
cleanup();
`
);
updateFile(
`libs/${mylib}/teardown.ts`,
stripIndents`
import { registerTsProject } from 'nx/src/utils/register';
const cleanup = registerTsProject('.', 'tsconfig.base.json');
import {teardown} from '@global-fun/globals';
export default async function() {teardown();}
cleanup();
`
);
updateFile(
`libs/${mylib}/jest.config.ts`,
stripIndents`
module.exports = {
testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'],
transform: {
'^.+\\.(ts|js|html)$': 'ts-jest'
},
resolver: '@nrwl/jest/plugins/resolver',
moduleFileExtensions: ['ts', 'js', 'html'],
coverageReporters: ['html'],
passWithNoTests: true,
globals: { testGlobal: ${testGlobal} },
globalSetup: '<rootDir>/setup.ts',
globalTeardown: '<rootDir>/teardown.ts'
};`
);
const appResult = await runCLIAsync(`test ${mylib} --no-watch`);
expect(appResult.combinedOutput).toContain(
'Test Suites: 1 passed, 1 total'
);
}, 300000);
it('should set the NODE_ENV to `test`', async () => {
const mylib = uniq('mylib');
runCLI(`generate @nrwl/workspace:lib ${mylib} --unit-test-runner jest`);
updateFile(
`libs/${mylib}/src/lib/${mylib}.spec.ts`,
`
test('can access jest global', () => {
expect(process.env.NODE_ENV).toBe('test');
});
`
);
const appResult = await runCLIAsync(`test ${mylib} --no-watch`);
expect(appResult.combinedOutput).toContain(
'Test Suites: 1 passed, 1 total'
);
}, 90000);
it('should support multiple `coverageReporters` through CLI', async () => {
const mylib = uniq('mylib');
runCLI(`generate @nrwl/workspace:lib ${mylib} --unit-test-runner jest`);
updateFile(
`libs/${mylib}/src/lib/${mylib}.spec.ts`,
`
test('can access jest global', () => {
expect(true).toBe(true);
});
`
);
const result = await runCLIAsync(
`test ${mylib} --no-watch --code-coverage --coverageReporters=text --coverageReporters=text-summary`
);
expect(result.stdout).toContain(
'File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s'
); // text
expect(result.stdout).toContain('Coverage summary'); // text-summary
}, 90000);
it('should be able to test node lib with babel-jest', async () => {
const libName = uniq('babel-test-lib');
runCLI(
`generate @nrwl/node:lib ${libName} --buildable --importPath=@some-org/babel-test --publishable --babelJest`
);
const cliResults = await runCLIAsync(`test ${libName}`);
expect(cliResults.combinedOutput).toContain(
'Test Suites: 1 passed, 1 total'
);
}, 90000);
});