Skip to content

Commit 3f21c31

Browse files
committed
test cleanup, remove or cleanup version checks, skip failing tsconfig "extends" tests on TS 2.7
1 parent 0b5a47b commit 3f21c31

File tree

4 files changed

+132
-96
lines changed

4 files changed

+132
-96
lines changed

src/test/helpers.ts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,7 @@ import semver = require('semver');
1717
const createRequire: typeof _createRequire = require('create-require');
1818
export { tsNodeTypes };
1919

20-
export const nodeSupportsEsmHooks = semver.gte(process.version, '12.16.0');
21-
export const nodeSupportsSpawningChildProcess = semver.gte(
22-
process.version,
23-
'12.17.0'
24-
);
25-
export const nodeUsesNewHooksApi = semver.gte(process.version, '16.12.0');
26-
export const nodeSupportsImportAssertions = semver.gte(
27-
process.version,
28-
'17.1.0'
29-
);
30-
20+
//#region Paths
3121
export const ROOT_DIR = resolve(__dirname, '../..');
3222
export const DIST_DIR = resolve(__dirname, '..');
3323
export const TEST_DIR = join(__dirname, '../../tests');
@@ -40,6 +30,9 @@ export const BIN_SCRIPT_PATH = join(
4030
);
4131
export const BIN_CWD_PATH = join(TEST_DIR, 'node_modules/.bin/ts-node-cwd');
4232
export const BIN_ESM_PATH = join(TEST_DIR, 'node_modules/.bin/ts-node-esm');
33+
//#endregion
34+
35+
//#region command lines
4336
/** Default `ts-node --project` invocation */
4437
export const CMD_TS_NODE_WITH_PROJECT_FLAG = `"${BIN_PATH}" --project "${PROJECT}"`;
4538
/** Default `ts-node` invocation without `--project` */
@@ -48,12 +41,33 @@ export const EXPERIMENTAL_MODULES_FLAG = semver.gte(process.version, '12.17.0')
4841
? ''
4942
: '--experimental-modules';
5043
export const CMD_ESM_LOADER_WITHOUT_PROJECT = `node ${EXPERIMENTAL_MODULES_FLAG} --loader ts-node/esm`;
44+
//#endregion
5145

5246
// `createRequire` does not exist on older node versions
5347
export const testsDirRequire = createRequire(join(TEST_DIR, 'index.js'));
5448

5549
export const ts = testsDirRequire('typescript');
5650

51+
//#region version checks
52+
export const nodeSupportsEsmHooks = semver.gte(process.version, '12.16.0');
53+
export const nodeSupportsSpawningChildProcess = semver.gte(
54+
process.version,
55+
'12.17.0'
56+
);
57+
export const nodeUsesNewHooksApi = semver.gte(process.version, '16.12.0');
58+
export const nodeSupportsImportAssertions = semver.gte(
59+
process.version,
60+
'17.1.0'
61+
);
62+
/** Supports tsconfig "extends" >= v3.2.0 */
63+
export const tsSupportsTsconfigInheritanceViaNodePackages = semver.gte(
64+
ts.version,
65+
'3.2.0'
66+
);
67+
/** Supports --showConfig: >= v3.2.0 */
68+
export const tsSupportsShowConfig = semver.gte(ts.version, '3.2.0');
69+
//#endregion
70+
5771
export const xfs = new NodeFS(fs);
5872

5973
/** Pass to `test.context()` to get access to the ts-node API under test */
@@ -65,6 +79,7 @@ export const contextTsNodeUnderTest = once(async () => {
6579
};
6680
});
6781

82+
//#region install ts-node tarball
6883
const ts_node_install_lock = process.env.ts_node_install_lock as string;
6984
const lockPath = join(__dirname, ts_node_install_lock);
7085

@@ -133,6 +148,7 @@ async function lockedMemoizedOperation(
133148
releaseLock();
134149
}
135150
}
151+
//#endregion
136152

