jest-fetch-mock

Unable to mock API request

喜夏-厌秋 提交于 2021-01-28 00:40:08
问题 Having a really hard time setting up jest-test-mock in in my TypeScript project. I was wondering if someone could point me in the right direction? I've got a function that looks like this: retrieveData.ts import fetch from 'cross-fetch'; export async function retrieveData({ endpoint, configuration, auth }: dataInterface): Promise<object> { try { console.log('Fetching the requested data... 📦') const settings = configuration ? JSON.parse(render(configuration || '', auth)) : {} if (settings.body

Jest TypeError: is not a constructor in Jest.mock

纵然是瞬间 提交于 2020-07-10 12:13:44
问题 I am trying to write a unit test case using jest and need to mock the below pattern . I am getting TypeError: is not a constructor. Usecase : My usecase is as mentioned below MyComponent.js : import serviceRegistry from "external/serviceRegistry"; serviceRegistry.getService("modulename", "servvice").then( service => { let myServiceInstance = new service(); myServiceInstance.init(p,d) }) Mycomponent.spec.js jest.mock('external/serviceRegistry', () => { return { getService: jest.fn(() =>

Jest TypeError: is not a constructor in Jest.mock

久未见 提交于 2020-07-10 12:11:34
问题 I am trying to write a unit test case using jest and need to mock the below pattern . I am getting TypeError: is not a constructor. Usecase : My usecase is as mentioned below MyComponent.js : import serviceRegistry from "external/serviceRegistry"; serviceRegistry.getService("modulename", "servvice").then( service => { let myServiceInstance = new service(); myServiceInstance.init(p,d) }) Mycomponent.spec.js jest.mock('external/serviceRegistry', () => { return { getService: jest.fn(() =>

Jest TypeError: is not a constructor in Jest.mock

≡放荡痞女 提交于 2020-07-10 12:08:08
问题 I am trying to write a unit test case using jest and need to mock the below pattern . I am getting TypeError: is not a constructor. Usecase : My usecase is as mentioned below MyComponent.js : import serviceRegistry from "external/serviceRegistry"; serviceRegistry.getService("modulename", "servvice").then( service => { let myServiceInstance = new service(); myServiceInstance.init(p,d) }) Mycomponent.spec.js jest.mock('external/serviceRegistry', () => { return { getService: jest.fn(() =>

Testing a function that returns an object with a function - JavaScript (Jest)

别说谁变了你拦得住时间么 提交于 2020-03-05 06:02:11
问题 I have a situation where I am facing an issue regarding unit testing of a function. There is a function SOP3loginConfig which returns an object, within that object we have a function isSOP3 that returns a boolean, I want to test this function and cover the test part of it. The actual implementation of the function sop3login.ts export const SOP3loginConfig = (props: IVariables) => { const { i18n } = props; return { buttonLabel: props.user != null ? i18n('StartJourney') : i18n('logIn'),

React Native + Enzyme + Jest: Mocked redux function call is not registered / is called zero times

喜你入骨 提交于 2020-01-16 09:45:29
问题 I'm writing an app using React Native and Redux. I am designing a login form and want to test the components handle submit function. Within the handleSubmit() functions several actions should be dispatched to Redux. Let me give you the handleSubmit() functions code and the tests for it. First the function itself: handleSubmit = (values, formikBag) => { formikBag.setSubmitting(true); const { loginSuccess, navigation, setHouses, setCitizens } = this.props; apiLoginUser(values.email, values

React Native: Simulate offline device in Jest unit test

ぃ、小莉子 提交于 2019-12-01 22:36:53
问题 I'm writing a React Native app and I'm using Jest to unit test my code. I've written a function than checks if there is an internet connection. I know want to write it's unit tests. I'm stuck, because I can't figure out how to mock the connection state of the device in a unit test. How would you simulate that the device is offline or online in a unit test? Edit: Here is the function: import { NetInfo } from "react-native"; import { NO_NETWORK_CONNECTION } from "../../../../config/constants

React Native: Simulate offline device in Jest unit test

家住魔仙堡 提交于 2019-12-01 18:53:13
I'm writing a React Native app and I'm using Jest to unit test my code. I've written a function than checks if there is an internet connection. I know want to write it's unit tests. I'm stuck, because I can't figure out how to mock the connection state of the device in a unit test. How would you simulate that the device is offline or online in a unit test? Edit: Here is the function: import { NetInfo } from "react-native"; import { NO_NETWORK_CONNECTION } from "../../../../config/constants/errors"; const checkNetwork = (): Promise<boolean | string> => new Promise((resolve, reject) => { NetInfo