I have a utils.js file with a function that calls another function in the same file. Like this: // utils.js async function originalFunc (params) { … await anotherFunc(arg1, arg2) } In another file I’m using jest to test the originalFunc // utils.test.js test(‘should test orginalFunc’, async () => { const params = { arg1: ‘data1’, ..
Category : jestjs
I’ve created a very simple button component, which is importing another function. For testing (using jest and testing library) I have to mock the imported function: component import React from ‘react’ import { Button } from ‘semantic-ui-react’ import { useReviewButton } from ‘@my-project/util-review’ export const ReviewButton = () => { const { button } = ..
I’m running an app built with NestJS. I have integration tests written with the supertest library. I’m using Jest as my testing framework / test runner. My integration tests look like this. describe(‘Item controller’, () => { beforeAll(async () => { const moduleRef = await Test.createTestingModule({ imports: [AppModule], }).compile(); app = moduleRef.createNestApplication(); await app.init(); test(‘/Delete ..
I am trying to write enzyme test and would like to access the text of the following div. <div className="toolbar__contentInformation"> <div className="text smallfont ellipsis">Alex</div> <div className="text smallfont ellipsis">12</div> </div> test.js let component = React.createElement(App}); let wrapper = enzyme.enzyme.mount(component); let val = wrapper.find(‘div.toolbar__contentInformation’) //how to access text = Alex ??? Source: Ask Javascript..
I have the following service that extends nestjs Logger. @Injectable({scope:Scope.TRANSIENT}) export class LogService extends Logger { async log(message: string, context?: string): Promise<void> { super.log(message, context); super.log(message, context); super.log(message, context); super.log(message, context); super.log(message, context); } async debug(message: string, context?: string): Promise<void> { super.debug(message, context); super.debug(message, context); super.debug(message, context); super.debug(message, context); super.debug(message, context); } I have a ..
I have the following test code: import React, { useState } from ‘react’ const TestComponent = ({}) => { const [isEvenOdd, setIsEvenOdd] = useState(‘Even’) const isEven = (name) => name === ‘Even’ const doTheChange = (e) => { e.preventDefault() return (isEven(isEvenOdd) ? setIsEvenOdd(‘Odd’) : setIsEvenOdd(‘Even’)) } return ( <> <Button onClick={doTheChange} id="even-odd-button" message={`This number is ..
I’m trying to test the following alert is displayed on a component: <p role="alert"> <strong>Name</strong> is there </p> Then, my test has this: expect(screen.getByRole(‘alert’, { name: /name is there/i })).toBeInTheDocument(); However, the test fails saying the element could not be find and it shows the alert role with an empty name: alert: Name "": <p ..
I have written the following Javascript function. It is supposed to return all the unique permutations (including partial permutations) of the elements of the input array; it is coded as a generator (introduced by function* and returning values using yield), so that the values are evaluated lazily on demand. function* permute(arr) { if (arr.length == ..
I am currently having some trouble compiling a test to see whether the fetch() function of my weather application is working correctly. I have made use of the useEffect() hook to fetch the data from the OpenWeather API to store and render once the API’s URL changes. I am new to Jest testing and have ..
I’d like to know if it’s possible to mock axios requests with different values depending on url. I use jest and typescript. Now I have this working example: import axios from ‘axios’; import { mocked } from ‘ts-jest’; jest.mock(‘axios’); const mAxios = mocked(axios, true); mAxios.get.mockResolvedValue({ data: [ { id: 1, title: ‘john doe’, }, { ..
Recent Comments