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

Using td.replaceEsm() in a TypeScript project #480

Closed
3 tasks done
beyerleinf opened this issue Feb 11, 2022 · 6 comments
Closed
3 tasks done

Using td.replaceEsm() in a TypeScript project #480

beyerleinf opened this issue Feb 11, 2022 · 6 comments

Comments

@beyerleinf
Copy link

beyerleinf commented Feb 11, 2022

Description

I'm currently trying to use TestDouble to mock dependencies in my TypeScript + ESM project. However, I'm not sure if what I'm trying to accomplish is even possible.

Issue

Using the testdouble loader throws an error when running my tests. Currently both ts-node/esm and testdouble loader are used in .mocharc.json but it also doesn't work with just the testdouble loader (see error below) or just the ts-node/esm loader (obviously mocking doesn't work). Have I reached the limits of what's possible and need to try and remove the ESM dependencies? Or would I need to ditch TypeScript to make this work?

(node:47550) DeprecationWarning: Obsolete loader hook(s) supplied and will be ignored: getFormat, getSource
(node:47550) ExperimentalWarning: The Node.js specifier resolution in ESM is experimental.

TypeError: Invalid module "file:///xxxxx/esbuild-azure-functions/src/builder.spec.ts" 
    at new NodeError (node:internal/errors:371:5)
    at ESMLoader.load (node:internal/modules/esm/loader:324:13)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:230:47)
    at link (node:internal/modules/esm/module_job:67:21)

Environment

  • node -v output: v16.13.1
  • npm -v output: 8.1.2
  • npm ls testdouble version: 3.16.4

Example Repo

You can clone this repo and run npm t to see the error.

@searls
Copy link
Member

searls commented Feb 11, 2022

I need to lean on @giltayar's help to understand this one

@quinnturner
Copy link
Contributor

Just chiming in here since I've dealt with this issue as well. From what I gather, there is currently a limitation in Node.js to support more than one loader with ESM in the way that you're describing. It's impossible to use this project in conjunction with ts-node/esm. There is active discussion/work going on here: https://github.com/nodejs/loaders.

My personal recommendation is to write tests in JavaScript with // @ts-check. If you use a TypeScript monorepo, you can continue to write tests in TypeScript in projects that don't use testdouble.

@beyerleinf
Copy link
Author

beyerleinf commented Feb 12, 2022

Thanks @quinnturner for clarifying! I saw the discussion about the loaders but thought maybe someone has found a way 😅 Do you have any examples on how you've dealt with this?

@searls This issue can be closed, if you want to :)

@searls
Copy link
Member

searls commented Feb 12, 2022

Ok, we'll all just have to hope for better loader support in Node proper 🤞

@searls searls closed this as completed Feb 12, 2022
@giltayar
Copy link
Collaborator

I have a solution. But today is a hectic day, so I'll write it down tomorrow. Keep your hopes up high, because at my company we're using both Testdouble and TypeScript.

@giltayar
Copy link
Collaborator

giltayar commented Feb 14, 2022

OK, here goes. You need three loaders! One for TypeScript, one for TestDouble (this one!), and one for supporting multiple loaders because Node.js doesn't support multiple loaders(yet. work is ongoing to fix this).

TypeScript: use https://github.com/giltayar/babel-register-esm to implement TypeScript support. You will need a babel.config.json that has the plugin for TypeScript, but, hey, as long as it works! See instructions on how to use this.

Testdouble: use the testdouble loader for supporting td.replaceEsm().

Multiple loaders: use https://github.com/giltayar/esm-multi-loader. See instructions on how to use this to load both the testdouble loader and the babel-register-esm loader.

Hope this helps..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants