-
-
Notifications
You must be signed in to change notification settings - Fork 537
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
Comments
Is this issue a duplicate of #113? |
@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
// 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"
}
// 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 and I found another bug, when I am using |
I had this issue too, but just fixed it by adding |
@sweetim my setup didn't work either when I just used option In |
Oops, I was trying to apply advices from this thread, but got no luck in OSX by far. Currently
|
As suggested by @snaptopixel , I added the Before
After
|
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 |
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 package.json "scripts": {
"test": "mocha --opts mocha.opts",
"test:watch": "mocha --opts mocha.opts --watch"
}, mocha.opts
|
@gpresland youp works for me too npm run test -- --watch-extensions ts --watch
mocha --opts ./src/test/mocha.opts "--watch-extensions" "ts" "--watch" and watch works very well |
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
@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:
Note however that you'll get a deprecation warning: When I followed that link and applied the advice, I ended up with this command, which also works, but nonetheless produces the same warning:
🤷♂️ |
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. |
…sions bump: "@types/node 12.6.8", "mocha 6.2.0" (Related issue: TypeStrong/ts-node#266)
seems like this is still happening? I have just installed the latest mocha |
I'm using mocha When I try
|
Can confirm that it's a bug on mocha
|
|
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 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. |
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:
|
|
@dedeard yep, this was fixed years ago 👍 |
I just encounter the same issue right now in mocha 10.2.0, and yes --watch-files did the trick. |
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
The version I am currently using
It works on the first time, but when I change the file it doesnt run again?
How to fix this problem?
The text was updated successfully, but these errors were encountered: