Skip to content

Commit d63e20b

Browse files
committed
fix tsconfig target value for tests when in old Node versions
1 parent 74f4599 commit d63e20b

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

scripts/test.ts

Lines changed: 47 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,33 @@ 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, mutates it in place, and returns it.
100+
*/
101+
export function modifyJSONFile(filepath: string, transformer: (json: JSONObject) => JSONObject): void {
102+
const fileContents = fs
103+
.readFileSync(filepath)
104+
.toString()
105+
.replace(/\/\/.*\n/g, '');
106+
// fileContents.replace(/\/\/.*\n/g, '');
107+
const json = JSON.parse(fileContents);
108+
const newJSON = transformer(json);
109+
fs.writeFileSync(filepath, JSON.stringify(newJSON, null, 2));
110+
}
111+
112+
const es6ifyTestTSConfig = (pkg: string): void => {
113+
const filepath = `packages/${pkg}/tsconfig.test.json`;
114+
const transformer = (json: JSONObject): JSONObject => {
115+
const tsconfig = json as TSConfigJSON;
116+
tsconfig.compilerOptions.target = 'es6';
117+
return json;
118+
};
119+
modifyJSONFile(filepath, transformer);
120+
};
121+
84122
/**
85123
* Skip tests which don't apply to Node and therefore don't need to run in older Node versions.
86124
*
@@ -107,15 +145,22 @@ function runTests(): void {
107145
if (CURRENT_NODE_VERSION === '8') {
108146
installLegacyDeps(NODE_8_LEGACY_DEPENDENCIES);
109147
// Inject a `const`-to-`var` transformer, in order to stop Node 8 from complaining when we shadow `global`
110-
addTransformer();
148+
addJestTransformer();
111149
// TODO Right now, this just skips incompatible tests, but it could be skipping more (hence the aspirational name),
112150
// and not just in Node 8. See `skipNonNodeTests`'s docstring.
113151
skipNonNodeTests();
152+
es6ifyTestTSConfig('utils');
114153
runWithIgnores(NODE_8_SKIP_TESTS_PACKAGES);
115154
}
116155
//
117156
else if (CURRENT_NODE_VERSION === '10') {
118157
installLegacyDeps(NODE_10_LEGACY_DEPENDENCIES);
158+
es6ifyTestTSConfig('utils');
159+
runWithIgnores(DEFAULT_SKIP_TESTS_PACKAGES);
160+
}
161+
//
162+
else if (CURRENT_NODE_VERSION === '12') {
163+
es6ifyTestTSConfig('utils');
119164
runWithIgnores(DEFAULT_SKIP_TESTS_PACKAGES);
120165
}
121166
//

0 commit comments

Comments
 (0)