jest: "Syntax Error: Invalid or unexpected token" with .png
I’m trying to test a simple module, but I’m getting this error:
Test suite failed to run
/home/matheusml/Projects/react-completo/src/assets/images/dribble-logo.png:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){�PNG
^
SyntaxError: Invalid or unexpected token
at transformAndBuildScript (node_modules/jest-runtime/build/transform.js:320:12)
at Object.<anonymous> (src/components/container/Container.js:4:46)
at Object.<anonymous> (src/components/App.js:4:44)
This is the test:
import React from 'react';
import renderer from 'react-test-renderer';
import { Router } from 'react-router';
import App from './App';
test('App', () => {
const component = renderer.create(
<App />
);
let tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
This is my package.json:
"jest": {
"moduleNameMapper": {
"\\.(jpg|jpeg|png|svg)$": "./src/config/fileMock.js",
"\\.(css|scss)$": "identity-obj-proxy"
}
}
And this is the fileMock.js:
module.exports = 'test-file-stub';
Thanks!
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 84
- Comments: 36 (5 by maintainers)
Commits related to this issue
- Fix for jest handling of static assets when running tests. See: https://github.com/facebook/jest/issues/2663#issuecomment-317109798 — committed to raythree/react-slingshot by raythree 7 years ago
- Fix for jest handling of static assets when running tests. See: (#457) https://github.com/facebook/jest/issues/2663#issuecomment-317109798 — committed to coryhouse/react-slingshot by raythree 7 years ago
- Implement React Router@4.0.0 (#393) * removed react-router form package.json & installed react-router-dom. * completed index.src refactor. * re-implemented App.js * added notes to import ch... — committed to coryhouse/react-slingshot by TobiahRex 7 years ago
- jestでテストする時にassetsの依存関係を良しなにする この設定が無いとこのようなエラーが起きる ``` console.error node_modules/vue/dist/vue.runtime.common.js:1715 /Users/fukudayukihiro/RubymineProjects/onepage/frontend/src/assets/home/user... — committed to yukihirop/onepage by yukihirop 6 years ago
- Fix for jest handling of static assets when running tests. See: (#457) https://github.com/facebook/jest/issues/2663#issuecomment-317109798 — committed to newsum2019/singleshot by newsum2019 7 years ago
- Implement React Router@4.0.0 (#393) * removed react-router form package.json & installed react-router-dom. * completed index.src refactor. * re-implemented App.js * added notes to import ch... — committed to newsum2019/singleshot by newsum2019 7 years ago
- fix: Jest에서 png 잘못 읽는 문제 해결 (#40) - https://github.com/facebook/jest/issues/2663#issuecomment-362261679 - 테스트코드 일부 추가 — committed to Yapp-17th/Web_2_Client by deleted user 4 years ago
- fix: Jest에서 png 잘못 읽는 문제 해결 (#40) - https://github.com/facebook/jest/issues/2663#issuecomment-362261679 - 테스트코드 일부 추가 — committed to Yapp-17th/Web_2_Client by deleted user 4 years ago
- Make react-navigation work with jest https://github.com/facebook/jest/issues/2663 — committed to RESKUE/reskueapp by DoctorJohn 3 years ago
I had the same error and resolved it by creating a assetsTransformer.js:
Then add this to your jest configuration in package.json:
"jest": { "moduleNameMapper": { "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/assetsTransformer.js", "\\.(css|less)$": "<rootDir>/assetsTransformer.js" } },
Source: https://facebook.github.io/jest/docs/webpack.html#content
For anyone looking into this issue @timgivois way works beautifully for me. You have to do install
npm install --save-dev identity-obj-proxy
to get the necessary dependencies.Check out http://facebook.github.io/jest/docs/tutorial-webpack.html#content
EDIT: new link is https://jestjs.io/docs/en/webpack
I also seem to be having this issue as well. It only happens when I require the png.
Another workaround:
I was having issues with a
svg
file. I realized I could mock it withidentity-obj-proxy
:I am dumb, i had another
jest.config.js
in my root folder which overwrote the settings inpackage.json
. Although it’s not runnning yet, i don’t get this particular error anymore.Same type error
Got it working. Here are the important bits (please note that i have some additional config due to enzyme 3 with react 16):
.babelrc
jest.config.js
jest.setup.js
Hope that helps someone.
@magnusart
it saves my day. Thanks!!.
I took a cue from this and installed
babel-jest
then I replaceidentity-obj-proxy
withbabel-jest
I was experiencing this on React Native as well, when attempting to jest test a file with React Navigation v5 Stack.Navigator and Stack.Screens in it.
This is on React Native 0.63.3 with the following installed in the project dependencies.
What fixed this for me was:
"jest-transform-stub": "2.0.0"
jest.config.js
file by adding this to themodule.exports
object:I believe in my case the moduleNameMapper was the only one really needed, but I’m keeping both for now as it works. Hopefully this may help another that runs into this.
Same error as well. Does someone fix it?
In my case, I didn’t realize that
<rootDir>
is a token supplied by Jest, and assumed it was something I needed to replace. Adding it to themoduleNameMapper
entries solved the issue.I solved this by using the
jest-react-native
package, as all solutions here did not work out for me. Maybe my problem is a different one, because I got problems with PNG files sitting inside thereact-native-router-flux
-package.Here some fragment of my
package.json
file:({“Object.<anonymous>”:function(module,exports,require,__dirname,__filename,global,jest){�PNG
None of the above answers has solved my problem
https://jestjs.io/docs/en/webpack
While the solution by @mbaranovski works, I don’t know if his intention was to use it as a transformer. It produces the following output (pay attention to the src attribute):
<img alt="ImgName" height="28" src={ Object { "process": [Function], } } width="112" />
Essentially, it is mapping everything from that module to the place where I required an SVG. Thus, you could get away with that module simply being:
module.exports = {}
The other work-around as stated above is to use identity-obj-proxy.
I had this png problem after adding
./node_modules/react-native-gesture-handler/jestSetup.js
tosetupFiles
(that was my attempt to resolve the jest issue withreact-native-gesture-handler
).I tried https://github.com/facebook/jest/issues/2663#issuecomment-341384494 (@magnusart using
identity-obj-proxy
), but it brokeenzyme
for whatever reason (it madeenzyme
’smock
into a symbol rather than a function). Then I tried https://github.com/facebook/jest/issues/2663#issuecomment-369040789 (@eddyerburgh usingjest-transform-stub
), but it didn’t do anything.Eventually, I tried https://github.com/facebook/jest/issues/2663#issuecomment-274270387 (@cpojer the
webpack
solution at the beginning), and that was the only one working for me.or use this https://github.com/eddyerburgh/jest-transform-stub
@magnusart
you save my day, it works fine
Errors like this may also be a result of using a preset with absolute paths set in module name mapper for example. This case however will be fixed in the next release