Reactjs 酶设置文件应该写入哪里?

Reactjs 酶设置文件应该写入哪里?,reactjs,enzyme,Reactjs,Enzyme,昨天我升级了我的React项目到v16.0,但我发现酶有一些问题 Error: Enzyme Internal Error: Enzyme expects an adapter to be configured, but found none. To configure an adapter, you should call `Enzyme.configure({ adapter: new Adapter() })` before using any

昨天我升级了我的React项目到v16.0,但我发现酶有一些问题

    Error: 
      Enzyme Internal Error: Enzyme expects an adapter to be configured, but found none. To
      configure an adapter, you should call `Enzyme.configure({ adapter: new Adapter() })`
      before using any of Enzyme's top level APIs, where `Adapter` is the adapter
      corresponding to the library currently being tested. For example:
      import Adapter from 'enzyme-adapter-react-15';
      To find out more about this, see http://airbnb.io/enzyme/docs/installation/index.html

    at validateAdapter (spec/components/page_components/SongListItem/index.spec.js:9:1141986)
    at getAdapter (spec/components/page_components/SongListItem/index.spec.js:9:323041)
    at new ReactWrapper (spec/components/page_components/SongListItem/index.spec.js:9:622193)
    at mount (spec/components/page_components/SongListItem/index.spec.js:9:2025476)
    at UserContext.<anonymous> (spec/components/page_components/SongListItem/index.spec.js:9:1235741)
但是我有一个问题:酶设置文件应该写在哪里?在每个测试文件前面

我试图将上述代码添加到一个测试文件中,但仍然存在问题

 Internal error: attempt to prepend statements in disallowed (non-array) context at C:/Users/killer/workspace/react/NetEase-Cloud-Music-Web/spec/components/page_components/SongListItem/index.spec.js
是我的项目地址吗?我也有类似的问题

如果使用jest运行测试,可以创建
test setup.js
文件,并从以下文档中添加代码段:

// test-setup.js
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

configure({ adapter: new Adapter() });
然后在jest配置中添加一个
setupTestFrameworkScriptFile
键并指向该文件。例如,如果jest配置位于
package.json

// package.json
{
    ...,
    "jest": {
        "setupTestFrameworkScriptFile": "<rootDir>/test-setup.js"
    }
}
//package.json
{
...,
“笑话”:{
“SetupTestFrameworkScript文件”:“/test setup.js”
}
}
从笑话文件 :

模块的路径,该模块在每次测试之前运行一些代码来配置或设置测试框架。由于setupFiles在环境中安装测试框架之前执行,因此此脚本文件为您提供了在环境中安装测试框架后立即运行某些代码的机会

这将在初始化jest环境之后执行,但在执行酶测试之前执行

适用于使用create react应用程序的用户
您需要运行
纱线弹出
npm运行弹出
,然后您将在
包中看到jest配置。json


此外,
setupTestFrameworkScriptFile
目前被弃用,取而代之的是
setupfileafterenv

对于使用create react app的用户,安装文件的预期路径是
src/setupTests.js
。请参阅GitHub上的(自述):

初始化测试环境

注意:此功能可用于react-scripts@0.4.0更高。 如果您的应用程序使用了您需要在测试中模拟的浏览器API,或 如果在运行测试之前只需要全局设置,请添加 将src/setupTests.js添加到项目中。它将自动执行 在运行测试之前


(至少在v1.4.1中,create-react应用程序不处理package.json中的选项
setupTestFrameworkScriptFile

更新2019年6月

无论谁使用CRA(创建react应用程序),
src/setupTests.js
都不会工作!在项目根文件夹中创建
jest.config.js
文件并粘贴以下内容

module.exports = {
    "moduleNameMapper": {
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
      "\\.(scss|sass|css)$": "identity-obj-proxy"
    },
    "setupFilesAfterEnv": ["<rootDir>/src/setupTests.js"]
}

此设置适用于react 16

我遇到了与react v16类似的问题抛出酶内部错误。您必须将以下代码放入src/setupTests.js

import Enzyme from "enzyme";
import EnzymeAdapter from "enzyme-adapter-react-16";

Enzyme.configure({
  adapter: new EnzymeAdapter(),
});
package.json文件中配置jest。添加以下行:

  "jest": {
    ...
    "setupFilesAfterEnv": [
      "<rootDir>/src/setupTests.js"
    ],
    ...
  },
“开玩笑”:{
...
“SetupFileAfterEnv”:[
“/src/setupTests.js”
],
...
},

无需向package.json添加配置。只需在“src”文件夹下添加一个“setupTests.js”文件并添加行

import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

configure({ adapter: new Adapter() });
如果您试图添加配置,jest将抛出类似这样的错误

我们在您的package.json中检测到setupfileafterenv

将其从Jest配置中删除,并将初始化代码放入 src/setupTests.js。此文件将自动加载


我尝试了这个,但仍然得到相同的适配器错误,在这个问题上已经3个小时了。有人有其他方法吗?现代的CreateReact应用程序版本不需要弹出。默认情况下,它将执行
src/setupTests
。如果没有,您仍然可以将其作为CLI参数传递给
包.json的
测试脚本
反应脚本测试--setupfileafterenv='./src/setupTests.ts'
附议MrKey的注释--有关详细信息,请参阅@VinceOPS。此安装后要运行的任何命令??即使在您要求进行设置之后,我也会遇到相同的错误。很好;)除了
npm run test
之外,没有其他命令可以为我运行,这可能有助于安装最新的react版本,并检查我是否在您的package.json的“jest”条目中保留了不受支持的密钥,最后my.eslintrc也引用了jest,以避免出现无法识别的“it”问题:“env”:{“browser”:true,“jest”:true},
  "jest": {
    ...
    "setupFilesAfterEnv": [
      "<rootDir>/src/setupTests.js"
    ],
    ...
  },
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

configure({ adapter: new Adapter() });