News Articles mocking

How to preserve working manual mocks in Jest and the new resetMocks default in CRA?

The resetMocks: true default in create-react-app (CRA) version 4.0.0 (see release notes) breaks manual mocks that have mocks inside them. In that situation, Jest will throw the error: mockConstructor(…): Nothing was returned from render. This GitHub issue for CRA (dated Oct 2020) contains a discussion of the pros and cons of the new resetMocks default, […]

How to mock a function inside a nested ES6 imports using jest?

I have following two modules. // exchange.ts export async function fetchBalances(exchangeIds: string[]): Promise<ExchangeBalance> { /* … */} // normalizer.ts import { fetchBalances } from ‘./exchange’; export async function scaleFactor() { const balances = await fetchBalances([…]); // …. } I am trying to mock fetchBalances of exchange when testing normalizer as follows: // normalizer.test.ts beforeEach(() => […]

How to mock .then() and .catch() in Jest?

can anyone please give me an example of how can I mock .then() block and .catch() block in Jest I am trying to test AWS cognito.signUp().then().catch() but returning TypeError: cognito.signUp(…).then(…).catch is not a function Source: JavaSript – Stack Overflow

How to mock react component for only one test with jest

I need to mock component only for one test. So I require actual component for every test but in a specific I’m trying to return mocked value. But in such way I’m getting TypeError: specificMockImpl.apply is not a function. How can I fix this? import { render } from ‘@testing-library/react’; import App from ‘./App’; import […]

How to mock react component for only one test with jest

I need to mock component only for one test. So I require actual component for every test but in a specific I’m trying to return mocked value. But in such way I’m getting TypeError: specificMockImpl.apply is not a function. How can I fix this? import { render } from ‘@testing-library/react’; import App from ‘./App’; import […]

How to mock react component for only one test with jest

I need to mock component only for one test. So I require actual component for every test but in a specific I’m trying to return mocked value. But in such way I’m getting TypeError: specificMockImpl.apply is not a function. How can I fix this? import { render } from ‘@testing-library/react’; import App from ‘./App’; import […]

How to mock react component for only one test with jest

I need to mock component only for one test. So I require actual component for every test but in a specific I’m trying to return mocked value. But in such way I’m getting TypeError: specificMockImpl.apply is not a function. How can I fix this? import { render } from ‘@testing-library/react’; import App from ‘./App’; import […]

How to mock react component for only one test with jest

I need to mock component only for one test. So I require actual component for every test but in a specific I’m trying to return mocked value. But in such way I’m getting TypeError: specificMockImpl.apply is not a function. How can I fix this? import { render } from ‘@testing-library/react’; import App from ‘./App’; import […]

How to mock react component for only one test with jest

I need to mock component only for one test. So I require actual component for every test but in a specific I’m trying to return mocked value. But in such way I’m getting TypeError: specificMockImpl.apply is not a function. How can I fix this? import { render } from ‘@testing-library/react’; import App from ‘./App’; import […]

Jest, mocking ES6 class constructor not working as intended

I have code that I am trying to test with Jest. I have an ES6 class, let’s call it ClassA. Within my test file, I am mocking ClassA as follows: const mockGetCheapestProductAllPages = jest.fn() const ClassA = require(‘../src/ClassA/ClassA’) jest.mock(‘../src/ClassA/ClassA’, () => { return jest.fn().mockImplementation(() => { return { getCheapestProductAllPages: mockGetCheapestProductAllPages } }) }) This syntax […]

React Testing Library – mocking a function

I am trying to test a function gets called import React from ‘react’; import { render, cleanup, fireEvent } from ‘@testing-library/react’; import ‘@testing-library/jest-dom’; import MyForm from ‘./MyForm’; afterEach(cleanup); const testFunction = jest.fn(); test(‘my form’, () => { const { getByTestId } = render(<MyForm />); const myButton = getByTestId(‘submit-button’); expect(myButton.tagName).toBe(‘BUTTON’); fireEvent.click(myButton) expect(testFunction).toHaveBeenCalledTimes(1) }); The problem I […]

Mocking setTimeout with jest

I have a website made with React.JS that is continuously emitting events for everything [and nothing] that is happening. For example, users typed something in the form, emit an event. Users set focus on a field and do not do anything for some time, we again emit an event. The idea is to understand customers’ […]

using stubby multiple responses for the same patch request endpoint

This is my patch request mapping in data.yaml request: url: ^/api/test method: PATCH headers: Content-Type: application/json response: headers: Content-Type: application/json status: 200 file: response/test-1.json api path api/test is a PATCH request which accepts a single request parameter in it’s body {testVar: "1111"} what I need to implement is when request param is {testVar: "1111"} -> […]

Mock React child component in snapshot testing

Let’s suppose to have a very huge component named ‘HugeComponent’ and its snapshot test. This component is used in N other components. For example: import React from ‘react’; import HugeComponent from ‘./huge-component/HugeComponent’; const FirstComponent = () => { return ( <div className="container"> <h1>My First Component</h1> <p>This component renders a huge component</p> <HugeComponent /> </div> ); […]

Mock React child component in snapshot testing

Let’s suppose to have a very huge component named ‘HugeComponent’ and its snapshot test. This component is used in N other components. For example: import React from ‘react’; import HugeComponent from ‘./huge-component/HugeComponent’; const FirstComponent = () => { return ( <div className="container"> <h1>My First Component</h1> <p>This component renders a huge component</p> <HugeComponent /> </div> ); […]

Mock ReactDOM.render not being called for dynamically imported component

I’m in the process of refactoring a group of React files that render components in an identical fashion to use a common loading function, using dynamic importing. However, a mock of ReactDOM.render() that was working when the file used a static import is no longer being called, even though a console.log shows the import was […]

Mock ReactDOM.render not being called for dynamically imported component

I’m in the process of refactoring a group of React files that render components in an identical fashion to use a common loading function, using dynamic importing. However, a mock of ReactDOM.render() that was working when the file used a static import is no longer being called, even though a console.log shows the import was […]

Mock ReactDOM.render not being called for dynamically imported component

I’m in the process of refactoring a group of React files that render components in an identical fashion to use a common loading function, using dynamic importing. However, a mock of ReactDOM.render() that was working when the file used a static import is no longer being called, even though a console.log shows the import was […]

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.