找不到 Jest 配置 setupFilesAfterEnv 选项

Ala*_*nIb 28 path jestjs

我正在尝试让 Jest 再次在 1 年前开发且未维护的项目上工作。

\n

setupFilesAfterEnv我的or路径有错误transform。

\n
    \n
  • 当我运行“纱线测试”时出现错误

    \n
    $ jest __testsv2__ --config=./jest.config.js\n\xe2\x97\x8f Validation Error:\n\nModule <rootDir>/jest/setup.js in the setupFilesAfterEnv option was not found.\n       <rootDir> is: /Users/alain/dev/ddf/release\n\nConfiguration Documentation:\nhttps://jestjs.io/docs/configuration.html\n\nerror Command failed with exit code 1.\n
    Run Code Online (Sandbox Code Playgroud)\n
  • \n
  • 我的文件系统,在 /Users/alain/dev/ddf/release/ 我有

    \n
      babel.config.js\n  jest.config.js\n  /jest\n    /setup\n    setup.js ( so  full path is : /Users/alain/dev/ddf/release/jest/setup.js ) \n    staticFileAssetTransform.js ( so full path is : /Users/alain/dev/ddf/release/jest/staticFileAssetTransform.js )     \n
    Run Code Online (Sandbox Code Playgroud)\n
  • \n
  • 我的package.json

    \n
    { ...\n "scripts": {\n      "test": "jest __testsv2__ --config=./jest.config.js"\n      ...\n  }\n}\n
    Run Code Online (Sandbox Code Playgroud)\n
  • \n
  • babel.config.js

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

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

Jas*_*dge 8

对于来到这里的人,请确保在路径前添加前缀<rootDir>

像这样:

setupFilesAfterEnv: ['<rootDir>/node_modules/@hirez_io/observer-spy/dist/setup-auto-unsubscribe.js']


小智 3

您可能想要删除节点模块、package-lock.json 并npm i再次运行,我遇到了类似的问题,这对我来说是一个修复。也尝试一下npm cache clean --force