R
R
Ruslan Shaficof2021-04-01 14:26:18
JavaScript
Ruslan Shaficof, 2021-04-01 14:26:18

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" })
};


here's what's in 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"]
    }
  }
}

Here's what's in /fileMock.js : All absolute paths were found, but now the problem is that it doesn't recognize images when I run jest:
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';


Please tell me what to do, I have already tried a lot of things, it remains to write here
There is little information on Google, I found this problem here: https://github.com/facebook/jest/issues/2663 , did the same, but still did not help, maybe he did something wrong, knowledgeable people, help me out pliz!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question