Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test coverage: moduleType overrides during ts-node loader usage #1376

Merged
merged 15 commits into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/test/index.spec.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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