137153
/**
138154
* Get a stream into a string.
@@ -170,6 +186,8 @@ export function getStream(stream: Readable, waitForPattern?: string | RegExp) {
170186
}
171187
}
172188

189+
//#region Reset node environment
190+
173191
const defaultRequireExtensions = captureObjectState(require.extensions);
174192
const defaultProcess = captureObjectState(process);
175193
const defaultModule = captureObjectState(require('module'));
@@ -230,4 +248,6 @@ function resetObject(
230248
Object.defineProperties(object, state.descriptors);
231249
}
232250

251+
//#endregion
252+
233253
export const delay = promisify(setTimeout);

src/test/index.spec.ts

Lines changed: 66 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import * as expect from 'expect';
33
import { join, resolve, sep as pathSep } from 'path';
44
import { tmpdir } from 'os';
55
import semver = require('semver');
6-
import { BIN_PATH_JS, nodeSupportsEsmHooks, ts } from './helpers';
6+
import {
7+
BIN_PATH_JS,
8+
nodeSupportsEsmHooks,
9+
ts,
10+
tsSupportsShowConfig,
11+
tsSupportsTsconfigInheritanceViaNodePackages,
12+
} from './helpers';
713
import { lstatSync, mkdtempSync } from 'fs';
814
import { npath } from '@yarnpkg/fslib';
915
import type _createRequire from 'create-require';
@@ -167,31 +173,29 @@ test.suite('ts-node', (test) => {
167173
expect(stdout).toBe('object\n');
168174
});
169175

170-
if (semver.gte(ts.version, '1.8.0')) {
171-
test('should allow js', async () => {
172-
const { err, stdout } = await exec(
173-
[
174-
CMD_TS_NODE_WITH_PROJECT_FLAG,
175-
'-O "{\\"allowJs\\":true}"',
176-
'-pe "import { main } from \'./allow-js/run\';main()"',
177-
].join(' ')
178-
);
179-
expect(err).toBe(null);
180-
expect(stdout).toBe('hello world\n');
181-
});
176+
test('should allow js', async () => {
177+
const { err, stdout } = await exec(
178+
[
179+
CMD_TS_NODE_WITH_PROJECT_FLAG,
180+
'-O "{\\"allowJs\\":true}"',
181+
'-pe "import { main } from \'./allow-js/run\';main()"',
182+
].join(' ')
183+
);
184+
expect(err).toBe(null);
185+
expect(stdout).toBe('hello world\n');
186+
});
182187

183-
test('should include jsx when `allow-js` true', async () => {
184-
const { err, stdout } = await exec(
185-
[
186-
CMD_TS_NODE_WITH_PROJECT_FLAG,
187-
'-O "{\\"allowJs\\":true}"',
188-
'-pe "import { Foo2 } from \'./allow-js/with-jsx\'; Foo2.sayHi()"',
189-
].join(' ')
190-
);
191-
expect(err).toBe(null);
192-
expect(stdout).toBe('hello world\n');
193-
});
194-
}
188+
test('should include jsx when `allow-js` true', async () => {
189+
const { err, stdout } = await exec(
190+
[
191+
CMD_TS_NODE_WITH_PROJECT_FLAG,
192+
'-O "{\\"allowJs\\":true}"',
193+
'-pe "import { Foo2 } from \'./allow-js/with-jsx\'; Foo2.sayHi()"',
194+
].join(' ')
195+
);
196+
expect(err).toBe(null);
197+
expect(stdout).toBe('hello world\n');
198+
});
195199

196200
test('should eval code', async () => {
197201
const { err, stdout } = await exec(
@@ -501,21 +505,16 @@ test.suite('ts-node', (test) => {
501505
});
502506

503507
test.suite('issue #884', (test) => {
508+
// TODO disabled because it consistently fails on Windows on TS 2.7
509+
test.skipIf(
510+
process.platform === 'win32' && semver.satisfies(ts.version, '2.7')
511+
);
504512
test('should compile', async (t) => {
505-
// TODO disabled because it consistently fails on Windows on TS 2.7
506-
if (
507-
process.platform === 'win32' &&
508-
semver.satisfies(ts.version, '2.7')
509-
) {
510-
t.log('Skipping');
511-
return;
512-
} else {
513-
const { err, stdout } = await exec(
514-
`"${BIN_PATH}" --project issue-884/tsconfig.json issue-884`
515-
);
516-
expect(err).toBe(null);
517-
expect(stdout).toBe('');
518-
}
513+
const { err, stdout } = await exec(
514+
`"${BIN_PATH}" --project issue-884/tsconfig.json issue-884`
515+
);
516+
expect(err).toBe(null);
517+
expect(stdout).toBe('');
519518
});
520519
});
521520

@@ -706,7 +705,7 @@ test.suite('ts-node', (test) => {
706705
]);
707706
});
708707

709-
if (semver.gte(ts.version, '3.2.0')) {
708+
if (tsSupportsTsconfigInheritanceViaNodePackages) {
710709
test('should pull ts-node options from extended `tsconfig.json`', async () => {
711710
const { err, stdout } = await exec(
712711
`${BIN_PATH} --show-config --project ./tsconfig-extends/tsconfig.json`
@@ -810,33 +809,33 @@ test.suite('ts-node', (test) => {
810809
}
811810
);
812811

813-
if (semver.gte(ts.version, '3.2.0')) {
814-
test.suite(
815-
'should bundle @tsconfig/bases to be used in your own tsconfigs',
816-
(test) => {
817-
const macro = test.macro((nodeVersion: string) => async (t) => {
818-
const config = require(`@tsconfig/${nodeVersion}/tsconfig.json`);
819-
const { err, stdout, stderr } = await exec(
820-
`${BIN_PATH} --showConfig -e 10n`,
821-
{
822-
cwd: join(TEST_DIR, 'tsconfig-bases', nodeVersion),
823-
}
824-
);
825-
expect(err).toBe(null);
826-
t.like(JSON.parse(stdout), {
827-
compilerOptions: {
828-
target: config.compilerOptions.target,
829-
lib: config.compilerOptions.lib,
830-
},
831-
});
812+
test.suite(
813+
'should bundle @tsconfig/bases to be used in your own tsconfigs',
814+
(test) => {
815+
test.runIf(tsSupportsTsconfigInheritanceViaNodePackages);
816+
817+
const macro = test.macro((nodeVersion: string) => async (t) => {
818+
const config = require(`@tsconfig/${nodeVersion}/tsconfig.json`);
819+
const { err, stdout, stderr } = await exec(
820+
`${BIN_PATH} --showConfig -e 10n`,
821+
{
822+
cwd: join(TEST_DIR, 'tsconfig-bases', nodeVersion),
823+
}
824+
);
825+
expect(err).toBe(null);
826+
t.like(JSON.parse(stdout), {
827+
compilerOptions: {
828+
target: config.compilerOptions.target,
829+
lib: config.compilerOptions.lib,
830+
},
832831
});
833-
test(`ts-node/node10/tsconfig.json`, macro, 'node10');
834-
test(`ts-node/node12/tsconfig.json`, macro, 'node12');
835-
test(`ts-node/node14/tsconfig.json`, macro, 'node14');
836-
test(`ts-node/node16/tsconfig.json`, macro, 'node16');
837-
}
838-
);
839-
}
832+
});
833+
test(`ts-node/node10/tsconfig.json`, macro, 'node10');
834+
test(`ts-node/node12/tsconfig.json`, macro, 'node12');
835+
test(`ts-node/node14/tsconfig.json`, macro, 'node14');
836+
test(`ts-node/node16/tsconfig.json`, macro, 'node16');
837+
}
838+
);
840839

841840
test.suite('compiler host', (test) => {
842841
test('should execute cli', async () => {
@@ -896,7 +895,7 @@ test.suite('ts-node', (test) => {
896895
});
897896
});
898897

899-
if (semver.gte(ts.version, '3.2.0')) {
898+
if (tsSupportsShowConfig) {
900899
test('--showConfig should log resolved configuration', async (t) => {
901900
function native(path: string) {
902901
return path.replace(/\/|\\/g, pathSep);

src/test/pluggable-dep-resolution.spec.ts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { context } from './testlib';
2-
import { contextTsNodeUnderTest, resetNodeEnvironment } from './helpers';
2+
import {
3+
contextTsNodeUnderTest,
4+
resetNodeEnvironment,
5+
tsSupportsTsconfigInheritanceViaNodePackages,
6+
} from './helpers';
37
import * as expect from 'expect';
48
import { resolve } from 'path';
59

@@ -21,6 +25,8 @@ test.suite(
2125
// ts-node should resolve ts-patch or @swc/core relative to the extended tsconfig
2226
// to ensure we use the known working versions.
2327

28+
const macro = _macro.bind(null, test);
29+
2430
macro('tsconfig-custom-compiler.json', 'root custom compiler');
2531
macro('tsconfig-custom-transpiler.json', 'root custom transpiler');
2632
macro('tsconfig-swc-custom-backend.json', 'root custom swc backend');
@@ -53,24 +59,30 @@ test.suite(
5359
'shared-config @swc/core'
5460
);
5561

56-
macro(
57-
'tsconfig-extend-custom-compiler.json',
58-
'shared-config custom compiler'
59-
);
60-
macro(
61-
'tsconfig-extend-custom-transpiler.json',
62-
'shared-config custom transpiler'
63-
);
64-
macro(
65-
'tsconfig-extend-swc-custom-backend.json',
66-
'shared-config custom swc backend'
67-
);
68-
macro('tsconfig-extend-swc-core.json', 'shared-config @swc/core');
69-
macro('tsconfig-extend-swc-wasm.json', 'shared-config @swc/wasm');
70-
macro('tsconfig-extend-swc.json', 'shared-config @swc/core');
62+
test.suite('"extends"', (test) => {
63+
test.runIf(tsSupportsTsconfigInheritanceViaNodePackages);
64+
65+
const macro = _macro.bind(null, test);
66+
67+
macro(
68+
'tsconfig-extend-custom-compiler.json',
69+
'shared-config custom compiler'
70+
);
71+
macro(
72+
'tsconfig-extend-custom-transpiler.json',
73+
'shared-config custom transpiler'
74+
);
75+
macro(
76+
'tsconfig-extend-swc-custom-backend.json',
77+
'shared-config custom swc backend'
78+
);
79+
macro('tsconfig-extend-swc-core.json', 'shared-config @swc/core');
80+
macro('tsconfig-extend-swc-wasm.json', 'shared-config @swc/wasm');
81+
macro('tsconfig-extend-swc.json', 'shared-config @swc/core');
82+
});
7183

72-
function macro(config: string, expected: string) {
73-
test(`${config} uses ${expected}`, async (t) => {
84+
function _macro(_test: typeof test, config: string, expected: string) {
85+
_test(`${config} uses ${expected}`, async (t) => {
7486
t.teardown(resetNodeEnvironment);
7587

7688
const output = t.context.tsNodeUnderTest

src/test/testlib.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ export interface TestInterface<
115115
skipUnless(conditional: boolean): void;
116116
/** If conditional is true, run tests, otherwise skip them */
117117
runIf(conditional: boolean): void;
118+
/** If conditional is false, skip tests */
119+
skipIf(conditional: boolean): void;
118120

119121
// TODO add teardownEach
120122
}
@@ -286,5 +288,8 @@ function createTestInterface<Context>(opts: {
286288
assertOrderingForDeclaringSkipUnless();
287289
automaticallySkip = automaticallySkip || !runIfTrue;
288290
};
291+
test.skipIf = function (skipIfTrue: boolean) {
292+
test.runIf(!skipIfTrue);
293+
};
289294
return test as any;
290295
}

0 commit comments

Comments
 (0)