Test suites
Test suites allow you to organize your tests by their type. For example, you can create separate suites for unit tests and functional tests and keep their test files in dedicated sub-folders.
import { configure } from '@japa/runner'
configure({
suites: [
{
name: 'unit',
files: ['tests/unit/**/*.spec.js'],
},
{
name: 'functional',
files: ['tests/functional/**/*.spec.js'],
}
]
})
- You must not use the
files
property when you are usingsuites
. - Each suite must have a unique
name
and afiles
property to associate test files with the suite.
Run selected suites
You can run tests for a specific suite by specifying the suite name after the test file name.
In the following example, only the unit tests will run.
node bin/test.js unit
The following example will run the tests for the unit and the functional suites.
node bin/test.js unit functional
Lifecycle hooks
Like tests and groups, you can also define lifecycle hooks for the suites. For example, you can use hooks to start the HTTP server before running the tests in the functional suite.
The configure
method in the suite configuration object receives an instance of the Suite class, and you can use it to register the hooks.
configure({
suites: [
{
name: 'functional',
files: ['tests/functional/**/*.spec.js'],
configure(suite) {
suite.setup(() => {
server = createServer(handler)
server.start()
return () => server.close()
})
},
}
]
})
Configure suite groups and tests
You can drill down the layers and configure groups/tests directly using the suite instance. Imagine a plugin adding extra functionality or lifecycle hooks on every test and group.
configure(suite) {
// Top level suite tests
suite.onTest((test) => {
test.setup(() => {})
})
suite.onGroup((group) => {
group.tap((test) => {
// Tests inside a group
test.setup(() => {})
})
})
},
Running suites in parallel
You might be aware that Japa does not run tests parallelly. However, you can use the following tip to run suites parallelly.
Start by installing the concurrently package from the npm registry.
npm i -D concurrently
And register the following scripts within the package.json
file.
{
"scripts": {
"unit:tests": "node bin/test.js unit",
"functional:tests": "node bin/test.js functional",
"test": "concurrently \"npm:unit:tests\" \"npm:functional:tests\""
}
}
And finally, run the tests as usual.
npm test