How can I avoid app crashing because of importing jest?

Viewed 80

I tried many ways of importing jest into my project. All of them cause my app to crash displaying the blank screen when visiting localhost:3000 even though all statements cause app to compile (the single line of import causes the app to crash):

import { mocked } from "ts-jest"

import { mocked } from '@jest/globals'

import { jest } from 'ts-jest'

import { jest } from '@jest/globals'

App crashes with the following error:

VM551:2 Uncaught ReferenceError: process is not defined

I have the following dependency in package.json:

"ts-jest": "^28.0.7"

What might be the reason behind crashing? What is the correct way to import jest? I need specifically mocked object to be available, is there a right way to make it work without crashing the app?

1 Answers

It sounds like you're trying to import jest or mocked into your application code that runs in the browser, which won't work. process is a global in Node, but not in the browser, which is why you're getting this error:

Uncaught ReferenceError: process is not defined

If you're trying to use mock data, you can define that data in a separate file that you can import both in your test files (which run in Node) and in your application code (which runs in the browser).

Related