jest.config.js - `moduleNameMapper` 不适用于 CSS 配置

问题描述

我正在尝试测试 react js 应用程序并使用 jest 作为测试框架。 JEST 的基本设置是通过 jest.config.js 文件完成的,并且能够在 app.test.js 中运行简单的 expect 测试用例 一旦我包含我的主要 App 组件,我使用的样式 {./App.css} 就会给我错误。所以添加了 moduleNameMapper 并将配置设置为 CSS 和其他文件分别设置为 styleMock.js 和 fileMock.js。 以下是 jest.config.js 文件

    module.exports = async () => {
        return {
            verbose: true,bail: true,collectCoverage:true,rootDir: "./src/",coverageThreshold:{
              global:{
                branches:95,functions:100,lines:100
              }
           },setupFilesAfterEnv: [
               "./setupTests.js"
           ],moduleNameMapper: {
               "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "./test-config/__mocks__/fileMocks.js","^.+\\.(css|less)$/": "./test-config/__mocks__/styleMocks.js"
           },transformIgnorePatterns: [
             "/node_modules/(?!vtk).+\\.js$"
           ]
       };
     };

仍然出现如下错误; 失败 src/components/App.test.js ● 测试套件运行失败

Jest encountered an unexpected token

Jest Failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript Syntax,or when Jest is not configured to support such Syntax.

Out of the Box,Jest supports Babel,which will be used to transform your files into valid JS based on your Babel configuration.

By default "node_modules" folder is ignored by transformers.

Here's what you can do:
 • If you are trying to use ECMAScript modules,see https://jestjs.io/docs/ecmascript-modules for how to enable it.
 • To have some of your "node_modules" files transformed,you can specify a custom "transformIgnorePatterns" in your config.
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations,see:
https://jestjs.io/docs/code-transformation

Details:

SyntaxError: D:\OFFICE\EZEST-Strive\QuantX-App\src\components\App.css: Unexpected token (1:0)

> 1 | .App {
    | ^
  2 |       text-align: center;
  3 |       height    : 100%;
  4 | }

  at Parser._raise (../node_modules/@babel/parser/src/parser/error.js:97:45)
  at Parser.raiseWithData (../node_modules/@babel/parser/src/parser/error.js:92:17)
  at Parser.raise (../node_modules/@babel/parser/src/parser/error.js:41:17)
  at Parser.unexpected (../node_modules/@babel/parser/src/parser/util.js:161:16)
  at Parser.parseExprAtom (../node_modules/@babel/parser/src/parser/expression.js:1212:20)
  at Parser.parseExprSubscripts (../node_modules/@babel/parser/src/parser/expression.js:615:23)
  at Parser.parseUpdate (../node_modules/@babel/parser/src/parser/expression.js:595:21)
  at Parser.parseMaybeUnary (../node_modules/@babel/parser/src/parser/expression.js:562:23)
  at Parser.parseExprOps (../node_modules/@babel/parser/src/parser/expression.js:366:23)
  at Parser.parseMaybeConditional (../node_modules/@babel/parser/src/parser/expression.js:331:23)

Package.json 文件

     {
         "name": "spme-app","version": "1.0.0","description": "some discription","main": "src/index.js","dependencies": {
           "@material-ui/core": "^4.11.2","autoprefixer": "^10.2.5","axios": "^0.21.1","bootstrap": "^4.6.0","css-loader": "^5.0.1","d3": "^6.3.1","lodash": "^4.17.21","prop-types": "^15.7.2","react": "^17.0.1","react-bootstrap": "^1.4.3","react-csv": "^2.0.3","react-dom": "^17.0.1","react-ga": "^3.3.0","react-idle-timer": "^4.5.1","react-router-dom": "^5.2.0","style-loader": "^2.0.0"
         },"devDependencies": {
           "@babel/core": "^7.12.10","@babel/plugin-proposal-class-properties": "^7.12.13","@babel/plugin-transform-runtime": "^7.12.15","@babel/preset-env": "^7.12.11","@babel/preset-react": "^7.12.10","@types/jest": "^26.0.23","@types/react": "^17.0.0","@wojtekmaj/enzyme-adapter-react-17": "^0.6.1","babel-loader": "^8.2.2","enzyme": "^3.11.0","eslint": "^7.22.0","eslint-config-standard": "^16.0.2","eslint-plugin-eslint-plugin": "^2.3.0","eslint-plugin-import": "^2.22.1","eslint-plugin-jest": "^24.3.2","eslint-plugin-node": "^11.1.0","eslint-plugin-promise": "^4.3.1","eslint-plugin-react": "^7.23.1","file-loader": "^6.2.0","html-webpack-plugin": "^4.5.1","identity-obj-proxy": "^3.0.0","jest": "^27.0.1","jest-enzyme": "^7.1.2","react-redux": "^7.2.2","redux": "^4.0.5","redux-thunk": "^2.3.0","terser-webpack-plugin": "^5.1.2","webpack": "^5.11.1","webpack-cli": "^4.3.1","webpack-dev-server": "^3.11.1"
         },"scripts": {
            "start": "webpack serve --mode development","build:dev": "webpack --config webpack-dev","build:prod": "webpack --config webpack-prod","build": "webpack --mode production","app": "./electron-app-start.sh",lint": "eslint src/**/*.js","test": "jest --watchAll --config ./jest.config.js"
         },"license": "ISC","browser": {
          "fs": false
     }
    }

babel.config.js

    module.exports = {
     "presets": ["@babel/preset-env","@babel/preset-react"],"plugins": [
       "@babel/plugin-transform-runtime","@babel/plugin-proposal-class-properties"
     ]
    }

setupTest.js

    import { configure } from 'enzyme'
    import Adapter from '@wojtekmaj/enzyme-adapter-react-17'

    configure({ adapter: new Adapter() }) 

还尝试了我从网上得到的所有其他解决方案;

    moduleNameMapper: {
    "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "./test- 
         config/__mocks__/fileMock.js","^.+\\.(css|less|scss)$/":  require.resolve('./test-config/__mocks__/styleMock.js')
   },-----------------------------------------------------------------------------------------
   moduleNameMapper: {
        "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": 
           "./test-config/__mocks__/fileMock.js","^.+\\.(css|less|scss)$/": 'identity-obj-proxy'
    },-----------------------------------------------------------------------------------------
   moduleNameMapper: {
    "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "./test- 
           config/__mocks__/fileMocks.js","^.+\\.(css|less)$/": "<rootDir>/test-config/__mocks__/styleMocks.js"
   },-----------------------------------------------------------------------------------------
   moduleNameMapper: {
    "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "./test- 
        config/__mocks__/fileMocks.js","^.+\\.(css|less)$/": "./test-config/__mocks__/styleMocks.js"
  },

他们都不为我工作。任何人都可以帮忙吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)