How to test method called in promise

I have a function that saves an entity and in the promise i want to check the delete is called. In my jest test it whizzes past and can’t think how to test it Here is the partial snippets, it all …

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(…

Jest: How to undo a global mock for certain tests in a file

I want to mock Math.random for certain tests and use its original implementation for other tests. How can I achieve this? I’ve read about using jest.doMock and jest.dontMock, but I’ve bumped into a …

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 …

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 …

String concatenation returning undefined on Jest test snapshot

I have the following react component import styles from ‘./Alert.module.scss’; const Alert = ({ role = ‘document’, type = ‘info’, }) => (

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())….

Jest testcase breaking in React Native Expo

Following is the error I receive on running a Sample testcase: Expected react-native/jest-preset to define transform[^.+.(bmp|gif|jpg|jpeg|mp4|png|psd|svg|webp)$] react-native/jest-preset contained …

Jest equivalent to RSpec lazy evaluated variables (let)?

In rspec you can do something like this: This allows you to define a method call or instantiation of a large object as a sum of its smaller parts. You can then override those individual small parts inside different contexts. The idea being that you create a happy path before each test, and then specify deviations from the happy path within your context blocks. Unfortunately, I can’t seem to do this with Jest. I’ve tried the following: Because jest executes every describe block before running any specific describe block, input is always ‘baz’. Does anyone know a work around, or

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