53

I used to solve similar errors while I was using Jest with only JavaScript, but currently I'm not able to do so with Typescript.

All my tests were running fine until I installed Puppeteer which requires @types/jest-environment-puppeteer, @types/puppeteer and @types/expect-puppeteer.

After installing them, puppeteer tests are running perfectly, but other tests started to fail with below error.

  D:\...\api\node_modules\uuid\dist\esm-browser\index.js:1    
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { default as v1 } from './v1.js';
                                                                                      ^^^^^^  

    SyntaxError: Unexpected token 'export'

      at Runtime.createScriptFromCode (../node_modules/jest-runtime/build/index.js:1796:14)   
      at Object.require (../node_modules/@nestjs/common/decorators/core/injectable.decorator.js:4:16)

WHAT I DID?

allowJs: true on tsconfig.json and set the transformIgnorePatterns on jest configs. So that jest can compile files from node_modules/ After that this error stopped but test failed for another strange reason. And worse is that test start time have increased too much.

So I left allowJs as in original setup and updated jest config from

"transform": {
   "^.+\\.(t|j)s$": "ts-jest"
}

to

"transform": {
   "^.+\\.(t)s$": "ts-jest"
}

So currently ts-jest doesnt compile js files. But I think I am not able to make babel pick the transformation for js files. These are my jest configs:

{
  "moduleFileExtensions": ["js", "json", "ts"],
  "rootDir": ".",
  "testEnvironment": "node",
  "testRegex": ".e2e-spec.ts$",
  "transform": {
    "^.+\\.(t)s$": "ts-jest",
    "^.+\\.(js|jsx)$": "babel-jest"
  },
  "transformIgnorePatterns": ["<rootDir>/node_modules/.+.(js|jsx)$"]
}
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
damdafayton
  • 1,600
  • 2
  • 10
  • 19

6 Answers6

82

Thanks to this reply: https://stackoverflow.com/a/54117206/15741905

I started googling for similar fixes and ended up here: https://github.com/uuidjs/uuid/issues/451

And this solved my problem: https://github.com/uuidjs/uuid/issues/451#issuecomment-1112328417

// jest.config.js
{
//................
  moduleNameMapper: {
    // Force module uuid to resolve with the CJS entry point, because Jest does not support package.json.exports. See https://github.com/uuidjs/uuid/issues/451
    "uuid": require.resolve('uuid'),
  }
}

Tough I would still be happy if there is solution to this by using jest-babel.

Because I had to carry jest configs from package.json to a seperate .js file.

Edit: According to this github issue compatibility issue has been solved with the latest release of the uuid library.

damdafayton
  • 1,600
  • 2
  • 10
  • 19
  • 1
    I was using a `jest.config.ts` file and the quotes kept getting stripped off `"uuid"` and the fix didn’t work. By making it `"^uuid$"` this started working for me. As of Aug 23, 2022 the latest version of uuid is still beta and the linked not above indicates it was only tested with the beta 29.x of jest so I think since I'm just now upgrading from 27.x I'll stick with this solution for now. Thanks. – Tod Aug 23 '22 at 22:39
  • 1
    you welcome. i understand you very well :) – damdafayton Oct 06 '22 at 21:02
30

Simply adding moduleNameMapper:{"^uuid$": "uuid"} into my jest.config.js fixed the issue for me:

transient dependency uuid: ^8.3.2

jest: 28.1.3

angular: 14.0.1

jawn
  • 851
  • 7
  • 10
0

Upgrade to uuidv4() version 9.0.0 ("uuid": "^9.0.0") now fixes the the issue. See comment from SimenB: https://github.com/uuidjs/uuid/issues/451#issuecomment-1377099565

I tried the answer above initially, adding moduleNameMapper:{"^uuid$": "uuid"} into my jest.config.js, which fixed the issue for me. Then I followed the link (above) to read more and I saw that it has been fixed in "uuid": "^9.0.0".

So I took out the fix, installed latest uuidv4() at version 9.0.0, (package link below) and it started working fine again. No more errors and tests are running fine, again. https://www.npmjs.com/package/uuid

My React App started having the issues after updating "jest": "26.0.1" to "jest": "29.4.1", "environment-jsdom-fifteen": "1.0.2" to "jest-environment-jsdom": "29.4.1". along with other updates "ts-jest": "26.5.6" to "ts-jest": "^29.0.5". However, as mentioned above, upgrading to uuid version 9.0.0 has fixed it.

Zen-life
  • 121
  • 1
  • 3
0

Date : 2/15/2023

Jest Version : "@types/jest": "^29.4.0",

UUID Version : "uuid": "^9.0.0"

using the jest.config.ts will generate an error that says

Multiple configurations found

here's how you can override that

/**
 * @format
 */

import 'react-native';
import React from 'react';
import App from '../src/App';
import {v4 as uuidv4} from 'uuid';

// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';

it('renders correctly', () => {
  renderer.create(<App />);
});

jest.mock('uuid', () => {
  return {
    v4: jest.fn(() => 1)
  }
})

Hoping this will solve other peoples error if the correct answer is not working for you :).

Ginxxx
  • 1,602
  • 2
  • 25
  • 54
0

adding this to the package.json file solved it for me:

"devDependencies": {
....
},
"browser": {
    "uuid": "./node_modules/uuid/dist/esm-browser/index.js"
  }
0

@damdafayton's answer partially worked for me (thank you), but it ended up causing problems with another library that uses uuid (in my case, mongoose). I think the override was causing mongoose to pick up the wrong version.

To overcome the game of whack-a-mole, I ended up creating a mock for uuid in my top-level __mocks__/uuid.js containing:

const uuid = require('../node_modules/uuid/dist');
module.exports = uuid;

...which seems to do the trick.

It may be that I'm more specifically forcing the version of uuid for jest, not interrupting the normal resolver, but it's hard to be certain.

Jim B.
  • 4,512
  • 3
  • 25
  • 53