Filtering Tests
Lupa offers several filtering layers to find and run an individual or group of tests. However, an excellent filtering experience starts with an organized test suite. Therefore, we will also discuss common ways to structure the tests properly.
Using suites
Using test suites is the first step towards dividing your tests by their nature. You should create a separate test suite for unit tests, component tests, browser/e2e tests, and so on.
A great exercise is creating test suites with the resources they need. Also, when you run tests for a specific suite, Lupa will not import test files from other suites, which may result in significantly faster execution time.
# Filter tests by suite name
npx lupa -s components
npx lupa -s functional
npx lupa --suites components,e2eOrganize using folders
Instead of creating multiple test groups within a single file, you must create nested folders and files to organize your tests.
An example folder structure for organizing tests by component -> behavior -> scenario follows:
.
├── components
│ └── button
│ ├── primary.spec.ts
│ └── secondary.spec.tsYou can run tests from a specific file or folder using positional arguments or the --files flag:
Run tests from any file matching "primary":
npx lupa primary
# Or using the flag:
npx lupa --files="primary"Run tests from any file matching "primary" inside the button folder:
npx lupa "button/primary"
# Or using the flag:
npx lupa --files="button/primary"Tagging tests
Tagging your tests is a great way to filter tests across multiple test suites and files.
For example, you might have tests interacting with an external mock API spread across multiple files and folders. Instead of applying multiple filters based on the filenames, you can tag these tests using the test.tags method and filter them by the tag name.
test('add payment method', () => {
// ...
}).tags(['@api_mock'])
test('charge user and create order', () => {
// ...
}).tags(['@api_mock'])You can run tests with the @api_mock tag as follows:
npx lupa --tags="@api_mock"You can ignore tests with the @api_mock tag by negating it with a tilde ~ symbol:
npx lupa --tags="~@api_mock"You can also specify multiple tags using the --tags filter:
npx lupa --tags="@api_mock,@slow"When filtering for multiple tags, all the tests containing any mentioned tags will run. You must use the --match-all flag if you want to run tests that have all the mentioned tags:
npx lupa --tags="@api_mock,@slow" --match-allFiltering by group title
You can filter tests by the group title using the --groups filter. The filter accepts the exact title (a substring will not work).
import { test } from '@pawel-up/lupa/testing'
test.group('polls list', () => {
test('show list of public polls', () => {})
})npx lupa --groups="polls list"Filtering by test title
You can filter tests by the test title using the --tests filter. The filter accepts the exact title (a substring will not work).
import { test } from '@pawel-up/lupa/testing'
test('show list of public polls', () => {})npx lupa --tests="show list of public polls"Pinning tests
Pinning tests is exceptionally helpful when you want to debug an individual or a group of tests for the time being. You can pin a test using the test.pin() method, and Lupa will only run the pinned tests and immediately skip everything else.
import { test } from '@pawel-up/lupa/testing'
test.group('polls list', () => {
test('show list of public polls', () => {})
test('show list of participating polls', () => {
// Lupa will only execute this test
}).pin()
})Listing pinned tests
You can view the list of all the currently pinned tests using the --list-pinned CLI flag. The output will contain the test title and its source code location.
npx lupa --list-pinnedRunning failed tests
You can automatically run only the tests that failed during the last run using the --failed CLI flag. It will run all the tests if there are no failing tests currently recorded.
npx lupa --failed