简体   繁体   中英

configure the jest moduleNameMapper not work

I want to apply a jest test to react component and I get an error that says:

Jest encountered an unexpected token

When I try to:

import moment from 'moment';
import './style.css';   //<----here

I try to mock css files and image objects and change the configuration:

// package.json
{
"jest": {
"moduleNameMapper": {
  "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
  "\\.(css|less)$": "<rootDir>/__mocks__/styleMock.js"
}
}
}

My styleMock.js is:

module.exports = {};

My fileMock.js is:

module.exports = 'test-file-stub';

But I still get the error:

Jest encountered an unexpected token

and the error is in the same place as it was previously.

My jest is the latest version "24.8.0".

Anyone knows why?

i use this lib for that identity-obj-proxy

npm install identity-obj-proxy

change this line:

"\\.(css|less)$": "<rootDir>/__mocks__/styleMock.js"

to this:

"\\.(css|less)$": "identity-obj-proxy"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM