mochajs / mocha

☕️ simple, flexible, fun javascript test framework for node.js & the browser
https://mochajs.org
MIT License
22.64k stars 3.02k forks source link

Watch fails on typescript after 2nd code recompile #4986

Closed nebosite closed 10 months ago

nebosite commented 1 year ago

Prerequisites

Description

With a simple typescript project, mocha in watch mode works correctly at first, but after the second source code change detection, it loses compiled information.

Steps to Reproduce

  1. Create a simple typescript project with a static class that exposes a single function to return a string and a test for that function. (.zip attached)
  2. run npm run testwatch (Result: there will be a passing test and a 'waiting for changes...' message
  3. Edit the tested function to return "hi1" and save.
  4. Edit the tested function again to return "hi2" and save.
  5. Edit the tested function again to return "hi3" and save.

Expected behavior:

Starting with step 3, should see a normal test failure after each edit and save.

Actual behavior: [What actually happens]

After first edit: Normal test failure After second edit: Fails with: TypeError: Cannot read properties of undefined (reading 'getMessage') After third edit: Compile failure: src/FooClass.spec.ts:2:26 - error TS2306: File 'src/FooClass.ts' is not a module.

Reproduces how often:

100%

Versions

"chai": "^4.3.4",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^3.9.9"

(I have also tried with earlier versions of mocha(8) and ts-node(9))

Node: 16.14.2 OS: Windows 11, 64 bit

mochaWatchFailure.zip

JoshuaKGoldberg commented 10 months ago

I don't reproduce this issue locally on macOS Ventura 13.2 with Node 16.14.2 or 20.11.0. Maybe it's a transient issue in a nested dependency? Maybe it's Windows-specific?

I'm charging an old Surface laptop to try on Windows 11 😄 . Will come back to this soon.

Aside: in general, it's not great practice to upload a .zip. Downloading and extracting arbitrary files from the internet is kind of scary for us maintainers 😄. I'd appreciate an isolated GitHub repository, Gist, Stackblitz, or other more transparent environment next time.

Here's a repro: https://github.com/JoshuaKGoldberg/repros/tree/mocha-typescript-watch-mode

JoshuaKGoldberg commented 10 months ago

Ok, yes, this doesn't reproduce for me on Windows 11 in stock cmd either. That, coupled with the lack of other reports on this area makes me suspect it was a transient issue in some other dependency. Closing as non-actionable.

Please do post back if it's still happening for you & you can provide an isolated reproduction with it. We'd be happy to take a look. Cheers!