Skip to content

node-tap mocking alongside ts-node #1417

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
drazisil opened this issue Aug 5, 2021 · 11 comments
Closed

node-tap mocking alongside ts-node #1417

drazisil opened this issue Aug 5, 2021 · 11 comments

Comments

@drazisil
Copy link

drazisil commented Aug 5, 2021

This might work better as a different issue type, if so let me know.

Expected Behavior

I am able to mock require

Actual Behavior

I receive this relatively unhelpful (to me) error stack

    Object.<anonymous> (test/dosStub.ts:8:3)

    Module.m._compile (node_modules/ts-node/src/index.ts:1225:23)

    Object.require.extensions.<computed> [as .ts] (node_modules/ts-node/src/index.ts:1228:12)

Steps to reproduce the problem

This is probably partly related to the node-tap's way of mocking, which is documented here. https://node-tap.org/docs/api/mocks/#api

Minimal reproduction

Since my code is already quite short, pasting it here in it's entirety

import t from "tap";
const { DosStub} = t.mock("../lib/dosStub.ts", {
  console: {
    error: () => {}
  }
});

t.test("DosStub", (t) => {
  t.throws(function() {new DosStub()}, 'foo')
  t.done
});

The contents of the dosStub.ts file are a single exported class. Since this is a require error, I don't think the content of that file are relevant, but I will share if desired.

TypeStrong/ts-node-repros#15

Specifications

  • ts-node version: v10.1.0
  • node version: v14.16.1
  • TypeScript version: v4.3.5
  • tsconfig.json, if you're using one:
{ }
  
  • Operating system and version:
  • If Windows, are you using WSL or WSL2?:

Description: Manjaro Linux
Release: 21.1.0

@drazisil
Copy link
Author

drazisil commented Aug 5, 2021

So as not to clutter the template, I'll add a couple notes here. Since the error says it occurs here https://github.com/TypeStrong/ts-node/blob/v10.1.0/src/index.ts#L1228 I speculate that https://github.com/TypeStrong/ts-node/blob/v10.1.0/src/index.ts#L1212 is at fault, based on the wording, and s-node is unable to register .ts as a extension that node can handle.

Per the docs, https://nodejs.org/dist/latest-v14.x/docs/api/modules.html#modules_require_extensions this API has been depreciated for some time, but I understand there is no other way to do this. nodejs/node#32483

Based on https://github.com/nodejs/node/blob/v14.16.1/test/parallel/test-module-multi-extensions.js#L20-L30, it appears that Node still tests that this works, so it does not appear to be broken. I have no idea where to hunt from here.

@drazisil
Copy link
Author

drazisil commented Aug 5, 2021

https://nodejs.org/dist/latest-v14.x/docs/api/esm.html#esm_no_require_extensions

Is it because I'm usingimport and tap is rewriting require, which import doesn't use, per the above?

No, tap should support this, per https://node-tap.org/docs/using-with/#using-tap-with-esm 🤔

@cspotcode
Copy link
Collaborator

A couple quick observations:

Based on your reproduction, ts-node is throwing an internal error that it cannot find @types/node. I believe this is a separate bug which I've tracked here: #1419
However, it may be masking the tap-related bug which you are trying to demonstrate. I believe you can work around the @types/node bug by adding @types/node as a dependency in package.json. This should make your reproduction more illustrative.

Second, there's a syntax error in f.ts: https://github.com/TypeStrong/ts-node-repros/blob/1fd4007a3ffa335fd786e823b1a9011aabe454a6/test/f.ts
What happens when the syntax error is corrected? Does the error you receive change at all?

@drazisil
Copy link
Author

drazisil commented Aug 5, 2021

Both fixed, thanks. Maybe we'll get the correct error now.

@cspotcode
Copy link
Collaborator

cspotcode commented Aug 5, 2021 via email

@drazisil
Copy link
Author

drazisil commented Aug 5, 2021

There are two commits since I'm editing on web on mobile. I think the typo was fixed with the second one? Otherwise I'm not seeing it and will have to review tomorrow

@cspotcode
Copy link
Collaborator

cspotcode commented Aug 5, 2021 via email

@drazisil
Copy link
Author

drazisil commented Aug 5, 2021

It's ready to run now and is giving the "correct" error locally.

@cspotcode cspotcode changed the title Object.require.extensions.<computed> [as .ts] (node_modules/ts-node/src/index.ts:1228:12) node-tap mocking alongside ts-node Aug 5, 2021
@cspotcode
Copy link
Collaborator

cspotcode commented Aug 7, 2021

still has a typo EDIT maybe spoke too soon

@cspotcode
Copy link
Collaborator

Ok yeah, still has a typo in t.ts

@drazisil
Copy link
Author

drazisil commented Aug 7, 2021

Ok yeah, still has a typo in t.ts

Where? It runs the tests and gives the error I expect. I'd be happy if the error was due to a typo. 👀

NM. Sorry to waste your time. :-/ I see the issue. I need to stop switching between tap and jest, it's just close enough syntax.

@drazisil drazisil closed this as completed Aug 7, 2021
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants