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

Can't run tests in Typescript #4900

Closed
Rc85 opened this issue Jul 11, 2022 · 4 comments
Closed

Can't run tests in Typescript #4900

Rc85 opened this issue Jul 11, 2022 · 4 comments
Labels
area: integrations related to working with 3rd party software (e.g., babel, typescript)

Comments

@Rc85
Copy link

Rc85 commented Jul 11, 2022

Prerequisites

node ../../node_modules/mocha/bin/mocha -r ts-node/register --loader=ts-node/esm

  • [x ] Checked that your issue hasn't already been filed by cross-referencing issues with the faq label
  • [x ] 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.
  • [x ] '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
  • [x ] 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

At first, I was getting this error Debug Failure. False expression: Non-string value passed to ts.resolveTypeReferenceDirective, likely by a wrapping package working with an outdated 'resolveTypeReferenceDirectives' signature. This is probably not a problem in TS itself.

Upgraded ts-node to latest version and then got this error ``TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts".

Added the --loader=ts-node/esm flag and now I'm getting this error ReferenceError: exports is not defined in ES module scope

Played around with module and target in tsconfig.json and none has helped. Changing target from commonjs to es2015 gives me error saying that it Error: Cannot find module yadayada import from soandso.

I'm out of ideas.

Steps to Reproduce

  1. Install Typescript 4.5.4, ts-node 10.8.2, mocha 10.0.0, run tests written in typescript with the npm command above.

Expected behavior: Should run tests

Actual behavior: Tests not running due to error mentioned above.

Reproduces how often: 100%

Versions

typescript 4.5.4
ts-node 10.8.2
mocha 10.0.0

Windows 10 64-bit

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

Additional Information

@Rc85
Copy link
Author

Rc85 commented Jul 12, 2022

I can run my tests if I use this flag with node --es-module-specifier-resolution=node

@yasinkocak
Copy link

I get the similar issue, on my monorepo when test import from another package inside the monorepo

@juergba
Copy link
Member

juergba commented Jul 16, 2022

Node's --loader option is still experimental, afaik. It feels like the situation around TS / ts-node / ESM is changing every few weeks/months. So I recommend you to follow the according issues on the ts-node repo.

Please have a closer look on our sample repo mochajs/mocha-examples#47 (comment) and the listed links. Most probably you will find some answers to make TS / ESM run with ts-node.

Some comments from my side:

  • bin/mocha has changed to bin/mocha.js in mocha@10.0.0
  • afaik using both options -r ts-node/register --loader=ts-node/esm doesn't make sense

@juergba juergba added area: integrations related to working with 3rd party software (e.g., babel, typescript) and removed unconfirmed-bug labels Jul 16, 2022
@juergba juergba closed this as completed Jul 16, 2022
@Rc85
Copy link
Author

Rc85 commented Jul 16, 2022

Node's --loader option is still experimental, afaik. It feels like the situation around TS / ts-node / ESM is changing every few weeks/months. So I recommend you to follow the according issues on the ts-node repo.

Please have a closer look on our sample repo mochajs/mocha-examples#47 (comment) and the listed links. Most probably you will find some answers to make TS / ESM run with ts-node.

Some comments from my side:

  • bin/mocha has changed to bin/mocha.js in mocha@10.0.0
  • afaik using both options -r ts-node/register --loader=ts-node/esm doesn't make sense

The tests in the sample repo doesn't import TS files into the test, which I think is the main issue here. Anyways, using -r ts-node/register --loader=ts-node/esm and --es-module-specifier-resolution=node allows me to run my tests again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: integrations related to working with 3rd party software (e.g., babel, typescript)
Projects
None yet
Development

No branches or pull requests

3 participants