Jest Unit Testing function that calls a second one that returns a promise

Edited Question with vazsonyidl suggestions applied I have to write unit tests for a function similar to this one: import {External} from ‘ExternalModule’; async functionA(){ this.functionB()….

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”, () => { …

JEST | Assert a function was called inside addEventListener callback

I have the following function: Code to test export default function main() {     const createAndAppendPTag = () => {         const p = document.createElement(‘p’);         document.body.appendChild(…

Unit test with react hook fails

I don’t understand the result I get on this unit test. I expect the second check for textField.valid to be true and instead it returns false. Below is part of the component I’m testing against: export …

Test clearing of search input field on submit (after fetching)

I want to test that a search box does calls a handler (passed as prop) with the fetched results and resets the input field afterwards. import React, { useState } from ‘react’ import Axios from ‘axios’ …

Mocking node_modules which return a function with Jest?

I am writing a typeScript program which hits an external API. In the process of writing tests for this program, I have been unable to correctly mock-out the dependency on the external API in a way …

Jest Unit test + received undefined

I am using Jest as my unit test framework. I am trying to mock third part npm “request” and executed my test cases, but i am receiving and the test fails expect(jest.fn())….

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 test es6 default values in jest

How do you test the given default parameter value in jest? An example having the module: // calculate.js module.exports = (a, b = 3) => { return a + b; } Or abit more complicated function …

How to test snapshots with Jest and new React lazy 16.6 API

I have to components imported with the new React lazy API (16.6). import React, {PureComponent, lazy} from ‘react’; const Component1 = lazy(() => import(‘./Component1’)); const Component2 = lazy(…