2017-10-08 65 views
9

昨天我升级我做出反应的项目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) 

而且我发现在官方website

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

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

一个解决方案,但我有一个问题:酶设置文件应该写在哪里?在每个测试文件前面?

我想上面的代码添加到测试文件之一,但仍有一个问题

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 

This是我的项目的

回答

21

我有一个类似的问题地址

如果您使用笑话来运行您的测试,您可以创建一个test-setup.js文件并添加酶文档中的代码段:

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

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

然后在您的笑话配置中添加一个setupTestFrameworkScriptFile键并指向该文件。例如,如果你的笑话配置是package.json

// package.json 
{ 
    ..., 
    "jest": { 
     "setupTestFrameworkScriptFile": "<rootDir>/test-setup.js" 
    } 
} 

从玩笑文档01​​https://facebook.github.io/jest/docs/en/configuration.html#setuptestframeworkscriptfile-string

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

这是你开玩笑环境初始化之后将执行,但你的酶测试被执行

3

之前对于使用创建反应的应用程序内,预期路径为您安装文件的人是src/setupTests.js。在Github上查看documentation(自述):

初始化测试环境

注:此功能适用于[email protected]高。 如果您的应用使用您需要在测试中模拟的浏览器API,或者如果您在运行测试之前只需要全局设置,则可以在项目中添加 src/setupTests.js。在运行测试之前,它将自动执行 。

(由于create-react-app不能处理,至少在v1.4.1中,package.json中的选项setupTestFrameworkScriptFile)。