Skip to content

Commit 23a73f0

Browse files
fix(test): support importing from ES modules in spec tests (#4136)
This adds support for importing from ES modules (w/ the `.mjs` extension) in spec tests by ensuring that 1. files with such an extension are passed to the jest preprocessor (which then runs them through typescript) 2. our typescript helper for string-to-strong transpilation will emit `.mjs` files instead of just dropping them on the floor Together these changes will allow running specs which import `.mjs` files. See #3251 for the original issue report
1 parent ebac6f8 commit 23a73f0

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

scripts/bundles/helpers/jest/jest-preset.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module.exports = {
3131
testPathIgnorePatterns: ['/.cache', '/.stencil', '/.vscode', '/dist', '/node_modules', '/www'],
3232
testRegex: '(/__tests__/.*|\\.?(test|spec))\\.' + moduleExtensionRegexp + '$',
3333
transform: {
34-
'^.+\\.(ts|tsx|jsx|css)$': path.join(testingDir, 'jest-preprocessor.js'),
34+
'^.+\\.(ts|tsx|jsx|css|mjs)$': path.join(testingDir, 'jest-preprocessor.js'),
3535
},
3636
watchPathIgnorePatterns: ['^.+\\.d\\.ts$'],
3737
};

src/compiler/transpile/transpile-module.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ export const transpileModule = (
8888
return normalizePath(fileName) === normalizePath(sourceFilePath) ? sourceFile : undefined;
8989
},
9090
writeFile: (name, text) => {
91-
if (name.endsWith('.js.map')) {
91+
if (name.endsWith('.js.map') || name.endsWith('.mjs.map')) {
9292
results.map = text;
93-
} else if (name.endsWith('.js')) {
93+
} else if (name.endsWith('.js') || name.endsWith('.mjs')) {
94+
// if the source file is an ES module w/ `.mjs` extension then
95+
// TypeScript will output a `.mjs` file
9496
results.code = text;
9597
}
9698
},

0 commit comments

Comments
 (0)