reactjs - 使用 Travis CI 时酶找不到 React 应用程序的 setupTests 文件
问题描述
我正在尝试使用 Travis for CI 在 Heroku 上部署一个 React 应用程序(在后端使用 express)。所有测试都在本地通过,但我遇到了 Enzyme 的问题。对于每个经过测试的组件,我都会在 Travis 上收到以下消息:
FAILsrc/tests/HomePage.test.js
●测试套件无法运行
在 Resolver.resolveModule (node_modules/react-scripts/node_modules/jest-> resolve/build/index.js:179:17) 中找不到模块 '../../src/setupTests'在对象。(src/tests/HomePage.test.js:3:1)
看起来我的 setupTests.js 文件的路径是错误的,但它如何在本地工作?
如果我不将 setupTests 文件导入到我测试的每个组件,我会收到以下错误消息:
Enzyme 内部错误:Enzyme 需要配置适配器,但 >> 没有找到。要配置适配器,您应该
Enzyme.configure({ adapter: new Adapter() })
在使用任何 Enzyme 的顶级 API 之前调用,其中Adapter
与当前正在测试的库对应的适配器在哪里。例如:import Adapter from 'enzyme-adapter-react-15';
这就是我配置 Travis 的方式:
language: node_js
node_js: node
services:
- mongodb
cache:
directories:
- node_modules
env:
- CI=true
script:
- cd server && npm install && npm test
- cd ../client && npm install && npm run build && npm test
deploy:
provider: heroku
api_key:
secure: MY-API-KEY
app: MY-APP-NAME
on:
repo: MY-REPO
我的 setupTests.js
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import { configure } from 'enzyme'
configure({adapter: new Adapter()});
我的 package.json:
{
"name": "central",
"version": "0.1.0",
"private": true,
"dependencies": {
"enzyme-adapter-react-16": "^1.3.1",
"jwt-decode": "^2.2.0",
"material-ui": "^0.20.2",
"moment": "^2.22.2",
"prop-types": "^15.6.2",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"react-redux": "^5.0.7",
"react-router-dom": "^4.3.1",
"react-stripe-checkout": "^2.6.3",
"redux": "^4.0.0",
"redux-form": "^7.4.2",
"redux-thunk": "^2.3.0",
"socket.io-client": "^2.1.1",
"validator": "^10.7.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"proxy": "http://localhost:8080",
"devDependencies": {
"enzyme": "^3.5.0",
"mock-local-storage": "^1.0.5",
"react-scripts": "^1.1.4",
"react-test-renderer": "^16.4.2"
}
}
我究竟做错了什么?非常感谢帮助!
解决方案
问题似乎来自于我在同一个 repo 上使用各种 packages.json 拥有客户端和 API。我为客户端和 API 分配了不同的存储库,我不再遇到这个问题了。
推荐阅读
- qr-code - 为什么具有相同掩码、版本和纠错的二维码具有不同的点分布?
- prolog - 目标使用多少内存?
- sas - SAS:稳健的回归和输出系数、t 值和 adj R 平方
- elasticsearch - 模板映射不起作用
- sql - 在 SQL Server 的 business_table 表中找到第一个匹配的父级并使用 parent_id 更新它
- ruby - 具有连续方法调用的 ruby 动态调度
- javascript - 在 React.js 中实现输入标签的自动保存
- regex - 在列表中搜索具有特殊字符的模式
- php - 将 PHP 生成的 HTML 内容保存为 PDF 文件
- php - 为什么在角色和权限中使用刀片指令?在这里向我解释下面的例子