子目录中的Mocha测试脚本在本地执行,但在CircleCI中被忽略

时间:2018-10-22 08:18:21

标签: node.js mocha circleci

在我看来,mocha的--recursive标志在我的机器上正常运行,但是在CI自动化测试中被忽略了。这意味着CircleCI中仅执行60个测试,但是当我在计算机上运行npm test时,将执行约100个测试。

部分项目结构

|--/test
|----mocha.opts
|----lifecycle.test.js
|----/integration
|------/models
|--------Thing.test.js
|------/controllers
|--------/thing
|----------thingAction.test.js
|--/.circleci
|----config.yml

mocha.opts

--recursive
--timeout 20000
--reporter list
--exit

.circleci / config.yml

# Javascript Node CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
version: 2
jobs:
  build:
    docker:
      # specify the version you desire here
      - image: circleci/node:8.11

      # Specify service dependencies here if necessary
      # CircleCI maintains a library of pre-built images
      # documented at https://circleci.com/docs/2.0/circleci-images/
      # - image: circleci/mongo:3.4.4

    working_directory: ~/repo

    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache:
          keys:
          - v1-dependencies-{{ checksum "package.json" }}
          # fallback to using the latest cache if no exact match is found
          - v1-dependencies-

      - run: yarn install

      - save_cache:
          paths:
            - node_modules
          key: v1-dependencies-{{ checksum "package.json" }}

      # run tests!
      - run: yarn test

package.json

"devDependencies": {
  "mocha": "^5.2.0",
},
"scripts": {
  "test": "npm run lint && npm run mocha-tests && echo 'Done.'",
  "lint": "eslint . --max-warnings=0 --report-unused-disable-directives && echo 'Your .js files look so good.' && lesshint assets/styles/ --max-warnings=0 && echo 'Your .less files look good, too.'",
  "mocha-tests": "node ./node_modules/mocha/bin/mocha test/lifecycle.test.js test/integration/**/*.test.js",
}

在此示例中,Thing.test.jsthingAction.test.js都在本地执行,但是在CircleCI构建期间仅执行Thing.test.js。换句话说,test/integration/**/*.test.js在本地匹配两个文件名,但仅匹配CircleCI的顶层子目录。

如何确保运行所有测试脚本?

1 个答案:

答案 0 :(得分:1)

可以通过在定义目标测试文件时在测试命令中添加单引号来解决该问题

"scripts": {
  "mocha-tests": "node ./node_modules/mocha/bin/mocha 'test/lifecycle.test.js' 'test/integration/**/*.test.js'",
}

如果不加引号,则glob模式将无法正常工作,尤其是在Linux环境中。