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

[BUG] Unable to test with jest #639

Closed
2 tasks done
grosch opened this issue Jun 20, 2022 · 3 comments
Closed
2 tasks done

[BUG] Unable to test with jest #639

grosch opened this issue Jun 20, 2022 · 3 comments
Labels

Comments

@grosch
Copy link

grosch commented Jun 20, 2022

Before you begin...

  • I have searched the existing issues
  • I am not using version 13.x of node (if so, please upgrade)

Description of the problem

Using jest 28.1.1, which does support ESM now, I'm not able to use this library. When I try and do a test I get this error:

SyntaxError: The requested module 'uuid' does not provide an export named 'v4'

  at Runtime.linkAndEvaluateModule (node_modules/jest-runtime/build/index.js:828:5)
  at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:317:13)
  at runJest (node_modules/@jest/core/build/runJest.js:407:19)
  at _run10000 (node_modules/@jest/core/build/cli/index.js:339:7)
  at runCLI (node_modules/@jest/core/build/cli/index.js:190:3)

Recipe for reproducing

In a spec file do this:


import {v4} from 'uuid'

describe('Employee', () => {
    let name: string

    beforeEach(() => {
        name = v4()
    })

Additional information

I saw another issue on this from a couple years ago, but that essentially said "Jest doesn't support ESM" but you can do ESM modules now. This is my jest.config.mjs, where I've included uuid in the transformIgnorePatterns

import ngPreset from 'jest-preset-angular/presets/index.js'

globalThis.ngJest = {
    skipNgcc: false,
    tsconfig: 'tsconfig-esm.spec.json'
}

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
const jestConfig = {
    ...ngPreset.defaultsESM,
    globals: {
        'ts-jest': {
            ...ngPreset.defaultsESM.globals['ts-jest'],
            tsconfig: '<rootDir>/tsconfig-esm.spec.json'
        }
    },
    globalSetup: 'jest-preset-angular/global-setup',
    moduleNameMapper: {
        tslib: 'tslib/tslib.es6.js',
        rxjs: '<rootDir>/node_modules/rxjs/dist/bundles/rxjs.umd.js'
    },
    setupFilesAfterEnv: ['<rootDir>/setup-jest-esm.ts'],
    transformIgnorePatterns: ['node_modules/(?!@infragistics|@igniteui|uuid|tslib|.*\\.mjs$)']
}

export default jestConfig

Environment

System:
OS: macOS 12.4
CPU: (10) arm64 Apple M1 Pro
Memory: 212.77 MB / 16.00 GB
Shell: 6.21.00 - /bin/tcsh
Binaries:
Node: 18.4.0 - /opt/homebrew/bin/node
Yarn: 1.22.19 - /opt/homebrew/bin/yarn
npm: 8.12.1 - /opt/homebrew/bin/npm
Browsers:
Chrome: 102.0.5005.115
Safari: 15.5
npmPackages:
@angular-devkit/build-angular: ^14.0.2 => 14.0.2
@angular/animations: ^14.0.0 => 14.0.2
@angular/cli: ~14.0.2 => 14.0.2
@angular/common: ^14.0.0 => 14.0.2
@angular/compiler: ^14.0.0 => 14.0.2
@angular/compiler-cli: ^14.0.0 => 14.0.2
@angular/core: ^14.0.0 => 14.0.2
@angular/forms: ^14.0.0 => 14.0.2
@angular/localize: ^14.0.0 => 14.0.2
@angular/platform-browser: ^14.0.0 => 14.0.2
@angular/platform-browser-dynamic: ^14.0.0 => 14.0.2
@angular/router: ^14.0.0 => 14.0.2
@infragistics/igniteui-angular: 14 => 14.0.1
@ng-bootstrap/ng-bootstrap: ^13.0.0-beta.1 => 13.0.0-beta.1
@ngneat/spectator: ^11.0.1 => 11.0.1
@ngneat/until-destroy: ^9.2.0 => 9.2.0
@popperjs/core: ^2.10.2 => 2.11.5
@types/jest: ^28.1.2 => 28.1.2
@types/uuid: ^8.3.4 => 8.3.4
bootstrap: ^5.1.3 => 5.1.3
jest: ^28.1.1 => 28.1.1
jest-preset-angular: ^12.1.0 => 12.1.0
ng-mocks: ^14.0.1 => 14.0.1
ngx-ui-loader: ^13.0.0 => 13.0.0
rxjs: ~7.5.0 => 7.5.5
tslib: ^2.3.0 => 2.4.0
typescript: ~4.7.2 => 4.7.4
uuid: ^8.3.2 => 8.3.2
zone.js: ~0.11.4 => 0.11.6

@grosch grosch added the bug label Jun 20, 2022
@uniqname
Copy link

Forgive me if I'm way off on this, I haven't dived too deep on this yet but I am running into this issue as well. I think the issue is with esm vs cjs resolution in the exports field in package.json. UUID doesn't have a type field in the package.json so Jest expects this to be a cjs package (absent an .mjs file extension). The "default" field in exports defaults to an esm file which causes Jest to choke when it sees the first export.

Probably the least invasive way to solve this would be for the esm-* folders to have a package.json file that just has

{
  "type": "module"
}

But there are also more holistic solutions too.

@uniqname
Copy link

Ooo, but this workaround in #451 worked for me.

@grosch
Copy link
Author

grosch commented Jun 23, 2022

This is actually a duplicate of #451. whoops

@grosch grosch closed this as completed Jun 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants