【问题标题】:Jest Tests Error - Cannot find module 'expect' from 'jest_expect.js'Jest 测试错误 - 无法从“jest_expect.js”中找到模块“expect”
【发布时间】:2018-08-21 17:09:19
【问题描述】:

我按照Jest configuration 为 Webpack 构建的 React 应用程序遵循了指南,并解决了我之前遇到的很多错误。然而,这个下面的错误却让人困惑。错误:

Cannot find module 'expect' from 'jest_expect.js'      
      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:194:17)

这是我的 package.json

   "devDependencies / dependencies" :
    "babel-core": "^6.26.0",
    "babel-eslint": "^8.2.1",
    "babel-jest": "^22.2.2",
    "babel-loader": "^7.1.2",
    "babel-polyfill": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "jest": "^22.3.0",
    "webpack": "^3.11.0",
    "webpack-dev-server": "^2.11.1"
    ...
    "identity-obj-proxy": "^3.0.0",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-redux": "^5.0.6",
    "redux": "^3.7.2",
    "redux-form": "^7.3.0",
    "redux-thunk": "^2.2.0",
    "webpack-merge": "^4.1.1"
     },
  "jest": {
    "verbose": true,
    "moduleDirectories": ["mode_modules"],
    "moduleFileExtensions": ["js", "jsx"],
    "moduleNameMapper": {
      "app(.*)$": "<rootDir>/apps$1",
      "appAthenaTrader(.*)$": "<rootDir>/apps/athenaTrader$1",
      "common(.*)$": "<rootDir>/src/modules/Common$1",
      "modules(.*)$": "<rootDir>/src/modules$1",
      "src(.*)$": "<rootDir>/src$1"
    },
    "modulePaths": ["src", "apps"]
  }

我的 webpack.config.js :

const PATHS = {
  app: path.join(__dirname, 'apps'),
  src: path.join(__dirname, 'src'),
  modules: path.join(__dirname, 'src/modules'),
  common: path.join(__dirname, 'src/modules/Common'),
  styles: path.join(__dirname, 'src/styles')
};

const common = {
  output: {
    path: PATHS.build,
    publicPath: '/',
    filename: 'bundle.js'
  },
  resolve: {
    alias: {
      app: PATHS.app,
      Common: PATHS.common,
      modules: PATHS.modules,
      src: PATHS.src,
      styles: PATHS.styles
    },
    extensions: ['.js', '.jsx', '.json']
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        include: [PATHS.app, PATHS.src],
        use: [
          {
            loader: 'babel-loader',
            options: {
              cacheDirectory: true
            }
          }
        ]
      },
...

我写的测试是:

import { Map as iMap } from 'immutable';
import * as actions from '../actionTypes';
import * as actionTypes from '../actions';

describe('Testing of User actions...', () => {
  it('should create an action for sendingUserAuthRequest', () => {
    const action = {
      type: actionTypes.SEND_AUTH_REQUEST,
      payload: iMap({
        isFetching: true,
        isAuthenticated: false
      })
    };
    expect(actions.sendUserAuthRequest()).toEqual(action);
  });
});

我阅读并尝试过的一些解决方案包括this link to regex moduleNameMapper

重申一下,每当我运行测试时,我都会得到 ​​p>

Cannot find module 'expect' from 'jest_expect.js'

【问题讨论】:

    标签: javascript testing webpack jestjs webpack-dev-server


    【解决方案1】:

    很可能是“mode”上的“m”错字,应该是“node”:

    "moduleDirectories": ["mode_modules"],
    

    【讨论】:

    • 谢谢,正则表达式是问题所在。
    猜你喜欢
    • 2020-05-18
    • 1970-01-01
    • 2016-09-15
    • 1970-01-01
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多