Jest change mock implementation for a POJO module between tests

I am trying to mock the import of a Plain Old Javascript Object in a test, where I want different implementations for each test. If I mock at the top of the file it works as expected: import { …

TypeError: jwt(…).unless is not a function when mocking with Jest

I am trying to mock the .unless function of express-jwt and currently have this mock setup to compare a JWT with an admin and non admin. This worked perfect until we added .unless and I am not sure …

TypeError: nock(…).persist(…).log is not a function

I am trying to get more info on why my nock mock is not right, but I can’t make the persist().log() work. Here is the test : it(“should delete online absentee bid given its id”, () => { …

Testing Cross Browser Extension With Jest, How To Mock Chrome Storage API?

After putting off testing for a while now due to Cypress not allowing visiting chrome:// urls, I decided to finally understand how to unit/integration test my extension – TabMerger. This comes after …

Mocking methods of a JavaScript object created within a function

I’ve written a JavaScript function that creates an object from a require()’d library, and then uses it. That seems to be causing me trouble when I try to write tests for it because I don’t seem to …

Using Spies and Mocks on complex objects with Jest

I’m fairly new to testing and writing tests for a currently uncovered javaScript codebase using Jest. The code covers some niche use cases, as its conditionally injected and executed by the browser during page load. Anyway, I’m having issues mocking up custom objects. Here’s the function in question: As far as I understand, I need to mock both window.location.pathname to return a string, and I need to mock window.TOOL.cookie.setCookie() as a mock function. Here’s my attempt at the test: The test fails, saying that window.TOOL.cookie.setCookie was called 0 times. I’ve dug into the process, and found that window.location.pathname is executing

How to determine if JEST is running the code or not?

I am creating a JS test on my react-native project. I’m specifically using firebase for react native, in which I would like to replace firebase instance with a mockfirebase instance if JS is running the code of my class. For example I have class setup like below. I’d like to have a check if jest is the running environment then I’d replace the import line with appropriate mock class. Answer jest sets an environment variable called JEST_WORKER_ID so you check if this is set: I also see that if NODE_ENV is not set the jest CLI sets it to the

Getting `TypeError: jest.fn is not a function`

I’m trying to create the following unit test using Jest. jest.dontMock(“pointsAwardingActions.js”); describe(“points awarding actions”, () => { describe(“award points&…

How to mock Push notification native module in React native jest tests?

When using the module react-native-push-notification, I had this error: I tried to mock the module by creating __mocks__/react-native.js and putting this code within it: Now, I have this error: How I could mock fully this module the right way? Answer I mocked the module PushNotificationIOS by creating a setup file jest/setup.js: I’ve configured jest to run this setup file by adding this line into packages.json: