Skip to content

Commit fa9a1ca

Browse files
committed
fix tsconfig target value for tests when in old Node versions
1 parent 06a01f0 commit fa9a1ca

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

scripts/test.ts

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ const NODE_8_LEGACY_DEPENDENCIES = [
2828
];
2929
const NODE_10_LEGACY_DEPENDENCIES = ['[email protected]'];
3030

31+
type JSONValue = string | number | boolean | null | JSONArray | JSONObject;
32+
33+
type JSONObject = {
34+
[key: string]: JSONValue;
35+
};
36+
type JSONArray = Array<JSONValue>;
37+
38+
interface TSConfigJSON extends JSONObject {
39+
compilerOptions: { lib: string[]; target: string };
40+
}
41+
3142
/**
3243
* Run the given shell command, piping the shell process's `stdin`, `stdout`, and `stderr` to that of the current
3344
* process. Returns contents of `stdout`.
@@ -52,7 +63,7 @@ function installLegacyDeps(legacyDeps: string[] = []): void {
5263
* it to a `var` solves this by making it redeclarable.
5364
*
5465
*/
55-
function addTransformer(): void {
66+
function addJestTransformer(): void {
5667
// Though newer `ts-jest` versions support transformers written in TS, the legacy version does not.
5768
run('yarn tsc --skipLibCheck jest/transformers/constReplacer.ts');
5869

@@ -81,6 +92,34 @@ function addTransformer(): void {
8192
fs.writeFileSync(path.resolve('jest/jest.config.js'), code);
8293
}
8394

95+
/**
96+
* Modify a json file on disk.
97+
*
98+
* @param filepath The path to the file to be modified
99+
* @param transformer A function which takes the JSON data as input and returns a mutated version. It may mutate the
100+
* JSON data in place, but it isn't required to do so.
101+
*/
102+
export function modifyJSONFile(filepath: string, transformer: (json: JSONObject) => JSONObject): void {
103+
const fileContents = fs
104+
.readFileSync(filepath)
105+
.toString()
106+
// get rid of comments, which the `jsonc` format allows, but which will crash `JSON.parse`
107+
.replace(/\/\/.*\n/g, '');
108+
const json = JSON.parse(fileContents);
109+
const newJSON = transformer(json);
110+
fs.writeFileSync(filepath, JSON.stringify(newJSON, null, 2));
111+
}
112+
113+
const es6ifyTestTSConfig = (pkg: string): void => {
114+
const filepath = `packages/${pkg}/tsconfig.test.json`;
115+
const transformer = (json: JSONObject): JSONObject => {
116+
const tsconfig = json as TSConfigJSON;
117+
tsconfig.compilerOptions.target = 'es6';
118+
return json;
119+
};
120+
modifyJSONFile(filepath, transformer);
121+
};
122+
84123
/**
85124
* Skip tests which don't apply to Node and therefore don't need to run in older Node versions.
86125
*
@@ -107,15 +146,22 @@ function runTests(): void {
107146
if (CURRENT_NODE_VERSION === '8') {
108147
installLegacyDeps(NODE_8_LEGACY_DEPENDENCIES);
109148
// Inject a `const`-to-`var` transformer, in order to stop Node 8 from complaining when we shadow `global`
110-
addTransformer();
149+
addJestTransformer();
111150
// TODO Right now, this just skips incompatible tests, but it could be skipping more (hence the aspirational name),
112151
// and not just in Node 8. See `skipNonNodeTests`'s docstring.
113152
skipNonNodeTests();
153+
es6ifyTestTSConfig('utils');
114154
runWithIgnores(NODE_8_SKIP_TESTS_PACKAGES);
115155
}
116156
//
117157
else if (CURRENT_NODE_VERSION === '10') {
118158
installLegacyDeps(NODE_10_LEGACY_DEPENDENCIES);
159+
es6ifyTestTSConfig('utils');
160+
runWithIgnores(DEFAULT_SKIP_TESTS_PACKAGES);
161+
}
162+
//
163+
else if (CURRENT_NODE_VERSION === '12') {
164+
es6ifyTestTSConfig('utils');
119165
runWithIgnores(DEFAULT_SKIP_TESTS_PACKAGES);
120166
}
121167
//

0 commit comments

Comments
 (0)