You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SyntaxError: Cannot use import statement outside a module #2178
Reacted by glenveegee, Chiye Ma, Val Kotlarov Hoffman, Julio Ochoa, Frederico Estrela, Philipp Mudra, Hyyan Abo Fakher, Fernando Domingo Rojas Meléndez and Alen Subasic
From what I can gather, Stencil is compiling the Typescript component file, then passing that code to Jest. What seems to be going wrong is that dependencies of the component file are not transpiled, so any import statements in those will cause the test to bail.
In theory, if the spec tests were to use the full CJS output for a given component, it would work. In practice, I'm not sure what needs to change. It doesn't seem like something fixable just by changing the jest parameters... but maybe I'm missing something.
Reacted by Thomas Urban, Hyyan Abo Fakher and Denis Ignatova
It started happening in 1.17.0 (fine in 1.16.5) which points to the upgrade of [email protected] as the smoking gun. In my case, one of my stencil components imports the graphql package. Testing my stencil component fails. Looking a graphql contents, all its modules have been compiled to js but all the folders contain both a transformed .js and an untransformed .mjs. Jest fails when testing my stencil component because it tries to import the .mjs files which are untransformed. It breaks because these are not being transformed by jest.
If I rm -r node_modules/**/*.mjs then the jest passes so this suggests .mjs files are being imported in preference to .js
I haven't yet found a way to coerce jest to compile the .mjs files that it clearly prefers
I've found a fix that works for me although this only became an issue after upgrading to 1.17.x.
So if you're on 1.17.x and you're getting this issue when running jest test you should be able to work around the issue by adding the following moduleFileExtensions setting to your stencil config in the testing block as follows:
Just for those that may arrive at this page in the future. I was getting this error because somehow a whole bunch of compiled JS files got left in my source. So the tests were running on the TS file (working) and then on the generated JS file which threw this error.
Solution was to remove generated js files (something like rm src/components/*/*.js). Hope this may help some other poor soul!
A solution that worked for me was to add this in stencil.config.ts:
You use the jest-preprocessor as per the docs specify, and you exclude the modules that are exported as ESM from the ignore pattern so it does it on them as well (initially tried babel-jest and ts-jest but I had other issues from using those solutions).
// Can simply extend the list if we ever consume more esModules.constesModules=[// dependencies of esm packages].join('|');exportconstconfig: Config={//...transform: {'^.+\\.(ts|tsx|js|jsx|css)$': "@stencil/core/testing/jest-preprocessor"},transformIgnorePatterns: [`/node_modules/(?!${esModules})`],
Reacted by ravindra-dyte, Niklas Arndt, Tobias and Olivier
Activity
chrisdarroch commentedon Jul 3, 2020
I've been running in to this one, too.
Jest doesn't work with ES modules (see jestjs/jest#9292, jestjs/jest#9622, and jestjs/jest#8751), so at the moment, it will bail on any code it encounters with an
importstatement inside it.From what I can gather, Stencil is compiling the Typescript component file, then passing that code to Jest. What seems to be going wrong is that dependencies of the component file are not transpiled, so any
importstatements in those will cause the test to bail.In theory, if the spec tests were to use the full CJS output for a given component, it would work. In practice, I'm not sure what needs to change. It doesn't seem like something fixable just by changing the jest parameters... but maybe I'm missing something.
fix(compiler): fix transitive module dependencies in unit tests (sten…
fix(compiler): fix transitive module dependencies in unit tests (#2178)…
fix(test): transitive module dependencies with js extensions will be …
fix(test): transitive module dependencies with js extensions will be …
test(testing): add a test that confirms that non-ts files will be com…
test(testing): add a test that confirms that non-ts files will be com…
Shirinrpatel-zz commentedon Jul 15, 2020
I am facing same issue currently. Can you please suggest any workaround or fix for this? Thanks
raind33 commentedon Jul 22, 2020
Me too! I am seeing the same question
Kiliandeca commentedon Jul 30, 2020
Hi @shirinrpatel @raind33, maybe this can help you jestjs/jest#9395 (comment)
glenveegee commentedon Jul 31, 2020
I'm running into this issue too.
It started happening in 1.17.0 (fine in 1.16.5) which points to the upgrade of [email protected] as the smoking gun. In my case, one of my stencil components imports the
graphqlpackage. Testing my stencil component fails. Looking a graphql contents, all its modules have been compiled to js but all the folders contain both a transformed.jsand an untransformed.mjs. Jest fails when testing my stencil component because it tries to import the.mjsfiles which are untransformed. It breaks because these are not being transformed by jest.If I
rm -r node_modules/**/*.mjsthen the jest passes so this suggests.mjsfiles are being imported in preference to.jsI haven't yet found a way to coerce jest to compile the
.mjsfiles that it clearly prefersglenveegee commentedon Jul 31, 2020
I've found a fix that works for me although this only became an issue after upgrading to 1.17.x.
So if you're on 1.17.x and you're getting this issue when running jest test you should be able to work around the issue by adding the following
moduleFileExtensionssetting to your stencil config in the testing block as follows:chiyema commentedon Dec 4, 2020
Thanks @Kiliandeca. It was working for me by adding something like
chavlji commentedon Dec 23, 2020
We're facing the same problems. Any
importoutside component's *.ts file makes Jest tests failing.Any working solution or at least a workaround?
paulcpederson commentedon Feb 4, 2021
Just for those that may arrive at this page in the future. I was getting this error because somehow a whole bunch of compiled JS files got left in my source. So the tests were running on the TS file (working) and then on the generated JS file which threw this error.
Solution was to remove generated js files (something like
rm src/components/*/*.js). Hope this may help some other poor soul!mecanos28 commentedon Dec 27, 2021
Having the same issue. Have tried adding all the possible permutations of below code and it has not worked. (adding in stencil.config.ts)
CalinaCristian commentedon Oct 24, 2022
A solution that worked for me was to add this in
stencil.config.ts:You use the
jest-preprocessoras per the docs specify, and you exclude the modules that are exported as ESM from the ignore pattern so it does it on them as well (initially triedbabel-jestandts-jestbut I had other issues from using those solutions).christian-bromann commentedon Jun 26, 2024
Let's consolidate this effort to enable support for ESM in Stencil unit tests within #3230. Thanks for all your comments.