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 } = ..

Read more

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 ..

Read more

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..

Read more

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 ..

Read more

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 ..

Read more

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 ..

Read more

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’, }, { ..

Read more