Defining Tests
This guide covers how to define and organize tests in Harness using test functions, test suites, and lifecycle hooks.
The following types are used in the type signatures below:
When a test function returns a promise, the runner will wait until it is resolved to collect async expectations. If the promise is rejected, the test will fail.
Platform-Specific Test Files
Harness test files normally run on every selected runner. If a file should only run on one platform, add the runner platform ID before .harness:
When you run Harness with --harnessRunner ios, files such as *.android.harness.ts and *.web.harness.ts are filtered out before Jest schedules them. Shared files such as smoke.harness.ts still run on every platform.
The platform segment is recognized only when it matches a platformId from one of your configured runners. Unknown segments are treated as part of the regular file name, so custom.foo.harness.ts still behaves like a shared Harness test unless foo is a configured platform ID.
Test Functions
test
- Alias:
it
test defines a set of related expectations. It receives the test name and a function that holds the expectations to test.
Test callbacks always receive a HarnessTestContext object at runtime. You can ignore the parameter when you do not need it.
The context also lets you dynamically skip a test and register lifecycle callbacks that run after the test finishes or fails.
test.skip
- Alias:
it.skip
If you want to skip running certain tests, but you don't want to delete the code due to any reason, you can use test.skip to avoid running them.
test.only
- Alias:
it.only
Use test.only to only run certain tests in a given suite. This is useful when debugging.
test.todo
- Alias:
it.todo
Use test.todo to stub tests to be implemented later. These tests will be reported as pending in the test results.
Test Suites
describe
describe creates a block that groups together several related tests.
describe.skip
Skip an entire describe block.
describe.only
Run only this describe block (and others marked with only).
Setup and Teardown
These functions allow you to hook into the life cycle of tests to avoid repeating setup and teardown code. They apply to the current describe block.
beforeEach
Register a callback to be called before each of the tests in the current context runs.
beforeEach receives the same HarnessTestContext object as the test callback, so you can inspect task metadata or dynamically skip from setup code.
afterEach
Register a callback to be called after each one of the tests in the current context completes.
afterEach also receives HarnessTestContext, which is useful for cleanup keyed to the current task.
beforeAll
Register a callback to be called once before starting to run all tests in the current context.
Unlike test, beforeEach, and afterEach, beforeAll does not receive a test context because it runs outside any single test case.
afterAll
Register a callback to be called once after all tests have run in the current context.
Like beforeAll, afterAll does not receive a test context.
Important Notes
- All test functions (
test,describe, lifecycle hooks) must be called within adescribeblock in Harness - Tests run synchronously by default - use
async/awaitfor asynchronous operations - Import all testing functions from
react-native-harness
