Answer the question
In order to leave comments, you need to log in
How to configure jest to recognize files on a next.js project?
The bottom line is this: I set up the baseUrl config in jsconfig.json to use absolute paths in the project and set up webpack in next.config.js to recognize image imports. When I first started Jest, he complained that he did not see absolute paths, so I solved the problem in this way (I used JsConfigPathsMapper ):
const JsConfigPathsMapper = require('jsconfig-paths-jest-mapper');
module.exports = {
verbose: true,
moduleNameMapper: new JsConfigPathsMapper({ configFileName: "jsconfig.json" })
};
{
"compilerOptions": {
"baseUrl": ".",
"lib": ["es5", "es6", "dom"],
"jsx": "react",
"strict": false,
"removeComments": true,
"paths": {
"src/*": ["src/*"],
"public/*": ["public/*"],
"public/(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": ["__mocks__/fileMock.js"]
}
}
}
module.exports = 'test-file-stub';
FAIL src/modules/place/PlaceCard/placeCard.test.js
● Test suite failed to run
/mnt/c/Users/715/Desktop/my projects/surgut.expert/public/img/heart1.png:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){�PNG
SyntaxError: Invalid or unexpected token
1 | import React from 'react';
2 | import styled from 'styled-components';
> 3 | import LikeSrc from 'public/img/heart1.png';
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question