When Mocha doesn't run tests in subfolders on either windows or your CI server

Published on January 29, 2019

I use a Windows PC for development and I had a problem where some of my mocha tests were not running on my CI server. The issue was that I use GitLab’s CI system which uses a Linux docker image to run the tests so the difference was the OS. This would also affect teams with developers on both Windows machines and Macs. Here’s how I fixed it…

My original test script looked like this

  "scripts": {
    "test": "sequelize db:migrate:undo:all && sequelize db:migrate && nyc mocha --opts ./.mocharc src/**/*.spec.ts"

I tear down the database, rebuild all the tables. Then I run coverage and mocha on all my test files.

My mocha options file is set to use --recursive so Mocha should look in all subfolders right?

--require ts-node/register
--slow 20

Instead mocha just runs the tests in the top level of the /src folder

When I ran this on my Windows machine I had 50 tests running as expected. When I ran it on GitLab I only had 6 tests running!

The issue here is that the given path gets parsed BEFORE it’s passed mocha and the /**/ part of the path is removed so on Posix systems Mocha receives src/*.spec.ts instead of src/**/*.spec.ts.

The way to fix this for all platforms is to wrap the path in double quotes. It’s really important that you use double quotes and not single quotes to be cross platform. And to use double quotes you have to escape them \"src/**/*.spec.ts\". So we end up with this…

  "scripts": {
    "test": "sequelize db:migrate:undo:all && sequelize db:migrate && nyc mocha --opts ./.mocharc \"src/**/*.spec.ts\""

and it all works on GitLab - 54 tests passing!…

GitLab output
GitLab output

You can see this in action on my starter source code @ https://gitlab.com/darragh.oriordan/starter/tree/master

Darragh ORiordan

Hi! I'm Darragh ORiordan.

I live and work in Sydney, Australia building and supporting happy teams that create high quality software for the web.

I also make tools for busy developers! Do you have a new M1 Mac to setup? Have you ever spent a week getting your dev environment just right?

My Universal DevShell tooling will save you 30+ hours of configuring your Windows or Mac dev environment with all the best, modern shell and dev tools.

Get DevShell here: ✨ https://usemiller.dev/dev-shell

Read more articles like this one...

List of article summaries


Extract user profile attributes from an Azure ADB2C tenant using the Microsoft Graph API

I had to retrieve a list of users from an Azure Active Directory B2C instance today. I thought I could just go through the Azure UI but that’s limited to short pages of data and limited attributes.

There is a CSV export provided on the UI but you won’t get the required identity objects in the csv output if you need a user’s signin email address.

I had to use the Microsoft Graph Api to get what I needed. This is a bit hacky but it does the trick!


Force restart your Azure App service site and host

Sometimes your Azure App service host will need to be restarted. You can do this but it’s hidden away in the Azure resource manager site. Here’s how to find it!


Scheduling a feature toggle using no-code with Azure Logic Apps

I use launch darkly to toggle features on an app. There is one third-party dependency that has regular scheduled maintenance and I need to toggle the feature on and off on schedule.

Launch Darkly has built in scheduling to handle this scenario but you have to be on the enterprise plan to use it. The enterprise plan is too expensive to upgrade to for scheduling alone so I needed to find a different way to automate this.


Avoid rebuild of React App in every CI stage

If you have a react app you can use env vars like REACT_APP_MY_ENV_VAR in your application and React will automatically pull them in to your app when you build the production application.

This is very useful but if you have variables that change for each environment and your application build takes a long time, you might want to avoid building unnecessarily in CI. For example you might have a QA environment and a Staging environment that have different configuration.

We type-check our code on each build and that was taking 5 minutes+ to build each environment so we had to make it faster. We changed our app from using REACT_APP env vars to using a configuration file that we could quickly write to using CI.

Our CI system is Azure DevOops so the CI scripts here are specifically for Azure DevOps but they apply to most CI systems with small changes.

The real work happens in a Node.js script that would work anywhere.