Concepts
TDD
Test-Driven Development (TDD) involves writing test code before writing the code for a certain function. This helps ensure that the code passes the test and drives the entire development process. Benefits of TDD include reduced regression bugs, improved code quality, and increased testing coverage.
BDD
Behavior-Driven Development (BDD) enables project members, even those without coding experience, to use natural language to describe system functions and business logic. This allows for automated system testing based on these descriptions.
Example:
Scenario:
Adding a Page
Given I visit the editor page
And I see the canvas
And the canvas contains 1 page
When I click the add page button
Then the canvas contains 2 pages
Property-based testing
You can write a single test that automatically sends 1000 different inputs and checks for which input our code fails to return the correct response. You can use property-based testing with your preferred test runner (e.g. Mocha, Jest, etc.) by using libraries such as js-verify, fast-check, or testcheck (with better documentation).
Framework
JEST
JEST run test across multi files
Make sure that tests are well isolatedjest --runInBand // To run tests sequentially
test.concurrent
to indicate which ones Jest should execute concurrently with a test suite.To control the number of tests running simultaneously, use the
-maxConcurrencyOption
option. To manage the number of worker threads spawned for running tests, use the -maxWorkers
option and specify the desired number of threads.Global hooks
Jest provides two configuration options,
globalSetup
and globalTeardown
, to set up global hooks. These options can be specified in the jest.config.js
file.Assertion
Ensure the expect to run
Expect the value could change
Spy a method
Use mock to another achievement
In Jest, all stubs are spies, but not all spies are stubs.
Spies
record data related to the usage of a function without interfering in its implementation.
Stubs
record data associated with the usage of a function and change its behavior, either by providing an alternative implementation or return value.
Mocks
change a function’s behavior, but instead of just recording information about its usage, they have expectations preprogrammed.
Mock behaviour
mockClear
erases a test double’s records but keeps the double in place.
mockReset
erases a test double’s records and any canned behavior but keeps the double in place.
mockRestore
completely removes the double, restoring the original implementation.How spyOn work
when to choose mock
or spyOn
- If you are mocking an object’s property, you should probably use
jest.spyOn
.
- If you are mocking an import, you should probably use
jest.mock
.
- In case you have to use the same replacement in multiple test files, you should, ideally, use a manual mock placed on the
mocks
folder.
React Testing Library is not a test runner
local Storage
Async testings
- pass done to the second callback function parameters.
Mock
Snapshot
Mock a function but preserve others from a module
Returns the actual module instead of a mock, bypassing all checks on whether the module should receive a mock implementation or not.
Settimeout to test
use
FakeTimer
Difference between jest.doMock
and jest.mock
jest.doMock
need special set up steps. When using
babel-jest
, calls to mock
will automatically be hoisted to the top of the code block. Use this method if you want to explicitly avoid this behaviour. One example when this is useful is when you want to mock a module differently within the same fileUnit test, integration test, UI test
test()
, it()
is same. Will trow error and we need to set a new ts config file
tsconfig.jest.json
jest.config.js
jest.config.js
jest.setup.ts
.eslintrc.js
Provide eslintrc suggestions on Jest test library
.eslintrc.js
We're not running our tests in a browser; we're running them in the terminal with Node.js. Node.js does not have the DOM API that is typically included with browsers. To simulate a browser environment in Node.js, Jest uses an npm package called jsdom, which is essential for testing React components.
Using Code Coverage
npm test -- --coverage
generate the /coverage/lcov-report/index.html to give interactive information.@testing-library/dom
provides screen
, getByText
@testing-library/jest-dom
to use it we need to add (provides toBeInTheDocument
)SuperTest
testing with databases and third-party APIs.
knexjs
⇒ ./node_modules/.bin/knex migrate:make —env development initial_schemaE2E
Selenium
To communicate with the Webdriver, Selenium uses a protocol called JSON Wire. This protocol specifies a set of HTTP routes for handling different actions to be performed within a browser.
JSON Wire
To communicate with the browser, each Webdriver uses the target browser’s remote-control APIs. Because different browsers have distinct remote-control APIs, each browser demands a specific driver.
Mocha
which is exclusively a test runnerChai
which is exclusively an assertion libraryNightwatch.js
https://nightwatchjs.orgWebdriverIO
https://webdriver.ioplaywright
https://playwright.devThese tools, just like Selenium, can interface with multiple Webdrivers and, therefore, are capable of controlling real browsers. The main difference between these libraries and Selenium is that they ship with testing utilities.
Puppeteer
https://pptr.dev event-driven architecture. It control only Chrome and Chromium.Cypress
Directly interfaces with a browser’s remote-control APIs
Cypress
and Puppeteer
, unlike Selenium, can directly control a browser instance. Webpack configure and add environment
The execute order
CodeceptJS
CodeceptJS
is an end-to-end testing framework for web applications written in JavaScript. It offers a user-friendly interface for writing and running tests. CodeceptJS
is compatible with popular front-end frameworks such as AngularJS
, ReactJS
, and VueJS
, and can execute tests on different browsers, web drivers, or headless browsers. Additionally, it integrates with various test runners like Mocha
, Jest
, and Protractor
, and can be used with cloud services like BrowserStack
and SauceLabs
for cross-browser testing.Experience
When testing a component which has use createProtol
when using links
import used library
To perform a snapshot test, render the component and get back the returned value of
asFragment
. Ensure that the fragment matches the snapshot. The result of the snapshot test is a test.js.snap
file in the snapshot
folder, which is created automatically in the same directory as the test.js
file when the tests are run.Mock a module
Should put out side of descriptions.