SyntaxError with Jest and React and importing CSS files

后端 未结 7 1135
-上瘾入骨i
-上瘾入骨i 2020-12-02 17:01

I am trying to get my first Jest Test to pass with React and Babel.

I am getting the following error:

SyntaxError: /Users/manueldupont/test/av

相关标签:
7条回答
  • 2020-12-02 17:26

    If you're using ts-jest, none of the solutions above will work! You'll need to mock transform.

    jest.config.js

    module.exports = {
      preset: 'ts-jest',
      testEnvironment: 'jsdom',
      roots: [
        "<rootDir>/src"
      ],
      transform: {
        ".(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/jest-config/file-mock.js",
        '.(css|less)$': '<rootDir>/jest-config/style-mock.js'
      },
    };
    

    file-mock.js

    module.exports = {
      process() {
        return `module.exports = 'test-file-stub'`;
      },
    };
    

    style-mock.js

    module.exports = {
        process() {
          return 'module.exports = {};';
        }
      };
    

    I found this working example

    0 讨论(0)
提交回复
热议问题