Skip to content
Advertisement

Tag: jestjs

Invalidate node cache when using Jest

I have a file with object that gets populated with process.env properties: env.js Now I try to test this file with different process.env properties: env.test.js Unfortunately, even though I try to load the file in every test separately the file gets loaded only once, making the third test fail with: P.S. It doesn’t fail when I run the test alone.

Mock a dependency’s constructor Jest

I’m a newbie to Jest. I’ve managed to mock my own stuff, but seem to be stuck mocking a module. Specifically constructors. usage.js I’d like to do something like this in the tests. However when I come to use it in usage.js the cw is a mockConstructor not a FakeMetrics I realise that my approach might be ‘less than idiomatic’

Error is thrown but Jest’s `toThrow()` does not capture the error

Here is my error codes: As you can see the error did indeed occurred: Function FunctionThatDoesNotExistsInString does not exists in string.. However it is not captured as a pass in Jest. Here is my codes: Answer expect(fn).toThrow() expects a function fn that, when called, throws an exception. However you are calling CheckFunctionExistenceByStr immediatelly, which causes the function to throw before

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

I’m trying to create the following unit test using Jest. But I’m getting the following error after running npm test. TypeError: jest.fn is not a function This is some section of my package.json: What could be the reason I’m getting that error? Answer The jest object is automatically in scope within every test file, so there’s no need to import

How to mock functions in the same module using Jest?

What’s the best way to correctly mock the following example? The problem is that after import time, foo keeps the reference to the original unmocked bar. module.js: module.test.js: I could change: to: but this is pretty ugly in my opinion to do everywhere. Answer fwiw, the solution I settled on was to use dependency injection, by setting a default argument.

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

Advertisement