Typescript-TDD / ts-auto-mock

Typescript transformer to unlock automatic mock creation for interfaces and classes
https://typescript-tdd.github.io/ts-auto-mock
MIT License
600 stars 16 forks source link

The requested module 'ts-auto-mock' does not provide an export named 'createMock' #1453

Open invakid404 opened 1 year ago

invakid404 commented 1 year ago

Subject of the issue

When used in a Jest ESM environment, attempting to import createMock results in a error:

  ● Test suite failed to run

    SyntaxError: The requested module 'ts-auto-mock' does not provide an export named 'createMock'

      at Runtime.linkAndEvaluateModule (node_modules/jest-runtime/build/index.js:682:5)

Your environment

Steps to reproduce

Reproducible example available here. Project is set up in accordance to the Jest ESM docs.

Expected behavior

ts-auto-mock should work fine in ESM environments.

Actual behavior

ts-auto-mock doesn't work in ESM environments as importing it results in an error.

uittorio commented 1 year ago

Hey, I've looked into this a while ago and I couldn't figure it out yet! Happy to see a PR for the changes required to the build process!

CarsonF commented 3 months ago

This is a weird interaction with the "named" exports becoming the default import. So instead of this

import { createMock } from 'ts-auto-mock';

it needs to be

import autoMock from 'ts-auto-mock';
const { createMock } = autoMock;

Looking into how the index.js file can be patched to fix this.


This also happens regardless of whether the transformer ran successfully or not, since it does not remove these "fake" imports.

CarsonF commented 3 months ago

It looks like the current compiled code does

module.exports = { createMock: ... }

And it needs to do

module.exports.createMock = ...;
$ yarn node
Welcome to Node.js v22.3.0.
> await import('ts-auto-mock')
[Module: null prototype] {
  createHydratedMock: [Function (anonymous)],
  createMock: [Function (anonymous)],
  createMockList: [Function (anonymous)],
  default: {
    registerMock: [Function (anonymous)],
    createHydratedMock: [Function (anonymous)],
    createMockList: [Function (anonymous)],
    createMock: [Function (anonymous)]
  },
  registerMock: [Function (anonymous)]
}
CarsonF commented 3 months ago

Here's a yarn patch file that fixes both this & #1094

ts-auto-mock-npm-3.7.3-caa6a18d65.patch