Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mocha --watch not working with ts-node #266

Closed
sweetim opened this issue Jan 25, 2017 · 32 comments
Closed

mocha --watch not working with ts-node #266

sweetim opened this issue Jan 25, 2017 · 32 comments
Labels
bug research Needs design work, investigation, or prototyping. Implementation uncertain.

Comments

@sweetim
Copy link

sweetim commented Jan 25, 2017

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?

@blakeembrey
Copy link
Member

Is this issue a duplicate of #113?

@sweetim
Copy link
Author

sweetim commented Jan 25, 2017

@blakeembrey i could compile the code without problem

@blakeembrey
Copy link
Member

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.

@sweetim
Copy link
Author

sweetim commented Jan 25, 2017

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

Below, is my project directory

src
   > 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');
        })
    });
});
// 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
// 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

@snaptopixel
Copy link

snaptopixel commented Jan 28, 2017

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

@elhigu
Copy link

elhigu commented Feb 1, 2017

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

jokester added a commit to jokester/typescript-boilerplate that referenced this issue Feb 2, 2017
@jokester
Copy link

jokester commented Feb 2, 2017

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)

@sweetim
Copy link
Author

sweetim commented Feb 6, 2017

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

@snaptopixel
Copy link

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

@blakeembrey blakeembrey added research Needs design work, investigation, or prototyping. Implementation uncertain. bug labels Mar 21, 2017
@Nepomuceno
Copy link

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

epayet added a commit to epayet/react-redux-typescript-seed that referenced this issue Apr 10, 2017
@sgronblo
Copy link

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.

@sgronblo
Copy link

Btw, for anyone who is interested in a workaround:

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

@gpresland
Copy link

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

@agborkowski
Copy link

@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

0livare added a commit to 0livare/catalyst that referenced this issue Mar 12, 2018
Note that the 'test:watch' script is not currently working.  It runs the
tests fine, but will not run again.  I think this may have happened when
I switched to ts-mocha, because it was working before with just mocha.

However, I think that this thread might hold the answer:
  TypeStrong/ts-node#266
@CodeRedPaul
Copy link

@elhigu Saved my life!

@blakeembrey
Copy link
Member

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

@subelsky
Copy link

subelsky commented Jul 11, 2018

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"

🤷‍♂️

@s-KaiNet
Copy link

s-KaiNet commented Feb 21, 2019

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

@s-KaiNet
Copy link

Created sample repro here

@s-KaiNet
Copy link

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

dmkachko pushed a commit to dmkachko/ts-project-seed that referenced this issue Aug 1, 2019
…sions bump: "@types/node 12.6.8", "mocha 6.2.0"

(Related issue: TypeStrong/ts-node#266)
@juliantellez
Copy link

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.

@JimLynchCodes
Copy link

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

@EliudArudo
Copy link

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

@cspotcode
Copy link
Collaborator

@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

@julienreszka
Copy link

julienreszka commented Feb 6, 2020

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

@cspotcode
Copy link
Collaborator

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

@cotterjd
Copy link

cotterjd commented Mar 13, 2020

Is there any way to have JS mocha tests watch TS files?

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.

@patrickkunka
Copy link

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.

@felipeik
Copy link

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

@dedeard
Copy link

dedeard commented Feb 16, 2023

mocha -r ts-node/register test/**/*.test.ts --extension ts --watch
I guarantee it will work

@elhigu
Copy link

elhigu commented Feb 17, 2023

@dedeard yep, this was fixed years ago 👍

@reke592
Copy link

reke592 commented Feb 25, 2023

I just encounter the same issue right now in mocha 10.2.0, and yes --watch-files did the trick.
mocha --require ts-node/register test/**/*.ts --watch-files ./test/**/*.ts --watch-files ./src/**/*.ts --watch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug research Needs design work, investigation, or prototyping. Implementation uncertain.
Projects
None yet
Development

No branches or pull requests