首页 > 解决方案 > 使用 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"
  }
}

我究竟做错了什么?非常感谢帮助!

标签: reactjsherokutravis-cienzyme

解决方案


问题似乎来自于我在同一个 repo 上使用各种 packages.json 拥有客户端和 API。我为客户端和 API 分配了不同的存储库,我不再遇到这个问题了。


推荐阅读