imaskjs: SyntaxError: Cannot use import statement outside a module.
Hello, when I try to add unit tests to my project, I’m having the following error in the console:
console.log: Jest: ● Test suite failed to run
[Info] C:\Code\Web\next-js\node_modules\imask\esm\index.js:1
[Info] ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import './_rollupPluginBabelHelpers-3c58f0e3.js';
[Info] ^^^^^^
[Info] SyntaxError: Cannot use import statement outside a module
[Info] at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
[Info] at node_modules/react-imask/dist/react-imask.js:2:83
[Info] at Object.<anonymous> (node_modules/react-imask/dist/react-imask.js:3:3)
[Info] at Object.<anonymous> (src/components/global-components/form-controls/MaskField.js:8:63)
[Info] at Object.<anonymous> (src/components/global-components/form-controls/index.js:25:62)
[Info] at Object.<anonymous> (src/components/component-library/examples/TextFieldExample.js:4:1)
[Info] at Object.<anonymous> (src/__tests__/components/component-library/examples/TextFieldExample.test.js:9:69)
I have tried several fixes in the web but I can’t make it work, have you had this issue before?
This is my package.json
{
"private": true,
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start -p $PORT",
"postinstall": "next build",
"test": "jest --maxWorkers=4 --colors",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage",
"test:CI": "jest --maxWorkers=4 --colors --coverage --coverageReporters=cobertura --json --outputFile=coverage/testResults.json --ci --reporters=default --reporters=jest-junit",
"export": "next export"
},
"dependencies": {
"@babel/core": "^7.8.6",
"@contentful/rich-text-react-renderer": "^13.4.0",
"@contentful/rich-text-types": "^14.0.1",
"@fortawesome/fontawesome-pro": "^5.11.2",
"@fortawesome/fontawesome-svg-core": "^1.2.25",
"@fortawesome/free-brands-svg-icons": "^5.11.2",
"@fortawesome/free-solid-svg-icons": "^5.11.2",
"@fortawesome/pro-light-svg-icons": "^5.11.2",
"@fortawesome/pro-regular-svg-icons": "^5.11.2",
"@fortawesome/pro-solid-svg-icons": "^5.11.2",
"@fortawesome/react-fontawesome": "^0.1.7",
"@microsoft/applicationinsights-web": "^2.3.1",
"@optimizely/react-sdk": "^1.0.1",
"@types/react": "^16.9.14",
"@zeit/next-css": "^1.0.1",
"axios": "^0.19.0",
"babel-plugin-prismjs": "^1.1.1",
"compression": "^1.7.4",
"contentful": "^7.10.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"google-map-react": "^1.1.5",
"isomorphic-unfetch": "^3.0.0",
"jest-junit": "^10.0.0",
"js-cookie": "^2.2.1",
"lodash": "^4.17.15",
"next": "^9.2.2",
"next-redux-wrapper": "^3.0.0",
"nookies": "^2.0.8",
"prismjs": "^1.17.1",
"prop-types": "^15.7.2",
"react": "^16.13.0",
"react-click-outside": "^3.0.1",
"react-dom": "^16.12.0",
"react-imask": "^6.0.3",
"react-loading-skeleton": "^1.3.0",
"react-redux": "^7.1.3",
"redux": "^4.0.4",
"redux-devtools-extension": "^2.13.8",
"redux-thunk": "^2.3.0"
},
"devDependencies": {
"babel-jest": "^25.1.0",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.2",
"enzyme-to-json": "^3.4.3",
"husky": "^4.2.3",
"istanbul-reports": "^3.0.0",
"jest": "^24.9.0",
"jest-fetch-mock": "^3.0.1",
"react-media": "^1.10.0",
"react-test-renderer": "^16.12.0",
"redux-mock-store": "^1.5.4"
}
}
An this my jest.config.js
file:
const esModules = ["react-imask", "imask"].join('|');
module.exports = {
moduleDirectories: ["node_modules", "src", "static", "store"],
modulePathIgnorePatterns: ["<rootDir>/node_modules/prismjs/plugins/line-numbers"],
transform: {
[`(${esModules}).+\\.js$`]: 'babel-jest'
},
transformIgnorePatterns: [`/node_modules/(?!${esModules})`],
testPathIgnorePatterns: [
"<rootDir>/src/components/component-library",
"<rootDir>/.next",
"jest.config.js",
"next.config.js"
],
collectCoverageFrom: [
"**/src/**",
"**/store/**",
"**/pages/**",
"!**/__tests__/**",
"!**/node_modules/**",
"!**/component-library/**"
],
testEnvironment: "node",
collectCoverage: true,
verbose: false,
automock: false,
setupFiles: ["./setupTests.js"],
moduleNameMapper: {
"functions/(.*)$": "<rootDir>/src/components/Functions/$1",
"scripts(.*)$": "<rootDir>/src/scripts/$1",
"^/(.*)$": "<rootDir>/src/$1",
"^store(.*)$": "<rootDir>/store/$1",
"\\.(css|less)$": "<rootDir>/__mocks__/styleMock.js"
},
coveragePathIgnorePatterns: ["/node_modules/"],
coverageThreshold: {
global: {
branches: 25,
functions: 20,
lines: 40,
statements: 40
}
}
};
An I can’t make it work, could you help me please?
Thank you!
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 3
- Comments: 16 (2 by maintainers)
If someone is still experiencing this error, the solution is quite simple.
Many npm modules don’t pre-compile their source code before publishing, and ES6 code needs to be compiled before it can be run by Jest.
Just add this configuration to the
jest.config.js
file:Sources: jest#9292 StackOverflow Jest Docs
For our project, we prefixed the jest command with
NODE_ENV=testing
and that got it working for us. We updated our test script to runNODE_ENV=testing jest --verbose ./source
and now we don’t get any errors from imask.@uNmAnNeR Hello. I recently had a similar issue using Jest. On a whim, I decided to install version
5.2.1
of your package and it actually resolved the issue. Whatever is causing the react component to break with Jest has just happened since5.2.1
.This error is not fixed, the imask react-plugin dont work at Nextjs (ssr).

For those using Create React App you can add this to your test script in
package.json
--transformIgnorePatterns \"node_modules/(?!(imask)/)/\""
I solved it with jest manual mocks (https://jestjs.io/docs/en/manual-mocks). This way you cant test any mask functionality though.
__mocks__
in rootvue-imask.js
Object.create(null)
in it