jestjs

Is done required in async Jest tests?

半世苍凉 提交于 2021-02-20 09:42:07
问题 I'm having an argument with a co-worker about done() in Jest tests. He's orders of magnitude more experienced with JavaScript than I am, and I came in after async / await was generally accepted, plus came from a .NET environment, so I was used to it. I write my tests like this: it("should return 200 OK for POST method", async () => { await request(app).post("SOMEENDPOINT") .attach("file", "file") .expect(200); }); He's more used to promises so will write his tests like this: it("should return

Date mock with two or more tests

拜拜、爱过 提交于 2021-02-19 08:17:07
问题 We are using jest for mocking. I have a function which will greet us based on the time that file looks like below: export default function getGreetingMessage() { const today = new Date(); const curHr = today.getHours(); if (curHr < 12) { return 'Good morning'; } else if (curHr < 18) { return 'Good afternoon'; } return 'Good evening'; } And My test file will look like below import getGreetingMessage from '../messages'; describe('messages', () => { function setup(date) { const DATE_TO_USE = new

Testing parent methods in stateless child component in Jest

假如想象 提交于 2021-02-19 07:35:14
问题 I currently have a react component with an onChange method. I want to test the onChange method like if someone were to type in, or paste things in. The problem is the handleChange is dependent on the parent components using it. What's the best way to test for the simulation? Do I need to reference the parent component's handleChange method and simulate that? CodeSandbox (i didn't add the tests here) https://codesandbox.io/s/react-basic-example-cwdh1?from-embed Updated Current Test: const

how to mock npm uniqid for react unit test when using Jest?

走远了吗. 提交于 2021-02-19 07:10:45
问题 I'm trying to mock npm uniqid library for react unit test. I have used this uniqid for check box. This is for component render test. 回答1: You can mock it like this: jest.mock('uniqid', ()=>(i)=> i + 'someUniqId') or if you use one of the other functions jest.mock('uniqid', () = > ({ process: (i) => i + 'someProcess, time: (i) => i + ' someTime, })) 来源: https://stackoverflow.com/questions/44537755/how-to-mock-npm-uniqid-for-react-unit-test-when-using-jest

how to mock npm uniqid for react unit test when using Jest?

自作多情 提交于 2021-02-19 07:10:36
问题 I'm trying to mock npm uniqid library for react unit test. I have used this uniqid for check box. This is for component render test. 回答1: You can mock it like this: jest.mock('uniqid', ()=>(i)=> i + 'someUniqId') or if you use one of the other functions jest.mock('uniqid', () = > ({ process: (i) => i + 'someProcess, time: (i) => i + ' someTime, })) 来源: https://stackoverflow.com/questions/44537755/how-to-mock-npm-uniqid-for-react-unit-test-when-using-jest

How to test async function with spyOn?

半腔热情 提交于 2021-02-19 02:57:09
问题 I am trying to test an async function in a react native app. class myClass extends React.Component { ... closeModal = async () => { if (someCondition) { await myFunction1(); } else { await myFunction2(); } this.props.navigation.state.params.onGoBack(); this.props.navigation.navigate('Main'); }; ... } This is my test: const navigation = { navigate: jest.fn(), state: { params: { onGoBack: jest.fn() } }, }; const renderComponent = overrides => { props = { navigation, ...overrides, }; return

How to test async function with spyOn?

别来无恙 提交于 2021-02-19 02:57:09
问题 I am trying to test an async function in a react native app. class myClass extends React.Component { ... closeModal = async () => { if (someCondition) { await myFunction1(); } else { await myFunction2(); } this.props.navigation.state.params.onGoBack(); this.props.navigation.navigate('Main'); }; ... } This is my test: const navigation = { navigate: jest.fn(), state: { params: { onGoBack: jest.fn() } }, }; const renderComponent = overrides => { props = { navigation, ...overrides, }; return

Jest error when set or assign window.location

扶醉桌前 提交于 2021-02-18 12:38:08
问题 Upgraded Jest library on v25 and all unit tests that were checking location change are failing. I checked a couple of issues opened on jest repo but I didn't actually understood how can this be fixed. The code that calls location.assign breaks with following error: Error: Not implemented: navigation (except hash changes) 69 | // window.location.href = url; > 70 | window.location.assign(url); I suppose that Jest jsdom window object shouldn't be treated anymore like a real browser window in

mocking store.getState()

旧街凉风 提交于 2021-02-18 11:42:08
问题 I want to assert that when a function gets my redux state value using store.getState() , it does various things based on the conditions of that state. How am I able to assert / mock what I want the state value to be for certain tests using the store.getState() method? Thanks. sampleFunction.js: import { store } from './reduxStore'; const sampleFunction = () => { const state = store.getState(); let result = false; if (state.foo.isGood) { result = true; } return result; }; export default

What is the purpose of `beforeEach` global in Jest?

狂风中的少年 提交于 2021-02-18 10:16:06
问题 I always find Jest-Enzyme test cases starting with a beforeEach global that resembles like the following: describe('TEST BLOCK' () => { let wrapper; beforeEach(() => { wrapper = shallow(<Component />); )); )); The function inside the beforeEach global runs before each test inside the TEST BLOCK describe block. In this case, it shallow renders the Component and assigns to wrapper before running each test. I am not quite sure why do we do this in the first place. Are we not deliberatly slowing