Skip to content

Commit

Permalink
fix(test): support importing from ES modules in spec tests (#4136)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alicewriteswrongs committed Mar 13, 2023
1 parent ebac6f8 commit 23a73f0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion scripts/bundles/helpers/jest/jest-preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = {
testPathIgnorePatterns: ['/.cache', '/.stencil', '/.vscode', '/dist', '/node_modules', '/www'],
testRegex: '(/__tests__/.*|\\.?(test|spec))\\.' + moduleExtensionRegexp + '$',
transform: {
'^.+\\.(ts|tsx|jsx|css)$': path.join(testingDir, 'jest-preprocessor.js'),
'^.+\\.(ts|tsx|jsx|css|mjs)$': path.join(testingDir, 'jest-preprocessor.js'),
},
watchPathIgnorePatterns: ['^.+\\.d\\.ts$'],
};
6 changes: 4 additions & 2 deletions src/compiler/transpile/transpile-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ export const transpileModule = (
return normalizePath(fileName) === normalizePath(sourceFilePath) ? sourceFile : undefined;
},
writeFile: (name, text) => {
if (name.endsWith('.js.map')) {
if (name.endsWith('.js.map') || name.endsWith('.mjs.map')) {
results.map = text;
} else if (name.endsWith('.js')) {
} else if (name.endsWith('.js') || name.endsWith('.mjs')) {
// if the source file is an ES module w/ `.mjs` extension then
// TypeScript will output a `.mjs` file
results.code = text;
}
},
Expand Down

0 comments on commit 23a73f0

Please sign in to comment.