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

Issue migrating my generators' tests to TypeScript (ERR_PACKAGE_PATH_NOT_EXPORTED) #164

Closed
groovytron opened this issue Jan 19, 2023 · 10 comments

Comments

@groovytron
Copy link

Hi,

I'm having troubles migrating my generators' tests to TypeScript.

My project is https://github.com/groovytron/generator-skjol. The migration branch is migrate-to-typescript.

I get the following error when I run npm run test (which runs the tests with Mocha like the previous version of my project):

> [email protected] test
> nyc --all --reporter lcov mocha


Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /home/julien/Documents/swissted/generator-skjol/node_modules/yeoman-test/package.json
    at new NodeError (node:internal/errors:399:5)
    at exportsNotFound (node:internal/modules/esm/resolve:266:10)
    at packageExportsResolve (node:internal/modules/esm/resolve:546:13)
    at resolveExports (node:internal/modules/cjs/loader:551:36)
    at Function.Module._findPath (node:internal/modules/cjs/loader:620:31)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1039:27)
    at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (/home/julien/Documents/swissted/generator-skjol/node_modules/@cspotcode/source-map-support/source-map-support.js:811:30)
    at Function.Module._load (node:internal/modules/cjs/loader:898:27)
    at Module.require (node:internal/modules/cjs/loader:1120:19)
    at require (node:internal/modules/helpers:112:18)
    at Object.<anonymous> (/home/julien/Documents/swissted/generator-skjol/test/slides.spec.ts:2:15)
    at Module._compile (node:internal/modules/cjs/loader:1239:14)
    at Module.replacementCompile (/home/julien/Documents/swissted/generator-skjol/node_modules/append-transform/index.js:60:13)
    at Module.m._compile (/home/julien/Documents/swissted/generator-skjol/node_modules/ts-node/src/index.ts:1618:23)
    at module.exports (/home/julien/Documents/swissted/generator-skjol/node_modules/default-require-extensions/js.js:7:9)
    at /home/julien/Documents/swissted/generator-skjol/node_modules/append-transform/index.js:64:4
    at require.extensions.<computed> (/home/julien/Documents/swissted/generator-skjol/node_modules/ts-node/src/index.ts:1621:12)
    at Object.<anonymous> (/home/julien/Documents/swissted/generator-skjol/node_modules/append-transform/index.js:64:4)
    at Module.load (node:internal/modules/cjs/loader:1096:32)
    at Function.Module._load (node:internal/modules/cjs/loader:935:12)
    at Module.require (node:internal/modules/cjs/loader:1120:19)
    at require (node:internal/modules/helpers:112:18)
    at Object.exports.requireOrImport (/home/julien/Documents/swissted/generator-skjol/node_modules/mocha/lib/nodejs/esm-utils.js:53:16)
    at async Object.exports.loadFilesAsync (/home/julien/Documents/swissted/generator-skjol/node_modules/mocha/lib/nodejs/esm-utils.js:100:20)
    at async singleRun (/home/julien/Documents/swissted/generator-skjol/node_modules/mocha/lib/cli/run-helpers.js:125:3)
    at async Object.exports.handler (/home/julien/Documents/swissted/generator-skjol/node_modules/mocha/lib/cli/run.js:370:5)

My current node version is v19.4.0.

Is there an issue with yeoman-test's exports or am I doing something wrong with my Mocha TypeScript configuration ?

I tried to use Jest instead because I was suspecting there was an issue with ts-node when used with Mocha but I stumble on the same error. Jest is installed to. You can test with Jest by renaming the slides.spec.jest.ts into slides.spec.ts and run npm run jest.

I'm actually just testing the slides generator so only the script slides.spec.ts is executed at the moment. I will migrate the other tests once this one works.

Thanks a lot for your help and have a nice day.

@Tateuf
Copy link

Tateuf commented Jan 26, 2023

same error for me, i try to use yeoman-test for my generator with mocha and exactly the same error : ( Always no solution ?

@mshima
Copy link
Member

mshima commented Jan 26, 2023

yeoman-test@7 is in typescript and have built-in types, maybe drop the @types/yeoman-test dependency?

@groovytron
Copy link
Author

yeoman-test@7 is in typescript and have built-in types, maybe drop the @types/yeoman-test dependency?

Thanks for your answer but removing @types/yeoman-test doesn't fix the issue. I still get the same error.

@groovytron
Copy link
Author

groovytron commented Jan 26, 2023

I also tried to upgrade to [email protected] but the error still persists.

@mshima
Copy link
Member

mshima commented Jan 26, 2023

yeoman-test@7 is native esm: https://github.com/yeoman/yeoman-test/releases/tag/v7.0.0

You need to convert your project to esm, commonjs won't work:
https://github.com/groovytron/generator-skjol/blob/690b27f8392c1fdd03c8fb03813f83b1bbafdaf3/package.json#L8

Or stick with yeoman-test@6

@groovytron
Copy link
Author

@mshima thanks a lot for your help. My tests are now passing. I lost my code coverage but I'll dig into it. I'm not sure the problem comes from yeoman-test.

In case anybody encounters this issue, here is a diff between before and after the TypeScript migration: groovytron/generator-skjol@1.1.1...2.0.0-rc.0

@groovytron
Copy link
Author

I managed to fix my code coverage issue by migrating to c8. It looks like nyc cannot generate coverage with ESM at the moment.

Here are my changes: groovytron/generator-skjol@be4970f

Here are my findings:

@mshima
Copy link
Member

mshima commented Feb 1, 2023

For future reference, the project don't need to be esm, tests need to be esm. Can use mts extension.

@ssoworld
Copy link

We should consider backward-compatibility and not have to force users to move to ECMA. Companies using this have to seriously consider the consequences of not unit testing their generators and having to rely on older versions while maintaining their generators. Sure, reverting to 6 is OK, but when one runs npm outdated or has Renovate (or equivalent) automating their updates, they have to take steps to disable this. Let's make it simple.

@mshima
Copy link
Member

mshima commented Mar 25, 2023

Dependencies would be outdated since they are migrating to esm too.
A dev dependency like yeoman-test is much easier to workaround:

describe(() => {
  let helpers;
  before(async() => {
    helpers = (await import('yeoman-test')).default;
  });
});

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

4 participants