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

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

Open
invakid404 opened this issue Dec 9, 2022 · 4 comments

Comments

@invakid404
Copy link

invakid404 commented Dec 9, 2022

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

  • ts-auto-mock version: 3.6.4
  • typescript version: 4.9.4
  • node version: 18.12.1
  • yarn version: 1.22.19
  • jest version: 29.3.1
  • jest-ts-auto-mock version: 2.1.0

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
Copy link
Member

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
Copy link

CarsonF commented Jul 25, 2024

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
Copy link

CarsonF commented Jul 25, 2024

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
Copy link

CarsonF commented Jul 25, 2024

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

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

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

3 participants