chore: migrate unit tests to Mocha (#5600)

Rather than maintain our own test runner we should instead lean on the community and use Mocha which is very popular and also our test runner of choice in DevTools too.

Note that this commit doesn't remove the TestRunner source as it's still used for other unit tests, but they will be updated in a future PR and then we can remove the TestRunner.

The main bulk of this PR is updating the tests as the old TestRunner passed in contextual data via the `it` function callback whereas Mocha does not, so we introduce some helpers for the tests to make it easier.
This commit is contained in:
Jack Franklin
2020-04-09 06:56:25 +01:00
committed by GitHub
parent 262da92bbb
commit 17cd8703f9
43 changed files with 4159 additions and 2812 deletions

View File

@@ -16,78 +16,97 @@
const fs = require('fs');
const path = require('path');
const expect = require('expect');
const {getTestState} = require('./mocha-utils');
module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, puppeteer}) {
const {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
describeChromeOnly('Tracing', function() {
let outputFile;
let browser;
let page;
describe('Tracing', function() {
beforeEach(async function(state) {
state.outputFile = path.join(__dirname, 'assets', `trace-${state.parallelIndex}.json`);
state.browser = await puppeteer.launch(defaultBrowserOptions);
state.page = await state.browser.newPage();
});
afterEach(async function(state) {
await state.browser.close();
state.browser = null;
state.page = null;
if (fs.existsSync(state.outputFile)) {
fs.unlinkSync(state.outputFile);
state.outputFile = null;
}
});
it('should output a trace', async({page, server, outputFile}) => {
await page.tracing.start({screenshots: true, path: outputFile});
await page.goto(server.PREFIX + '/grid.html');
await page.tracing.stop();
expect(fs.existsSync(outputFile)).toBe(true);
});
it('should run with custom categories if provided', async({page, outputFile}) => {
await page.tracing.start({path: outputFile, categories: ['disabled-by-default-v8.cpu_profiler.hires']});
await page.tracing.stop();
/* we manually manage the browser here as we want a new browser for each
* individual test, which isn't the default behaviour of getTestState()
*/
const traceJson = JSON.parse(fs.readFileSync(outputFile));
expect(traceJson.metadata['trace-config']).toContain('disabled-by-default-v8.cpu_profiler.hires');
});
it('should throw if tracing on two pages', async({page, server, browser, outputFile}) => {
await page.tracing.start({path: outputFile});
const newPage = await browser.newPage();
let error = null;
await newPage.tracing.start({path: outputFile}).catch(e => error = e);
await newPage.close();
expect(error).toBeTruthy();
await page.tracing.stop();
});
it('should return a buffer', async({page, server, outputFile}) => {
await page.tracing.start({screenshots: true, path: outputFile});
await page.goto(server.PREFIX + '/grid.html');
const trace = await page.tracing.stop();
const buf = fs.readFileSync(outputFile);
expect(trace.toString()).toEqual(buf.toString());
});
it('should work without options', async({page, server, outputFile}) => {
await page.tracing.start();
await page.goto(server.PREFIX + '/grid.html');
const trace = await page.tracing.stop();
expect(trace).toBeTruthy();
});
it('should return null in case of Buffer error', async({page, server}) => {
await page.tracing.start({screenshots: true});
await page.goto(server.PREFIX + '/grid.html');
const oldBufferConcat = Buffer.concat;
Buffer.concat = bufs => {
throw 'error';
};
const trace = await page.tracing.stop();
expect(trace).toEqual(null);
Buffer.concat = oldBufferConcat;
});
it('should support a buffer without a path', async({page, server}) => {
await page.tracing.start({screenshots: true});
await page.goto(server.PREFIX + '/grid.html');
const trace = await page.tracing.stop();
expect(trace.toString()).toContain('screenshot');
});
beforeEach(async() => {
const {defaultBrowserOptions, puppeteer} = getTestState();
browser = await puppeteer.launch(defaultBrowserOptions);
page = await browser.newPage();
outputFile = path.join(__dirname, 'assets', 'trace.json');
});
};
afterEach(async() => {
await browser.close();
browser = null;
page = null;
if (fs.existsSync(outputFile)) {
fs.unlinkSync(outputFile);
outputFile = null;
}
});
it('should output a trace', async() => {
const { server} = getTestState();
await page.tracing.start({screenshots: true, path: outputFile});
await page.goto(server.PREFIX + '/grid.html');
await page.tracing.stop();
expect(fs.existsSync(outputFile)).toBe(true);
});
it('should run with custom categories if provided', async() => {
await page.tracing.start({path: outputFile, categories: ['disabled-by-default-v8.cpu_profiler.hires']});
await page.tracing.stop();
const traceJson = JSON.parse(fs.readFileSync(outputFile));
expect(traceJson.metadata['trace-config']).toContain('disabled-by-default-v8.cpu_profiler.hires');
});
it('should throw if tracing on two pages', async() => {
await page.tracing.start({path: outputFile});
const newPage = await browser.newPage();
let error = null;
await newPage.tracing.start({path: outputFile}).catch(e => error = e);
await newPage.close();
expect(error).toBeTruthy();
await page.tracing.stop();
});
it('should return a buffer', async() => {
const { server } = getTestState();
await page.tracing.start({screenshots: true, path: outputFile});
await page.goto(server.PREFIX + '/grid.html');
const trace = await page.tracing.stop();
const buf = fs.readFileSync(outputFile);
expect(trace.toString()).toEqual(buf.toString());
});
it('should work without options', async() => {
const { server } = getTestState();
await page.tracing.start();
await page.goto(server.PREFIX + '/grid.html');
const trace = await page.tracing.stop();
expect(trace).toBeTruthy();
});
it('should return null in case of Buffer error', async() => {
const { server } = getTestState();
await page.tracing.start({screenshots: true});
await page.goto(server.PREFIX + '/grid.html');
const oldBufferConcat = Buffer.concat;
Buffer.concat = bufs => {
throw 'error';
};
const trace = await page.tracing.stop();
expect(trace).toEqual(null);
Buffer.concat = oldBufferConcat;
});
it('should support a buffer without a path', async() => {
const { server } = getTestState();
await page.tracing.start({screenshots: true});
await page.goto(server.PREFIX + '/grid.html');
const trace = await page.tracing.stop();
expect(trace.toString()).toContain('screenshot');
});
});