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
- tackling mocha config for proper .ts watch (not working) maybe related: https://github.com/TypeStrong/ts-node/issues/266 — committed to jokester/typescript-boilerplate by jokester 7 years ago
- Update test watch script See: https://github.com/TypeStrong/ts-node/issues/266#issuecomment-277569351 — committed to epayet/react-redux-typescript-seed by epayet 7 years ago
- Write first typescript react test with enzyme 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 switc... — committed to 0livare/catalyst by 0livare 6 years ago
- Fix the mocha watch: Add --watch-extensions to the test:tdd task, versions bump: "@types/node 12.6.8", "mocha 6.2.0" (Related issue: https://github.com/TypeStrong/ts-node/issues/266) — committed to dmkachko/ts-project-seed by deleted user 5 years ago
- allows passing --watch to npm run testonly https://github.com/TypeStrong/ts-node/issues/266 — committed to yaacovCR/graphql-executor by yaacovCR 3 years ago
- allows passing --watch to npm run testonly https://github.com/TypeStrong/ts-node/issues/266 — committed to yaacovCR/graphql-executor by yaacovCR 3 years ago
I had this issue too, but just fixed it by adding
--watch-extensions ts
not sure if it’s the same problem as #113Anybody searching for a solution, try this:
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 saysmocha --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:
TS_NODE_TRANSPILE_ONLY=true
is optional for faster initializationsNODE_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 meI managed to get
--watch
working with:package.json
mocha.opts
If you’re using a config file with your test scripts, this combination seems to work with mocha 7:
scripts
./config/mocha/.mocharc.json
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 nowBefore
After
@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-05I 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:
(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:
🤷♂️
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 to6.1.4
.--watch-extensions ts
doesn’t work for me anymore with mocha6.x
. I have exactly the same config as suggested here and it shows0 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.