marianozunino / morpheus

Morpheus is database migration tool for Neo4j written in Typescript.
MIT License
18 stars 4 forks source link

Error when using lib with jest e2e tests #13

Closed maximeSurmontGH closed 2 years ago

maximeSurmontGH commented 2 years ago

Me again :)

I m just trying to implement e2e tests and I run into this error :

 FAIL  test/app.e2e-spec.ts
  ● Test suite failed to run

    Cannot find module 'morpheus4j/nest' from '../src/app.module.ts'

    Require stack:
      /{path}/project-name/src/app.module.ts
      app.e2e-spec.ts

      2 | import { AppController } from './app.controller';
      3 | import { AppService } from './app.service';
    > 4 | import { MorpheusModule } from 'morpheus4j/nest';
        | ^

Do you have any idea where it can come from ? I can provide a repo with this error reproducted, or just do :

nest new project-test
cd project-test
npm install morpheus4j
# set MorpheusModule module
npm run test:e2e
marianozunino commented 2 years ago

Hey @maximeSurmontGH Since currently Jest does not support ESM (https://stackoverflow.com/a/62210156/7599734), you can bypass this issue importing the module as so: import { MorpheusModule } from 'morpheus4j/dist/nest';

maximeSurmontGH commented 2 years ago

Oh ! Just tried and ran into this error :

Error: Package subpath './dist/nest' is not defined by "exports" in /Users/maximesurmontlcdm/Documents/LCDM/agriflux-app/server/node_modules/morpheus4j/package.json
    at new NodeError (node:internal/errors:371:5)
    at throwExportsNotFound (node:internal/modules/esm/resolve:440:9)

I "fixed it" locally by adding :

// morpheus4j's package.json
{
 // ...
  "exports": {
    // ...
    "./dist/nest": "./dist/nest/index.js",
  }

}

I also tried to run jest with their experimental ESM support but no change 😭

marianozunino commented 2 years ago

Do you mind cloning and checking if this repo works for you? https://github.com/marianozunino/morpheus-e2e-bug

marianozunino commented 2 years ago

Either way, I changed the default export to be Nest's module. Since there is no reason to export the other functions. So, now you should be able to import the module just using :

import { MorpheusModule } from 'morpheus4j';

maximeSurmontGH commented 2 years ago

Works well thx 🙏