Jest and React testing library are installed as a part of create-react-app
npx create-react-app my-app --template typescript npm i -D eslint-plugin-testing-library eslint-plugin-jest-dom @testing-library/user-event Add extension in the VS code - ESLint
"eslintConfig": { "plugins": [ "testing-library", "jest-dom" ], "extends": [ "react-app", "react-app/jest", "plugin:testing-library/react", "plugin:jest-dom/recommended" ] },
"eslint.validate": [ { "language": "javascriptreact", "autoFix": false }, { "language": "typescript", "autoFix": false }, { "language": "typescriptreact", "autoFix": false }, ... ], "[typescript]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, "[typescriptreact]": { "editor.defaultFormatter": "esbenp.prettier-vscode" },
userEvent.click(colorButton);
screen.getByRole('button', { name: 'Change to blue'}) // name = displayed text
await find...
await findAllByRole
test('Displays image for each scoop option from server', async () => { render(); // Find images const scoopImages = await screen.findAllByRole('img', { name: /scoop$/i }); expect(scoopImages).toHaveLength(2); // Confirm alt text of images // @ts-ignore const altText = scoopImages.map(element => element.alt); expect(altText).toEqual(['Chocolate scoop', 'Vanilla scoop']); });
await waitFor(async () => { const alerts = await screen.findAllByRole('alert'); expect(alerts).toHaveLength(2); const alertsTexts = await screen.findAllByText( /An unexpected error occurred. Please try again later./i ); expect(alertsTexts).toHaveLength(2); });
command
[All]
QueryType