2018-09-22 03:44:43 +00:00
|
|
|
# TestRunner
|
|
|
|
|
|
|
|
This test runner is used internally by Puppeteer to test Puppeteer itself.
|
2017-12-08 00:37:22 +00:00
|
|
|
|
2018-09-21 21:51:22 +00:00
|
|
|
- testrunner is a *library*: tests are `node.js` scripts
|
2017-12-08 00:37:22 +00:00
|
|
|
- parallel wrt IO operations
|
|
|
|
- supports async/await
|
|
|
|
- modular
|
|
|
|
- well-isolated state per execution thread
|
|
|
|
|
2018-09-21 21:51:22 +00:00
|
|
|
### Installation
|
|
|
|
|
|
|
|
```sh
|
|
|
|
npm install --save-dev @pptr/testrunner
|
|
|
|
```
|
|
|
|
|
|
|
|
### Example
|
|
|
|
|
|
|
|
Save the following as `test.js` and run using `node`:
|
|
|
|
|
|
|
|
```sh
|
|
|
|
node test.js
|
|
|
|
```
|
2017-12-08 00:37:22 +00:00
|
|
|
|
|
|
|
```js
|
2018-09-21 21:51:22 +00:00
|
|
|
const {TestRunner, Reporter, Matchers} = require('@pptr/testrunner');
|
2017-12-08 00:37:22 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2018-09-21 21:51:22 +00:00
|
|
|
// Test hooks can be async.
|
|
|
|
beforeAll(async state => {
|
2017-12-12 21:34:21 +00:00
|
|
|
state.parallelIndex; // either 0 or 1 in this example, depending on the executing thread
|
2017-12-08 00:37:22 +00:00
|
|
|
state.foo = 'bar'; // set state for every test
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('math', () => {
|
|
|
|
it('to be sane', async (state, test) => {
|
2018-04-10 19:25:14 +00:00
|
|
|
state.parallelIndex; // Very first test will always be ran by the 0's thread
|
2017-12-08 00:37:22 +00:00
|
|
|
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();
|
|
|
|
```
|
2018-09-21 21:51:22 +00:00
|
|
|
|