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 the many times that I had to manually test the ever growing functionality and in some cases forgot to check a thing or two. Having automated testing will
Tag: mocking
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 have a good way to gain control over that object and create mocks of its methods to test the behavior of my function.
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
How to mock libraries that are unknown in Javascript on my local environment?
I am developing a website using Javascript for a device which has a particular Javascript library that can be used. This Javascript library is known on the device but it is not known locally. F.e. I execute library.function(). For this, locally I get the error: library is not defined. Is there a way to avoid this error locally so that
How to return based on actual arguments being sent?
Let’s say we have a function with two arguments. It’s being called many times with different arguments on every call. So, its impossible to stub it with withArgs option. I want to write a stub function which will check the actual arguments being passed (args1, args2) and return a static response with a switch case. Something on the following lines:
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
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 change the behaviour of a mocked import?
I am quite confused with mocking in Jest an how to unit test the implementations. The thing is i want to mock different expected behaviours. Is there any way to achieve this? as imports can be only on the top of the file and to be able to mock something it must be declared before the import. I have also
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