-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
update-jest-config-ext.ts
147 lines (130 loc) · 4.1 KB
/
update-jest-config-ext.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
import {
formatFiles,
joinPathFragments,
logger,
offsetFromRoot,
ProjectConfiguration,
readJson,
readProjectConfiguration,
stripIndents,
Tree,
updateJson,
updateProjectConfiguration,
} from '@nrwl/devkit';
import { jestConfigObject } from '../../utils/config/functions';
import { dirname, extname, join } from 'path';
import {
removePropertyFromJestConfig,
addPropertyToJestConfig,
} from '../../utils/config/update-config';
import { JestExecutorOptions } from '../../executors/jest/schema';
import { forEachExecutorOptions } from '@nrwl/workspace/src/utilities/executor-options-utils';
const allowedExt = ['.ts', '.js'];
let isRootPresetUpdated = false;
function updateJestPreset(
tree: Tree,
options: JestExecutorOptions,
projectName: string
) {
const oldConfig = jestConfigObject(tree, options.jestConfig);
if (!oldConfig) {
return;
}
// if using the root preset and the root preset was updated to ts file.
// then update the jest config
if (isRootPresetUpdated && oldConfig?.preset?.endsWith('jest.preset.js')) {
removePropertyFromJestConfig(tree, options.jestConfig, 'preset');
addPropertyToJestConfig(
tree,
options.jestConfig,
'preset',
joinPathFragments(
offsetFromRoot(dirname(options.jestConfig)),
'jest.preset.ts'
),
{ valueAsString: false }
);
}
}
function updateTsConfig(tree: Tree, tsConfigPath: string) {
try {
updateJson(tree, tsConfigPath, (json) => {
json.exclude = Array.from(
new Set([...(json.exclude || []), 'jest.config.ts'])
);
return json;
});
} catch (e) {
logger.warn(
stripIndents`Nx Unable to update ${tsConfigPath}. Please manually ignore the jest.config.ts file.`
);
}
}
function isJestConfigValid(tree: Tree, options: JestExecutorOptions) {
const configExt = extname(options.jestConfig);
if (!tree.exists(options.jestConfig) || !allowedExt.includes(configExt)) {
logger.debug(
`unable to update file because it doesn't exist or is not a js or ts file. Config: ${
options.jestConfig
}. Exists?: ${tree.exists(options.jestConfig)}`
);
return false;
}
return true;
}
function updateTsconfigSpec(
tree: Tree,
projectConfig: ProjectConfiguration,
path
) {
updateJson(tree, joinPathFragments(projectConfig.root, path), (json) => {
json.include = Array.from(
new Set([...(json.include || []), 'jest.config.ts'])
);
return json;
});
}
export async function updateJestConfigExt(tree: Tree) {
if (tree.exists('jest.config.js')) {
tree.rename('jest.config.js', 'jest.config.ts');
}
if (tree.exists('jest.preset.js')) {
isRootPresetUpdated = true;
tree.rename('jest.preset.js', 'jest.preset.ts');
}
forEachExecutorOptions<JestExecutorOptions>(
tree,
'@nrwl/jest:jest',
(options, projectName, target, configuration) => {
const projectConfig = readProjectConfiguration(tree, projectName);
if (!isJestConfigValid(tree, options)) {
return;
}
updateJestPreset(tree, options, projectName);
const newJestConfigPath = options.jestConfig.replace('.js', '.ts');
tree.rename(options.jestConfig, newJestConfigPath);
const rootFiles = tree.children(projectConfig.root);
for (const fileName of rootFiles) {
if (fileName === 'tsconfig.json') {
const filePath = joinPathFragments(projectConfig.root, fileName);
const tsConfig = readJson(tree, filePath);
if (tsConfig.references) {
for (const { path } of tsConfig.references) {
if (path.endsWith('tsconfig.spec.json')) {
updateTsconfigSpec(tree, projectConfig, path);
continue;
}
updateTsConfig(tree, joinPathFragments(projectConfig.root, path));
}
} else {
updateTsConfig(tree, filePath);
}
}
}
projectConfig.targets[target].options.jestConfig = newJestConfigPath;
updateProjectConfiguration(tree, projectName, projectConfig);
}
);
await formatFiles(tree);
}
export default updateJestConfigExt;