From e0f460e34e1cf4150264b8cafe3310a682c3570b Mon Sep 17 00:00:00 2001 From: James Addison Date: Fri, 9 Jul 2021 05:30:37 +0100 Subject: [PATCH] Test coverage: moduleType overrides during ts-node loader usage (#1376) * Test coverage: add test case to confirm that moduleType overrides are applied for ts-node in loader mode * Ensure that a default export exists for the esm-exception module * Re-order tsconfig.json glob rules, and use implicit globbing * lint fixup: apply prettier * Add 'experimental-specifier-resolution' flag to NPM options in ESM module override test * Ensure that a default export exists for the cjs-subdir module * Revert "Ensure that a default export exists for the cjs-subdir module" This reverts commit c64cf928787b476911dc86657dde7d4289a277a4. * Revert "Add 'experimental-specifier-resolution' flag to NPM options in ESM module override test" This reverts commit 1093df80dcf7782910a00c8c292f3bd2a6a8a5a7. * Specify tsconfig project using TS_NODE_PROJECT environment variable * Use full file paths in preference to directory-default module imports This avoids ERR_UNSUPPORTED_DIR_IMPORT, without having to provide the 'experimental-specifier-resolution' flag to ts-node * Update index.ts * Update index.ts * Update tsconfig.json * Extract execModuleTypeOverride function * Add expected failure cases for Node 12.15, 14.13 Co-authored-by: Andrew Bradley --- src/test/index.spec.ts | 33 +++++++++++++++++++ .../src/cjs-subdir/esm-exception/index.ts | 6 ++++ tests/module-types/src/cjs-subdir/index.ts | 3 ++ tests/module-types/test.esm.js | 7 ++++ tests/module-types/tsconfig.json | 3 +- 5 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 tests/module-types/src/cjs-subdir/esm-exception/index.ts create mode 100644 tests/module-types/test.esm.js diff --git a/src/test/index.spec.ts b/src/test/index.spec.ts index 5fda4567e..773dc61af 100644 --- a/src/test/index.spec.ts +++ b/src/test/index.spec.ts @@ -1888,5 +1888,38 @@ test.suite('ts-node', (test) => { expect(stdout).to.contain('CommonJS'); }); } + + async function execModuleTypeOverride() { + return exec(`${cmd} ./module-types/test.esm.js`, { + env: { + ...process.env, + TS_NODE_PROJECT: './module-types/tsconfig.json', + }, + }); + } + + // TODO: are 12.15 and 14.13 precisely the version ranges for which overrides cause errors? + if (semver.satisfies(process.version, '14.13.0')) { + test('[expected failure] moduleTypes can override module type to CJS in an ESM loader project', async () => { + const { err, stderr, stdout } = await execModuleTypeOverride(); + expect(err).to.not.equal(null); + expect(stderr).to.contain( + 'Error: Unexpected export statement in CJS module.' + ); + }); + } else if (semver.satisfies(process.version, '12.15.0')) { + test('[expected failure] moduleTypes can override module type to CJS in an ESM loader project', async () => { + const { err, stderr, stdout } = await execModuleTypeOverride(); + expect(err).to.not.equal(null); + expect(stderr).to.contain( + 'TypeError [ERR_INVALID_RETURN_PROPERTY_VALUE]: Expected string to be returned for the "format" from the "loader resolve" function but got type undefined.' + ); + }); + } else { + test('moduleTypes can override module type to CJS in an ESM loader project', async () => { + const { err, stderr, stdout } = await execModuleTypeOverride(); + expect(err).to.equal(null); + }); + } }); }); diff --git a/tests/module-types/src/cjs-subdir/esm-exception/index.ts b/tests/module-types/src/cjs-subdir/esm-exception/index.ts new file mode 100644 index 000000000..d90eb38f6 --- /dev/null +++ b/tests/module-types/src/cjs-subdir/esm-exception/index.ts @@ -0,0 +1,6 @@ +export const cjs: boolean = true; + +declare const require: any; +const requireType = typeof require; + +export default { requireType }; diff --git a/tests/module-types/src/cjs-subdir/index.ts b/tests/module-types/src/cjs-subdir/index.ts index a9a1650ec..8bb71bcd4 100644 --- a/tests/module-types/src/cjs-subdir/index.ts +++ b/tests/module-types/src/cjs-subdir/index.ts @@ -1 +1,4 @@ export const cjs: boolean = true; + +declare const require: any; +export const requireType = typeof require; diff --git a/tests/module-types/test.esm.js b/tests/module-types/test.esm.js new file mode 100644 index 000000000..df09a1f96 --- /dev/null +++ b/tests/module-types/test.esm.js @@ -0,0 +1,7 @@ +import assert from 'assert'; + +import cjsSubdirCJS from './src/cjs-subdir/index.ts'; +import cjsSubdirESM from './src/cjs-subdir/esm-exception/index.ts'; + +assert(cjsSubdirCJS.requireType === 'function'); +assert(cjsSubdirESM.requireType === 'undefined'); diff --git a/tests/module-types/tsconfig.json b/tests/module-types/tsconfig.json index e71261266..9655d689a 100644 --- a/tests/module-types/tsconfig.json +++ b/tests/module-types/tsconfig.json @@ -5,7 +5,8 @@ "webpack.config.ts": "cjs", // Test that subsequent patterns override earlier ones "src/cjs-subdir/**/*": "esm", - "src/cjs-subdir": "cjs" + "src/cjs-subdir": "cjs", + "src/cjs-subdir/esm-exception": "esm" } }, "compilerOptions": {