ts-node: mocha --watch not working with ts-node

I am using Typescript and node-ts for the mocha unit testing, but the mocha --watch option is not working with ts-node

Below is the command i pass to run the unit test

mocha -r ts-node/register test/**/**/*.test.ts --watch -R min

The version I am currently using

"mocha": "^3.2.0",
"ts-node": "^1.7.3",
"typescript": "^2.1.5"

It works on the first time, but when I change the file it doesnt run again?

How to fix this problem?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 25
  • Comments: 32 (3 by maintainers)

Commits related to this issue

Most upvoted comments

I had this issue too, but just fixed it by adding --watch-extensions ts not sure if it’s the same problem as #113

Anybody searching for a solution, try this:

"scripts": {
    "test": "mocha --require ts-node/register tests/**/*.ts",
    "test:watch": "mocha --require ts-node/register --watch --watch-files src, tests/**/*.ts"
  },

When you change a file in either folder src or tests it will rerun mocha

Look at the documentation here to understand the syntax : https://mochajs.org/#-watch-files-filedirectoryglob

@sweetim my setup didn’t work either when I just used option mocha -r ts-node/register.

In README.md example says mocha --compilers ts:ts-node/register,tsx:ts-node/register which seems to work with --watch-extensions ts --watch correctly.

My working script for mocha 7.0.2:

"test:watch": "TS_NODE_TRANSPILE_ONLY=true NODE_ENV=test mocha --require ts-node/register --watch-extensions ts --watch --watch-files src 'src/**/**.spec.ts'"
  • TS_NODE_TRANSPILE_ONLY=true is optional for faster initializations
  • NODE_ENV=test optional
  • --require ts-node/register for TS
  • --watch-extensions ts for TS
  • --watch obviously
  • --watch-files src this did the trick. that’s what was missing for me

I managed to get --watch working with:

package.json

  "scripts": {
    "test": "mocha --opts mocha.opts",
    "test:watch": "mocha --opts mocha.opts --watch"
  },

mocha.opts

--require ts-node/register
--watch-extensions ts
tests/**/*.ts

If you’re using a config file with your test scripts, this combination seems to work with mocha 7:

scripts
"scripts": {
  "test": "mocha \"**/*.test.ts\" --config ./config/mocha/.mocharc.json",
  "test:watch": "npm run test -- --watch",
}
./config/mocha/.mocharc.json
{
    "require": "ts-node/register",
    "watch-files": ["./src/**/*.ts"] 
}

NB: the path supplied to watch-files must be relative to where the mocha command is called from, not the config file location, which surprised me.

As suggested by @snaptopixel , I added the watch-extension it is working now

Before

npm run test -- --watch -R min

After

npm run test -- --watch-extensions ts --watch -R min

@EliudArudo @JimLynchCodes

--watch-extensions was removed from mocha 7, so if you’re trying to use that flag, it’s not doing anything. It looks like there are other flags that fill the same role now, so I assume you have to switch to them. https://github.com/mochajs/mocha/blob/master/CHANGELOG.md#700--2020-01-05

I followed @elhigu’s advice, which worked; here is the full command for future visitors to this thread:

mocha --compilers ts:ts-node/register,tsx:ts-node/register --watch-extensions ts --watch test/**/*.spec.ts

Note however that you’ll get a deprecation warning: (node:50228) DeprecationWarning: "--compilers" will be removed in a future version of Mocha; see https://git.io/vdcSr for more info

When I followed that link and applied the advice, I ended up with this command, which also works, but nonetheless produces the same warning:

mocha --require ts-node/register --watch-extensions ts --watch "test/**/*.spec.ts"

🤷‍♂️

seems like this is still happening? I have just installed the latest mocha 7.0.0 and couldn’t get the watch feature to work; didn’t look much into it though and ended up reverting to 6.1.4.

--watch-extensions ts doesn’t work for me anymore with mocha 6.x. I have exactly the same config as suggested here and it shows 0 passing tests. When I use it without --watch command, it works correctly.

Do you have any ideas what might be wrong?

Btw, for anyone who is interested in a workaround:

I gave up on using mocha --watch and just switched to using Chokidar instead.