-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
update-jest-config-ext.spec.ts
154 lines (136 loc) · 5.35 KB
/
update-jest-config-ext.spec.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
149
150
151
152
153
154
import {
readJson,
readProjectConfiguration,
Tree,
updateJson,
updateProjectConfiguration,
} from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { jestInitGenerator } from '@nrwl/jest';
import { updateJestConfigExt } from './update-jest-config-ext';
import { libraryGenerator as workspaceLib } from '@nrwl/workspace';
describe('Jest Migration (v14.0.0)', () => {
let tree: Tree;
beforeEach(async () => {
tree = createTreeWithEmptyWorkspace(2);
jestInitGenerator(tree, { js: true, skipPackageJson: true });
await workspaceLib(tree, { name: 'lib-one' });
tree.rename('libs/lib-one/jest.config.ts', 'libs/lib-one/jest.config.js');
updateProjectConfiguration(tree, 'lib-one', {
...readProjectConfiguration(tree, 'lib-one'),
targets: {
test: {
executor: '@nrwl/jest:jest',
options: {
jestConfig: 'libs/lib-one/jest.config.js',
passWithNoTests: true,
},
},
},
});
});
it('should rename project jest.config.js to jest.config.ts', async () => {
await updateJestConfigExt(tree);
expect(tree.exists('libs/lib-one/jest.config.ts')).toBeTruthy();
expect(tree.read('libs/lib-one/jest.config.ts', 'utf-8')).toMatchSnapshot();
});
it('should rename root jest files', async () => {
await updateJestConfigExt(tree);
expect(tree.exists('jest.config.ts')).toBeTruthy();
expect(tree.exists('jest.preset.ts')).toBeTruthy();
});
it('should update jest.config.ts preset to use the jest.preset.ts', async () => {
tree.rename('libs/lib-one/jest.config.js', 'libs/lib-one/jest.config.ts');
const projectConfig = readProjectConfiguration(tree, 'lib-one');
updateProjectConfiguration(tree, 'lib-one', {
...projectConfig,
targets: {
test: {
...projectConfig.targets.test,
options: {
jestConfig: 'libs/lib-one/jest.config.ts',
passWithNoTests: true,
},
},
},
});
expect(tree.exists('libs/lib-one/jest.config.ts')).toBeTruthy();
await updateJestConfigExt(tree);
expect(tree.exists('libs/lib-one/jest.config.ts')).toBeTruthy();
expect(tree.read('libs/lib-one/jest.config.ts', 'utf-8')).toMatchSnapshot();
});
it('should only update js/ts files', async () => {
tree.rename('libs/lib-one/jest.config.js', 'libs/lib-one/jest.config.ts');
updateProjectConfiguration(tree, 'lib-one', {
...readProjectConfiguration(tree, 'lib-one'),
targets: {
test: {
executor: '@nrwl/jest:jest',
options: {
jestConfig: 'libs/lib-one/jest.config.ts',
passWithNoTests: true,
},
},
},
});
await workspaceLib(tree, { name: 'lib-two' });
tree.delete('libs/lib-two/jest.config.ts'); // lib generator creates a ts file
tree.write('libs/lib-two/jest.config.json', '{}');
updateProjectConfiguration(tree, 'lib-two', {
...readProjectConfiguration(tree, 'lib-two'),
targets: {
test: {
executor: '@nrwl/jest:jest',
options: {
jestConfig: 'libs/lib-two/jest.config.json',
passWithNoTests: true,
},
},
},
});
await workspaceLib(tree, { name: 'lib-three' });
expect(tree.exists('libs/lib-one/jest.config.ts')).toBeTruthy();
await updateJestConfigExt(tree);
expect(tree.exists('libs/lib-one/jest.config.ts')).toBeTruthy();
expect(tree.exists('libs/lib-two/jest.config.ts')).toBeFalsy();
expect(tree.exists('libs/lib-two/jest.config.json')).toBeTruthy();
expect(tree.exists('libs/lib-three/jest.config.ts')).toBeTruthy();
});
it('should not throw error if file does not exit', async () => {
tree.delete('libs/lib-one/jest.config.js');
await updateJestConfigExt(tree);
expect(tree.exists('libs/lib-one/jest.config.ts')).toBeFalsy();
expect(tree.exists('libs/lib-one/jest.config.js')).toBeFalsy();
});
it('should update correct tsconfigs', async () => {
updateJson(tree, 'libs/lib-one/tsconfig.lib.json', (json) => {
json.exclude = ['**/*.spec.ts'];
return json;
});
updateJson(tree, 'libs/lib-one/tsconfig.spec.json', (json) => {
json.include = ['**/*.spec.ts'];
return json;
});
await updateJestConfigExt(tree);
const tsconfig = readJson(tree, 'libs/lib-one/tsconfig.json');
const libTsConfig = readJson(tree, 'libs/lib-one/tsconfig.lib.json');
const specTsConfig = readJson(tree, 'libs/lib-one/tsconfig.spec.json');
expect(tsconfig.exclude).toBeFalsy();
expect(libTsConfig.exclude).toEqual(['**/*.spec.ts', 'jest.config.ts']);
expect(specTsConfig.exclude).toBeFalsy();
expect(specTsConfig.include).toEqual(['**/*.spec.ts', 'jest.config.ts']);
});
it('should add exclude to root tsconfig with no references', async () => {
tree.delete('libs/lib-one/tsconfig.spec.json');
tree.delete('libs/lib-one/tsconfig.lib.json');
updateJson(tree, 'libs/lib-one/tsconfig.json', (json) => {
delete json.references;
return json;
});
await updateJestConfigExt(tree);
const tsconfig = readJson(tree, 'libs/lib-one/tsconfig.json');
expect(tsconfig.exclude).toEqual(['jest.config.ts']);
expect(tree.exists('libs/lib-one/tsconfig.spec.json')).toBeFalsy();
expect(tree.exists('libs/lib-one/tsconfig.lib.json')).toBeFalsy();
});
});