jestjs

jest test emitting events for eventemitter objects (http)

老子叫甜甜 提交于 2021-01-29 14:38:58
问题 assume the following nodejs code const server = http.listen(8080,'127.0.0.1') .on("error", err => { // ... }) module.exports = server; how to write a test using jest to emit the http "error" event (to cover the error event handler)? 回答1: Since you create a server in module scope, the code will execute immediately when you require or import server.js . You need to stub the http.createServer before you require this module. For testing .on(error, callback) method, you should use

Why doesn't jest.spyOn() sometimes work on a Vue component's method?

独自空忆成欢 提交于 2021-01-29 14:18:02
问题 I see the changes the method makes as event handler, but jest.spyOn(wrapper.vm, 'methodName') doesn't catch the call in major cases, but in some tests it somehow works. I suspect a possible re-render (maybe, due to options argument in mount() call), that replaces the original method with the jest.spyOn() , because, if I trigger event twice in a row, toHaveBeenCalled() fulfills and tracks the call. The component: <template> <v-app-bar app color="primary" dark class="header" > <router-link :to=

Jest mock not resolving to each injected value

☆樱花仙子☆ 提交于 2021-01-29 13:39:51
问题 I'm trying to test a Firebase cloud function named myCloudFn in my functions/send.js module. My tests are in functions/test/send.spec.js : // send.js const admin = require('firebase-admin'); async function myCloudFn (email) { const authUser = await admin.auth().getUserByEmail(email); return authUser; } module.exports = { myCloudFn }; // send.spec.js const send = require('../send.js'); jest.mock('firebase-admin', () => ({ auth: () => ({ getUserByEmail: jest.fn() .mockResolvedValueOnce({ uid:

How to wrap into act(…) second/deferred/async rendering

你。 提交于 2021-01-29 10:25:44
问题 I have the following component: function MyComp(props) { const [items, setItems] = useState([]); useEffect(() => { loadItems(); }, []); async function loadItems() { const result = await axios.get(`https://example.com/api`); setItems(result.data); } return (<div>{items.map(item => ...)}</div>); } And this test produces the act warning: const items = { "data": [{"id": 1, "title": "Title1"}] }; axios.get.mockImplementationOnce(() => Promise.resolve(items)); const wrapper = mount(<MyComp />);

Create a user programatically using Firebase Auth emulator

纵饮孤独 提交于 2021-01-29 10:23:05
问题 I am trying to write jest tests using the Firebase Auth emulator and continue to receive the following CORS error. console.error Error: Headers X-Client-Version forbidden at dispatchError (/Users/me/my-project/node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:62:19) at validCORSPreflightHeaders (/Users/me/my-project/node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:99:5) at Request.<anonymous> (/Users/me/my-project/node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:367:12) at Request

TypeError “styled”: symbol is not a function

情到浓时终转凉″ 提交于 2021-01-29 08:52:59
问题 I'm running npm test and getting the following error: FAIL src/App.test.js ● Test suite failed to run TypeError: symbol is not a function at String (<anonymous>) 3 | import styled from "styled-components"; 4 | > 5 | const NoAccess = styled(NoAccess_)` | ^ 6 | width: 100px; 7 | height: 75px; 8 | `; at options (node_modules/styled-components/src/models/StyledComponent.js:265:68) at je (node_modules/styled-components/src/models/StyledComponent.js:265:68) at Object.<anonymous> (src/App.js:5:18)

Ignore HOC when testing React Components

五迷三道 提交于 2021-01-29 08:20:54
问题 I want to test a simple Material UI select form which uses a FormControl and is exported withStyles. My Test case is pretty simple, i want to assert for example that my component renders an child. I try the following assertion: expect(wrapper.find('InputLabel')).toEqual(true); However, this assertion fails simply because the InputLabel is wrapped inside WithStyles AND WithFromControlContext (see debug output) : <WithStyles(FormControl) id="my-control-id"> <WithStyles(WithFormControlContext

(Jest) ConnectionNotFoundError: Connection “default” was not found

故事扮演 提交于 2021-01-29 08:19:28
问题 I try to test a function in class who use a typeorm repository. To write my testing class, I Use this exemple on github : https://github.com/jmcdo29/testing-nestjs/blob/master/apps/typeorm-sample/src/cat/cat.service.spec.ts import { Injectable } from '@nestjs/common'; import { InjectRepository, getRepositoryToken } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { Contact } from './contact.entity'; import { CreateContactDto } from './dto/createContact.dto'; import {

I am mocking two functions exactly the same way. In one case the mock value is returned and in another case the real function is called. Why?

醉酒当歌 提交于 2021-01-29 08:14:15
问题 I have a file that exports some functions: function getNow() { console.log('real now'); return dayjs(); } function groupProducts(productInfos, now) { console.log('real group'); return productInfos.reduce((groups, productInfo) => { const groupKey = dayjs(productInfo.saleStartDate) > now ? dayjs(productInfo.saleStartDate).format('YYYY-MM-DD') : dayjs(now).format('YYYY-MM-DD'); let group = groups[groupKey]; if (!group) { group = []; // eslint-disable-next-line no-param-reassign groups[groupKey]

Did I properly mock the API Call?

半腔热情 提交于 2021-01-29 08:08:10
问题 I'm not sure if I got it right so please - feel free to provide any kind of explanation. I'm trying to write a test for the function responsible for calling the API. I know that the good practice is to mock the API function instead of calling the real API. I've done it and my test is passing but can I be really sure that I'm not calling the real API? How do I check that ? domain.js export const getCountriesData = () => { return fetch("https://restcountries.eu/rest/v2/all").then((response) =>