Giter VIP home page Giter VIP logo

to-do-list-testing-library's Introduction

testing-library/react

screen query methods:

  • getBy: default for getting element. If no element found - error
  • findBy vs getBy: findBy - can be used with async/await
  • findBy - good for testing scenarios, where http API calls are made (e.g. with axios)
  • queryBy vs getBy: findBy - if no match - it returns "null" instead of an error
  • queryBy - good for testing scenarios, where element has disappeared or check if it exists

Best Practices

  1. Install and use the ESLint plugin for Testing Library:
    • eslint-plugin-testing-library
    • eslint-plugin-jest-dom
  1. don't use cleanup: it happens automatically now
  2. use screen for querying and debugging - you no longer need to keep the render call destructure up-to-date as you add/remove the queries you need
  3. use act where necessary: render and fireEvent are already wrapped in act
  4. try to avoid using container for querying elements
  5. Only use the query* variants for asserting that an element cannot found
  6. Use @testing-library/user-event over fireEvent where possible - type call, will trigger keyDown, keyPress, and keyUp events for each character during typing.
  7. use find* any time you want to query for something that may not be available right away, but not waitFor

When you have no other options:

  1. Go up to the parent:
    const element = screen.queryByText(topicItem.title).closest('a');

Mocks

React resets mocks every time. Inside node_modules/react-scripts/util
We have "createJestConfig" file
To change it, change in file: "resetMock: false"

If you don't create custom jest config, and run it with "jest runner", then default one from "react-scripts" will be used

Some use cases:

react-testing-library renders into a

by default but you can render your component inside another element instead

wrap rendered component into

const tableRow = document.createElement('tbody');
render(<OrderComponent order={testOrder} />, {
    container: document.body.appendChild(tableRow)
});

Jest

jest.fn() - mock a f() jest.mock() - mock a whole module

e.g.:

jest.mock('axios');

to-do-list-testing-library's People

Contributors

goncharigor avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.