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

Mocha test scripts in sub-directories are executed locally, but ignored in 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.

中的顶级子目录

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

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

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

没有引号,glob 模式将无法正常工作,尤其是在 linux 环境中。