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

Created on 25 Jan 2017  ·  29Comments  ·  Source: TypeStrong/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?

bug research

Most helpful comment

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

All 29 comments

@blakeembrey i could compile the code without problem

Do you have an example project you'd be able to share? The only reason I can think of this happening is if there's a compilation error blocking the re-require. I don't think mocha does anything else that's special.

I created a simple example to showcase this problem, below is the code:

Below, is my project directory

   > student.ts
test
   > mocha.opts
   > student.test.ts
// src/student.ts
export class Student {
    constructor(
        private _name: string,
    ) {    }

    get name(): string {
        return this._name;
    }
}
// test/student.test.ts
import * as assert from 'assert';
import { Student } from '../src/student';

describe('Student', () => {
    describe('#getName()', () => {
        it('should return name', () => {
            let s = new Student('ali', 12);
            assert.strictEqual(s.name, 'ali');
        })
    });
});

```typescript
// package.json
"scripts": {
"test": "mocha",
"test:watch": "npm run test -- --watch -R min"
},
"devDependencies": {
"@types/mocha": "^2.2.38",
"@types/node": "^7.0.4",
"mocha": "^3.2.0",
"ts-node": "^2.0.0",
"typescript": "^2.1.5"
}


// mocha.opts
--ui bdd
--recursive
--require ts-node/register
test/*.ts

```typescript
// tsconfig.json
{
    "compilerOptions": {
        "target": "ES6",
        "module": "commonjs",
        "noEmitOnError": true,
        "noImplicitAny": true,
        "experimentalDecorators": true,
        "sourceMap": true,
        "sourceRoot": "src",
        "outDir": "bin",
        "typeRoots": [  // added to fix cannot find type definition file for node in windows
            "node_modules/@types"
        ]
    }
}

I try to run the watch mode, it will just get stuck there and it doesnt reload whenever I saved the file, or add and remove new file

I have tried this on Windows 10 and Linux, both OS doesnt work...

and I found another bug, when I am using ts-node on Windows, it will complain cannot find type definition file for node which is related to issue #216 , this doesnt happen at Linux

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

@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.

Oops, I was trying to apply advices from this thread, but got no luck in OSX by far.

Currently node_modules/.bin/mocha --opts mocha-ts.opts --watch from my boilerplate repo is like:

% node_modules/.bin/mocha --opts mocha-ts.opts --watch

running /Users/ME/sidekick/i-reflog/lib-ts/index.ts

  ✓ TestSuite testSync: 0ms
  ✓ TestSuite testAsync: 2ms

  2 passing (8ms)

(after I change a file and save)

running /Users/ME/sidekick/i-reflog/lib-ts/index.ts


  0 passing (0ms)

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

Note that although this is mostly working you may still experience #113 when syntax errors occur and you have to restart the watch

I am having still the same problem of @jokester that after I save a file it does not find the tests anymore.

Same problem as @jokester and @Nepomuceno
If I change a file once I get the 0 passing (0ms) and I can actually see some console.log output from some of the code that is tested. If I change a file a second time, mocha does not react any more.

Btw, for anyone who is interested in a workaround:

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

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

@gpresland youp works for me too npm run test:watch execute cmds like:

npm run test -- --watch-extensions ts --watch
mocha --opts ./src/test/mocha.opts "--watch-extensions" "ts" "--watch"

and watch works very well

@elhigu Saved my life!

I’m going to close this issue now. Based on all the comments it looks like a lack of watch extensions was the issue.

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"

🤷‍♂️

--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?

Created sample repro here

Ok, guys, that's the issue from mocha. It's closed and will be available soon.

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.

I'm using mocha 7.0.1 and can't get watch more working either...

When I try mocha.opts it gives me an error that mocha.opts is deprecated, and when it try this it only runs once and doesn't watch:

mocha -r ts-node/register src/**/*.test.ts --watch --watch-extensions ts

Can confirm that it's a bug on mocha 7.0.1. I downgraded to 5.0.0 with this command and it works.

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

@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

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

@julienreszka Are you able to test the example proposed in #961? I don't use mocha's watch mode in any of my projects, so I'm not 100% sure it works.

Is there any way to have mocha tests written in JavaScript watch files written in TypeScript?

I have integration tests that merely test my routes, but not necessarily my TypeScript code. I just want my tests to re-run after I change one of my TypeScript files without having to convert all my tests to TypeScript.

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.

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
Was this page helpful?
0 / 5 - 0 ratings

Related issues

KiaraGrouwstra picture KiaraGrouwstra  ·  3Comments

watzon picture watzon  ·  3Comments

nehalist picture nehalist  ·  3Comments

mattdell picture mattdell  ·  4Comments

cevek picture cevek  ·  4Comments