You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Tag tests in their names such as #E2E, #Integration, etc. Then can filter the list as needed by "tag"
Use at least 2 levels of describe nesting to group related tests and provide useful names in test output. User -> add user -> happy path -> returns id and User -> add user -> non-admin -> throws error
Test both black box (purely on specs) and white box (look at the implementation and let that inform the tests you write). White-box-testing can improve test coverage, by better testing edge cases.
Mindset: "This is so important, that we have to build an automated test suite for it. I don’t trust human testers to do the job."
Automated tests minimize faulty assumptions. They're a safety net for refactors later when the programmer doesn't know about all the previous assumptions and verbally communicated requirements.
Snapshot testing - Useful for regression protection. Assures output doesn't change for a given input. So useful for components, mock API responses, to alert if anything changes after upgrading dependencies, or code that you want to refactor that's not currently under test. But NOT useful for TDD. Also not as good as visual tests (Percy/Chromatic) for UI due to false negatives. ANY markup refactor breaks the snapshot.
When you have a jest mock function, you should use toHaveBeenCalledTimes and toHaveBeenCalledWith. Even if it's called only once, you want to make sure that it's called no more or less than one time. And note that order matters
React libraries
react-dom/test-utils (primarily useful for act and simulate, though RTL wraps act and has fireEvent, which are alternatives)
act and Simulate are the most useful pieces. For DOM queries, Enzyme and RTL below are better.
Use Cypress Testing Library so you can write accessible "testing library" style queries in Cypress.
Sometimes you'll need to force a click if the element is styled in a way that Cypress thinks hides it: cy.getByLabelText("Select role for gsparks1").click({force: true });
it.only("should find 630 users when filtered by role of Salesperson or None",()=>{cy.getByLabelText("Filter by Role").click().then(subject=>{cy.getByLabelText("Salesperson",{container: subject}).click();cy.getByLabelText("None",{container: subject}).click();});cy.getByText("630 users");});
Instructions for downloading Cypress if corp network is blocking:
Playwright - A fork of Puppeteer created by Microsoft, to focus on automated testing. A short comparison of Playwright vs Cypress. (Note that Cypress now supports multiple domains and webkit, so the post is outdated. Here's another (Playwright vs Cypress post. Note that Playwright is faster than Cypress. If you are a beginner with testing and are looking for ease of installation and usage with solid docs, then Cypress is the way to go. However, if speed matters more (it's faster, it even runs tests in parallel locally, and you can run tests against multiple browsers at the same time), you need hover or tab support, you prefer Playwright's traditional JS approach (instead of Cypress' chaining), or you need Playwright's multi-tab support, then it's the right choice.
Even simply Jest snapshots (to store the expected responses from each APIs)
Benefits:
Can reveal unused interfaces - throws an error when there's unused provider code. If no apps are consuming it, obviously the code can be deleted from the provider (the API).
My Cypress vs Playwright Benchmark
102 tests in Chrome
On Jenkins, single core:
Cypress: 4:30
Playwright: 1:58
M1 MacBook Pro (multi-core):
Cypress: 2:15
Playwright: 20 seconds!
Best Practices
#E2E
,#Integration
, etc. Then can filter the list as needed by "tag"describe
nesting to group related tests and provide useful names in test output.User -> add user -> happy path -> returns id
andUser -> add user -> non-admin -> throws error
Data
Principles
Programmer tests should - Approach comparison
More general advice
Automated Testing
npm i @types/jest
to get autocomplete. ThreadCypress vs Playwright differences
General Structure and Practices
React libraries
Testing Library Tips
npm i -D eslint-plugin-testing-library eslint-plugin-jest-dom
within
. Example and hereCypress Tips
3 ways to handle login:
More broadly, don't use the UI to build up state for each test. Instead, set it.
For example, don't actually run real login for each step. Instead, simulate it:
it.only
.run
command locally)cy.getByLabelText("Select role for gsparks1").click({force: true });
Instructions for downloading Cypress if corp network is blocking:
to download the cypress package.
set DEBUG=cypress:cli && set CYPRESS_INSTALL_BINARY=C:/temp/cypress.zip && npm install cypress
in the VSCode terminal or a cmd window
npm i @testing-library/cypress
Cypress issues
Cypress isn't perfect.
Testing Different File Types
In browser testing / Integration Testing
hover
ortab
support, you prefer Playwright's traditional JS approach (instead of Cypress' chaining), or you need Playwright's multi-tab support, then it's the right choice.Contract Testing (aka Consumer Driven Contract Testing)
Overview: https://www.linkedin.com/pulse/api-contract-testing-visual-guide-peter-thomas/
Test the client/server contract. Assure the API provides the expected responses, via tests. Here's why it's useful.
Tools:
Benefits:
Visual Testing
Performance Testing / Monitoring
Property Based Testing
Generate valid random values and feed to tests. e.g. Pass random strings and ints to a func that accepts a string and int.
Validation Testing
https://ealush.com/vest
Mutation Testing
Change code and if it doesn't fail, that means you have holes in your test coverage.
Fuzz Testing / Randomized Testing / Test Generation (like Pex for C#, or QuickCheck which popularized the idea)
Recommended Reading
Testing React components with Jest and Enzyme
Node and JS Testing Best Practices
The text was updated successfully, but these errors were encountered: