puppeteer/utils/testrunner
Jack Franklin 6522e4f524
chore: Use expect for assertions (#5581)
Rather than use our own custom expect library, we can use expect from npm [1], which has an API almost identical to the one Puppeteer has, but with more options, better diffing, and is used by many in the community as it's the default assertions library that comes with Jest.

It's also thoroughly documented [2].

[1]: https://www.npmjs.com/package/expect
[2]: https://jestjs.io/docs/en/expect
2020-04-03 13:22:55 +02:00
..
examples chore: Use expect for assertions (#5581) 2020-04-03 13:22:55 +02:00
test chore: Use expect for assertions (#5581) 2020-04-03 13:22:55 +02:00
.npmignore chore: add .npmignore for testrunner (#3290) 2018-09-21 17:27:10 -07:00
index.js chore: Use expect for assertions (#5581) 2020-04-03 13:22:55 +02:00
LICENSE chore: prepare testrunner to be published to npm (#3289) 2018-09-21 14:51:22 -07:00
Multimap.js tests: drop jasmine test runner (#1519) 2017-12-07 16:37:22 -08:00
package.json chore: update URLs (#5185) 2019-11-26 13:12:25 +01:00
README.md chore: Use expect for assertions (#5581) 2020-04-03 13:22:55 +02:00
Reporter.js chore(testrunner): distinguish between TERMINATED and CRASHED (#4821) 2019-08-08 15:15:09 -07:00
TestRunner.js chore(testrunner): distinguish between TERMINATED and CRASHED (#4821) 2019-08-08 15:15:09 -07:00

TestRunner

This test runner is used internally by Puppeteer to test Puppeteer itself.

  • testrunner is a library: tests are node.js scripts
  • parallel wrt IO operations
  • supports async/await
  • modular
  • well-isolated state per execution thread
  • uses the expect library from npm for assertions.

Installation

npm install --save-dev @pptr/testrunner

Example

Save the following as test.js and run using node:

node test.js
const {TestRunner, Reporter} = require('@pptr/testrunner');
const expect = require('expect')

// 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
});

// Extract jasmine-like DSL into the global namespace
const {describe, xdescribe, fdescribe} = runner;
const {it, fit, xit} = runner;
const {beforeAll, beforeEach, afterAll, afterEach} = runner;

// Test hooks can be async.
beforeAll(async state => {
  state.parallelIndex; // either 0 or 1 in this example, depending on the executing thread
  state.foo = 'bar'; // set state for every test
});

describe('math', () => {
  it('to be sane', async (state, test) => {
    state.parallelIndex; // 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();