Skip to content

Commit

Permalink
Test coverage: moduleType overrides during ts-node loader usage (#1376)
Browse files Browse the repository at this point in the history
* 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 c64cf92.

* Revert "Add 'experimental-specifier-resolution' flag to NPM options in ESM module override test"

This reverts commit 1093df8.

* 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 <cspotcode@gmail.com>
  • Loading branch information
jayaddison and cspotcode committed Jul 9, 2021
1 parent dcbd916 commit e0f460e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
33 changes: 33 additions & 0 deletions src/test/index.spec.ts
Expand Up @@ -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);
});
}
});
});
6 changes: 6 additions & 0 deletions 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 };
3 changes: 3 additions & 0 deletions 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;
7 changes: 7 additions & 0 deletions 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');
3 changes: 2 additions & 1 deletion tests/module-types/tsconfig.json
Expand Up @@ -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": {
Expand Down

0 comments on commit e0f460e

Please sign in to comment.