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 runs old javascript test instead of new typescript test #4981

Closed
4 tasks done
Almenon opened this issue Apr 30, 2023 · 2 comments
Closed
4 tasks done

Mocha runs old javascript test instead of new typescript test #4981

Almenon opened this issue Apr 30, 2023 · 2 comments
Labels
type: question support question

Comments

@Almenon
Copy link

Almenon commented Apr 30, 2023

Prerequisites

  • Checked that your issue hasn't already been filed by cross-referencing issues with the faq label
  • Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn't just a feature that actually isn't supported in the environment in question or a bug in your code.
  • 'Smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
  • Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with: node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend that you not install Mocha globally.

Description

Mocha runs the test in the compiled javascript file, instead of the test in the typescript file. My understanding of mocha with ts-node is that it should transpile the ts file on the fly, not read the javascript file.

Steps to Reproduce

  1. Clone https://github.com/mochajs/mocha-examples/tree/master/packages/typescript
  2. npm i
  3. Add console.log('foo') to packages\typescript\src\index.ts
  4. Remove outDir in packages\typescript\tsconfig.json
  5. run npm run compile. There will now be a index.js file in src.
  6. run npm test. "foo" is shown
  7. Change foo to fah in index.ts
  8. run npm test

Expected behavior: fah

Actual behavior: foo

Reproduces how often: [What percentage of the time does it reproduce?]
100%

Versions

  • The output of mocha --version and node_modules/.bin/mocha --version: latest
  • The output of node --version: 16.14.2
  • Your operating system
    • name and version: Windows 10
    • architecture (32 or 64-bit): 64-bit
  • Your shell (e.g., bash, zsh, PowerShell, cmd): cmd
  • Your browser and version (if running browser tests):
  • Any third-party Mocha-related modules (and their versions): ts-node ^9.1.1
  • Any code transpiler (e.g., TypeScript, CoffeeScript, Babel) being used (and its version): typescript ^4.2.4

Additional Information

Might be a issue with ts-node. What's interesting about this is that packages\typescript\.mocharc.json specifies the ts extension only, so I don't know why it's reading the js. Same thing with spec, it specifies just ts.

If you're wondering how this scenario would happen in the real world, I run into it from time to time in https://github.com/almenon/AREPL-backend. I compile the typescript to publish the package but don't always have the compiler running, so the compiled files might not match the typescript files.

@Almenon
Copy link
Author

Almenon commented Apr 30, 2023

Might be the same bug that https://stackoverflow.com/questions/54807005/mocha-runs-test-from-deleted-spec ran into

@JoshuaKGoldberg
Copy link
Member

Mocha can be configured to run on .ts source files using transpilers such as ts-node. But it doesn't do by default. By default, it just reads from .js files.

The mocha-examples/typescript example doesn't use a transpiler. That's why it has a separate compile command to run tsc.

  "scripts": {
    "compile": "tsc --declaration",
    "lint": "tslint --project tsconfig.json",
    "prepublish": "npm run compile",
    "test": "mocha"
  },

The typescript-babel example also doesn't use on-the-fly transpilation.

So this is really a docs / examples request - that we don't have any examples showing on-the-fly transpilation. Filed mochajs/mocha-examples#75 to add examples. Thanks for reporting! 🙌

@JoshuaKGoldberg JoshuaKGoldberg closed this as not planned Won't fix, can't repro, duplicate, stale Jan 18, 2024
@JoshuaKGoldberg JoshuaKGoldberg added type: question support question and removed unconfirmed-bug labels Jan 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question support question
Projects
None yet
Development

No branches or pull requests

2 participants