Skip to content

Mocking globals with Node's new native TypeScript support doesn't work #319

@stuarthowieson

Description

@stuarthowieson

Hi there,

It doesn't seem to be possible to mock globals with Node's new --experimental-strip-types TypeScript support.

The following code correctly makes console.log do nothing when mocking a JS file but not a TS file:

const { someFunction } = await esmock.strict("./file.ts", {
  import: {
    console: {
      log: () => {},
    },
  },
});

I've traced through the code and believe the issue is the regex on line 25 in esmockLoader.js. The new TypeScript support introduced a new format of module-typescript (possibly a CJS variant too, not checked) which the loader isn't aware of. I added the new format to the regex locally and it works as expected so it seems to be a simple fix.

I'm happy to contribute the change and a relevant test if you're happy with the above fix?

PS: esmock is brilliant, thanks for making it!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions