Allow JSON imports in NodeJS 16.15 by queengooborg · Pull Request #1792 · TypeStrong/ts-node
Awesome, thanks!
We run our tests against a bunch of node versions to test stuff like this, and we conditionally check the node version in the tests to figure out what we should test.
Here is the test for JSON imports, where we check the node version. Depending on the check, we either pass --experimental-json-modules or we don't.
| test.suite('supports import assertions', (test) => { | |
| test.runIf(nodeSupportsImportAssertions); | |
| test.suite('node >=17.5.0', (test) => { | |
| test.runIf(semver.gte(process.version, '17.5.0')); | |
| test('Can import JSON modules with appropriate assertion', async (t) => { | |
| const { err, stdout } = await exec( | |
| `${CMD_ESM_LOADER_WITHOUT_PROJECT} ./importJson.ts`, | |
| { | |
| cwd: resolve(TEST_DIR, 'esm-import-assertions'), | |
| } | |
| ); | |
| expect(err).toBe(null); | |
| expect(stdout.trim()).toBe( | |
| 'A fuchsia car has 2 seats and the doors are open.\nDone!' | |
| ); | |
| }); | |
| }); | |
| test.suite('node <17.5.0', (test) => { | |
| test.runIf(semver.lt(process.version, '17.5.0')); | |
| test('Can import JSON using the appropriate flag and assertion', async (t) => { | |
| const { err, stdout } = await exec( | |
| `${CMD_ESM_LOADER_WITHOUT_PROJECT} --experimental-json-modules ./importJson.ts`, | |
| { | |
| cwd: resolve(TEST_DIR, 'esm-import-assertions'), | |
| } | |
| ); | |
| expect(err).toBe(null); | |
| expect(stdout.trim()).toBe( | |
| 'A fuchsia car has 2 seats and the doors are open.\nDone!' | |
| ); | |
| }); | |
| }); | |
| }); |
Can you also update that version check in the tests? Most of the version checks are declared as boolean exports from helpers.ts, like this:
| //#region version checks | |
| export const nodeSupportsEsmHooks = semver.gte(process.version, '12.16.0'); | |
| export const nodeSupportsSpawningChildProcess = semver.gte( | |
| process.version, | |
| '12.17.0' | |
| ); | |
| export const nodeUsesNewHooksApi = semver.gte(process.version, '16.12.0'); | |
| export const nodeSupportsImportAssertions = semver.gte( | |
| process.version, | |
| '17.1.0' | |
| ); | |
| /** Supports tsconfig "extends" >= v3.2.0 */ | |
| export const tsSupportsTsconfigInheritanceViaNodePackages = semver.gte( | |
| ts.version, | |
| '3.2.0' | |
| ); | |
| /** Supports --showConfig: >= v3.2.0 */ | |
| export const tsSupportsShowConfig = semver.gte(ts.version, '3.2.0'); | |
| //#endregion |
Can we move this version check into that file? Can be named something like export const nodeSupportUnflaggedJsonImports =