mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
e6725e15af
This patch introduces a tiny test runner to run puppeteer tests. The test runner is self-container and allows parallel (wrt IO) test execution. It will also allow us to split tests into multiple files if necessary. Comparing to the jasmine, the testrunner supports parallel execution, properly handles "unhandled promise rejection" event and signals. Comparing to ava/jest, the testrunner doesn't run multiple node processes, which makes it simpler but sufficient for our goals.
46 lines
1.2 KiB
Markdown
46 lines
1.2 KiB
Markdown
# TestRunner
|
|
|
|
- no additional binary required; tests are `node.js` scripts
|
|
- parallel wrt IO operations
|
|
- supports async/await
|
|
- modular
|
|
- well-isolated state per execution thread
|
|
|
|
Example
|
|
|
|
```js
|
|
const {TestRunner, Reporter, Matchers} = require('../utils/testrunner');
|
|
|
|
// Runner holds and runs all the tests
|
|
const runner = new TestRunner({
|
|
parallel: 2, // run 2 parallel threads
|
|
timeout: 1000, // setup timeout of 1 second per test
|
|
});
|
|
// Simple expect-like matchers
|
|
const {expect} = new Matchers();
|
|
|
|
// Extract jasmine-like DSL into the global namespace
|
|
const {describe, xdescribe, fdescribe} = runner;
|
|
const {it, fit, xit} = runner;
|
|
const {beforeAll, beforeEach, afterAll, afterEach} = runner;
|
|
|
|
beforeAll(state => {
|
|
state.parallel; // this will be set with the execution thread id, either 0 or 1 in this example
|
|
state.foo = 'bar'; // set state for every test
|
|
});
|
|
|
|
describe('math', () => {
|
|
it('to be sane', async (state, test) => {
|
|
state.parallel; // Very first test will always be ran by the 0's thread
|
|
state.foo; // this will be 'bar'
|
|
expect(2 + 2).toBe(4);
|
|
});
|
|
});
|
|
|
|
// Reporter subscribes to TestRunner events and displays information in terminal
|
|
const reporter = new Reporter(runner);
|
|
|
|
// Run all tests.
|
|
runner.run();
|
|
```
|