transformIgnorePatterns doesn't work
See original GitHub issueš Bug Report
I have this exception
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
ā¢ To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
ā¢ If you need a custom transformation specify a "transform" option in your config.
ā¢ If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
C:\Code\SPFx\BCO\node_modules\@microsoft\sp-core-library\lib\index.js:11
export { default as _BrowserDetection } from './BrowserDetection';
^^^^^^
SyntaxError: Unexpected token export
19 | } from 'office-ui-fabric-react/lib/Utilities';
20 | import { IUserProvider } from "../UserProviders/IUserProvider";
> 21 | import {
| ^
22 | Environment,
23 | EnvironmentType
24 | } from '@microsoft/sp-core-library';
at ScriptTransformer._transformAndBuildScript (node_modules/jest/node_modules/jest-runtime/build/script_transformer.js:403:17)
at Object.<anonymous> (src/webparts/BCO/components/EmployeeSelector/EmployeeSelector.tsx:21:1)
at Object.<anonymous> (src/webparts/BCO/components/FieldMapping/FieldMapping.tsx:13:1)
And tried these transformIgnorePatterns expressions in config.json
"transformIgnorePatterns": [
"\\node_modules\\@microsoft\\sp-dialog",
"\\node_modules\\@microsoft\\sp-core-library",
"node_modules/(?!sp-core-library)",
"node_modules/(?!@microsoft/sp-core-library)"
],
and none of them worked. I run this on Windows 10 so I tried also this format https://jestjs.io/docs/en/cli.html#jest-regexfortestfiles
Run npx envinfo --preset jest
System: OS: Windows 10 CPU: x64 IntelĀ® Coreā¢ i5-4460S CPU @ 2.90GHz Binaries: npm: 5.10.0 - C:\Program Files\nodejs\npm.CMD
Issue Analytics
- State:
- Created 5 years ago
- Reactions:10
- Comments:8
Top Results From Across the Web
Jest transformIgnorePatterns not working - Stack Overflow
Looking at the linked github issue, it seems like it won't work in the package.json either, but I haven't tried it. ā NJCodeMonkey....
Read more >transformIgnorePatterns not working Ā· Issue #9814 - GitHub
hi could you please explain why this is not an issue? jest does not seem to be transpiling any of the packages under...
Read more >Getting jest to respect transformIgnorePatterns inside .bs.js file
I have been trying to get my tests to run with jest, ... It leads me to believe this transformIgnorePattern does not work...
Read more >[Solved]-Jest transformIgnorePatterns not working-Reactjs
As a workaround for now I have changed my config to use the moduleNameMapper option to load a mock class for that library...
Read more >Jest: Unexpected token 'export' - Sendbird Community
I tried to add transformIgnorePatterns and moduleNameMapper to my jest config but it doesn't work and I didn't find any topic/thread aboutĀ ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@aslanovsergey sorry about the delay, finally found some time to dig a bit into your repo.
You have at least 2 issues in configuration: You do not pass
js
files tots-jest
, and you havenāt told tsc to compile JS files. The following diff fixes it (you could also usebabel-jest
to compile JS if you want):However, the file is still not transpiled because of how Jest uses the ignore pattern. See the following snippet: https://github.com/facebook/jest/blob/v23.6.0/packages/jest-runtime/src/script_transformer.js#L484-L502, specifically
new RegExp(config.transformIgnorePatterns.join('|'))
.However, the pattern works correctly if you combine it into a single one:
"node_modules/(?!(@microsoft/sp-dialog|@microsoft/office-ui-fabric-react-bundle))"
.Running that correctly transpiles your code, but you get a runtime error when executing:
Canāt help you with that one.
(The stack trace is messed up, not sure whyā¦)
Hi @SimenB, I uploaded a demo. please look at https://github.com/aslanovsergey/Jest-Demo-transformIgnorePatterns