首页 > 解决方案 > TravisCI 与来自创建反应应用程序的 npm 运行测试

问题描述

我正在尝试使用TravisCIcreate react app 生成的项目执行自动化测试。

有人告诉我npm run test -- --coverage应该在运行时停止watch mode或提示出现npm run test。但是我仍然看到提示。

Watch Usage
 › Press f to run only failed tests.
 › Press o to only run tests related to changed files.
 › Press q to quit watch mode.
 › Press p to filter by a filename regex pattern.
 › Press t to filter by a test name regex pattern.
 › Press Enter to trigger a test run.

更新:

项目结构

/redribbon
  .travis.yml
  docker-compose.yml
  /client
    package.json
    Dockerfile
    Dockerfile.dev
  /server

我了解到默认情况下 TravisCI 会CI=true自动设置。目前,我npm run test的in[package.json][1]还没有被触及。

.travis.yml

sudo: required    
services:
  - docker
before_install:
  - docker build -t bradford/redribbon-client -f ./client/Dockerfile.dev ./client
script:
  - docker run bradford/redribbon-client npm run test

TravisCI 输出可以在这里看到

如果没有 TravisCI,当我运行命令npm run test或者npm test这是我的输出时:

No tests found related to files changed since last commit.
Press `a` to run all tests, or run Jest with `--watchAll`.

Watch Usage
 › Press a to run all tests.
 › Press f to run only failed tests.
 › Press q to quit watch mode.
 › Press p to filter by a filename regex pattern.
 › Press t to filter by a test name regex pattern.
 › Press Enter to trigger a test run.

如果我添加CI=true-->"test": "CI=true react-scripts test",

这是我的输出:

$ npm test

> client@0.1.0 test /Users/bli1/Development/projects/sideprojects/redribbon/client
> CI=true react-scripts test

No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In /Users/bli1/Development/projects/sideprojects/redribbon/client
  14 files checked.
  testMatch: /Users/bli1/Development/projects/sideprojects/redribbon/client/src/**/__tests__/**/*.{js,jsx,ts,tsx}, /Users/bli1/Development/projects/sideprojects/redribbon/client/src/**/*.{spec,test}.{js,jsx,ts,tsx} - 0 matches
  testPathIgnorePatterns: /node_modules/ - 14 matches
  testRegex:  - 0 matches
Pattern:  - 0 matches
npm ERR! Test failed.  See above for more details.

标签: javascriptreactjstravis-cicreate-react-app

解决方案


在你的 travis.yml 中试试这个,

sudo: required    
services:
  - docker
before_install:
  - docker build -t bradford/redribbon-client -f ./client/Dockerfile.dev ./client
script:
  - docker run -e CI=true bradford/redribbon-client npm run test

这将允许您在测试完成后运行测试并退出映像


推荐阅读