Mocha test scripts in sub-directories are executed locally, but ignored in CircleCI

Also posted on StackOverflow.

To me, it looks like mocha’s --recursive flag is working correctly on my machine, but being ignored during CI automated testing. This means that only 60 tests are executed in CircleCI, but ~100 are executed when I run npm test on my machine.

Partial Project Structure

|--/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",
}

In this example, both Thing.test.js and thingAction.test.js are being executed localy, but only Thing.test.js is executed during the CircleCI build. Put another way, test/integration/**/*.test.js matches both file names locally, but only the top subdirectory in CircleCI.

How do I make sure all the test scripts get run?

Turns out that my issue had nothing to do with CircleCI; only the fact that I’ve been developing on Windows and the automated tests are run in Linux. The file matching pattern in package.json’s mocha-tests script works fin in Windows, but it needs an extra set of quotes to work correctly in Linux. With an escaped double quote, everything is now working on both OSs.

"mocha-tests": "node ./node_modules/mocha/bin/mocha \"test/lifecycle.test.js\" \"test/integration/**/*.test.js\"",
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.