【问题标题】:Jest configuration setupFilesAfterEnv option was not found未找到 Jest 配置 setupFilesAfterEnv 选项
【发布时间】:2021-03-14 08:08:53
【问题描述】:

我正在尝试让 Jest 再次用于 1 年前开发但未维护的项目。

setupFilesAfterEnvtransform的路径有错误。

  • 我在运行“yarn test”时遇到的错误

    $ jest __testsv2__ --config=./jest.config.js
    ● Validation Error:
    
    Module <rootDir>/jest/setup.js in the setupFilesAfterEnv option was not found.
           <rootDir> is: /Users/alain/dev/ddf/release
    
    Configuration Documentation:
    https://jestjs.io/docs/configuration.html
    
    error Command failed with exit code 1.
    
  • 我的文件系统,在 /Users/alain/dev/ddf/release/ 我有

      babel.config.js
      jest.config.js
      /jest
        /setup
        setup.js ( so  full path is : /Users/alain/dev/ddf/release/jest/setup.js ) 
        staticFileAssetTransform.js ( so full path is : /Users/alain/dev/ddf/release/jest/staticFileAssetTransform.js )     
    
  • 我的 package.json

    { ...
     "scripts": {
          "test": "jest __testsv2__ --config=./jest.config.js"
          ...
      }
    }
    
  • babel.config.js

    module.exports = function(api) {
       api.cache(false);
       const presets = ['@babel/preset-env', '@babel/preset-react'];    
       const plugins = [['@babel/proposal-object-rest-spread'],];
    
       return {
         presets,    plugins,   sourceMaps: "inline",
         ignore: [(process.env.NODE_ENV !== 'test' ?  "**/*.test.js" : null)    ].filter(n => n)
       };
     };
    
  • jest.config.js

     module.exports = {
      resolver: 'browser-resolve',
      clearMocks: true,
      moduleNameMapper: {    '\\.(css|less|styl|md)$': 'identity-obj-proxy'    },
      // A list of paths to modules that run some code to configure or set up the testing framework before each test
      // setupFilesAfterEnv: ['./jest/setup.js'], // don't work too
      setupFilesAfterEnv: ['<rootDir>/jest/setup.js'], 
    
    
      // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
      testPathIgnorePatterns: ['/node_modules/', '/__gql_mocks__/'],
    
      // A map from regular expressions to paths to transformers
      transform: {
        '^.+\\.js$': './jest/babelRootModeUpwardTransform.js',
        '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': '<rootDir>/jest/staticFileAssetTransform.js',
      },
    };
    

【问题讨论】:

  • 尝试 cat /Users/alain/dev/ddf/release/jest/setup.js 检查文件是否真的存在

标签: path jestjs


【解决方案1】:

Module &lt;rootDir&gt;/jest.setup.js in the setupFilesAfterEnv option was not found. 错误很明显。

为什么会出现此错误是因为在您的项目的root directory 中找不到jest.setup.js

修复

jest.config.js 中删除setupFilesAfterEnv

或者:

在您的项目的root directory 中创建一个jest.setup.js,并在文件中的每个测试之前设置一个测试框架。

import '@testing-library/jest-dom/extend-expect'jest.setup.js 文件中。

.

【讨论】:

    猜你喜欢
    • 2019-12-17
    • 2019-09-09
    • 2018-11-08
    • 2022-09-27
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    相关资源
    最近更新 更多