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

Error on ESM TypeScript project when using ts-node + mocha #1094

Open lostvicking opened 2 years ago

lostvicking commented 2 years ago

Error is thrown when running mocha tests

ESM TypeScript project is being tested with Mocha, when ts-auto-mock's createMock is called an error is thrown

Your environment

Steps to reproduce

Example repo here: https://github.com/lostvicking/esm-ts-auto-mock-example Run npm test

Expected behavior

Expect createMock to generate a mock of specified type without errors.

Actual behavior

An error is thrown:

Error:
  hey, it looks like ts-auto-mock is not configured correctly! You can find the instructions here https://typescript-tdd.github.io/ts-auto-mock/installation.
  If you need further assistance feel free to drop a message on slack. (link at the bottom of https://typescript-tdd.github.io/ts-auto-mock)

    at r.createMock (/Users/victorneiman/Code/esm-ts-auto-mock-example/node_modules/ts-auto-mock/index.js:1:502)
    at file:///Users/victorneiman/Code/esm-ts-auto-mock-example/tests/test.ts:5:26
    at ModuleJob.run (internal/modules/esm/module_job.js:170:25)
    at async Loader.import (internal/modules/esm/loader.js:178:24)
    at async formattedImport (/Users/victorneiman/Code/esm-ts-auto-mock-example/node_modules/mocha/lib/nodejs/esm-utils.js:7:14)
    at async Object.exports.requireOrImport (/Users/victorneiman/Code/esm-ts-auto-mock-example/node_modules/mocha/lib/nodejs/esm-utils.js:48:32)
    at async Object.exports.loadFilesAsync (/Users/victorneiman/Code/esm-ts-auto-mock-example/node_modules/mocha/lib/nodejs/esm-utils.js:103:20)
    at async singleRun (/Users/victorneiman/Code/esm-ts-auto-mock-example/node_modules/mocha/lib/cli/run-helpers.js:125:3)
    at async Object.exports.handler (/Users/victorneiman/Code/esm-ts-auto-mock-example/node_modules/mocha/lib/cli/run.js:374:5)
uittorio commented 2 years ago

Hello,

I did not forget about this, just being busy with the family, and decided not to spend time on ts-auto-mock.

Anyway, it has been 2 months 🤣

I couldn't fix the problem or find the root cause yet but I've checkout out your repo and I've realised that it was not configured correctly, or at least the transformer was not running at all. Instead of using the example provided by ts-auto-mock I've installed ts-patch to run the transformer.

this unfortunately gave me another error

    at file:///Users/{username}/repos/esm-ts-auto-mock-example/tests/test.ts:5:25
    at ModuleJob.run (node:internal/modules/esm/module_job:197:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:337:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)
➜  esm-ts-auto-mock-example git:(master) ✗ 

I think I am almost there, I just need to study esm modules.

I am quite certain that the error is because of something we do in ts-auto-mock about imports that does not get converted correctly for esm

CarsonF commented 2 months ago

I just hit this. It's because the transformer generates a namespaced import, when actually a default import is needed.

https://github.com/Typescript-TDD/ts-auto-mock/blob/master/src/typescriptFactory/typescriptFactory.ts#L442-L455

Changing to this generates correctly:

core.ts.factory.createImportClause(false, moduleName)

Though obv that would need to be conditional.