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
This commit is contained in:
Jack Franklin
2020-04-03 12:22:55 +01:00
committed by GitHub
parent 4c41421dd9
commit 6522e4f524
16 changed files with 55 additions and 168 deletions

View File

@@ -16,8 +16,9 @@
const fs = require('fs');
const path = require('path');
const rm = require('rimraf').sync;
const GoldenUtils = require('./golden-utils');
const {Matchers} = require('../utils/testrunner/');
const utils = require('./utils');
const expect = require('expect');
const YELLOW_COLOR = '\x1b[33m';
const RESET_COLOR = '\x1b[0m';
@@ -59,13 +60,13 @@ module.exports.addTests = ({testRunner, product, puppeteerPath}) => {
}
const suffix = FFOX ? 'firefox' : product.toLowerCase();
const GOLDEN_DIR = path.join(__dirname, 'golden-' + suffix);
const OUTPUT_DIR = path.join(__dirname, 'output-' + suffix);
if (fs.existsSync(OUTPUT_DIR))
rm(OUTPUT_DIR);
const {expect} = new Matchers({
toBeGolden: GoldenUtils.compare.bind(null, GOLDEN_DIR, OUTPUT_DIR)
});
utils.extendExpectWithToBeGolden(GOLDEN_DIR, OUTPUT_DIR);
const testOptions = {
testRunner,

View File

@@ -27,8 +27,8 @@ module.exports.addTests = function({testRunner, expect, puppeteer}) {
// The pages will be the testing page and the original newtab page
const targets = browser.targets();
expect(targets.some(target => target.type() === 'page' &&
target.url() === 'about:blank')).toBeTruthy('Missing blank page');
expect(targets.some(target => target.type() === 'browser')).toBeTruthy('Missing browser target');
target.url() === 'about:blank')).toBeTruthy();
expect(targets.some(target => target.type() === 'browser')).toBeTruthy();
});
it('Browser.pages should return all of the pages', async({page, server, context}) => {
// The pages will be the testing page

View File

@@ -16,6 +16,8 @@
const fs = require('fs');
const path = require('path');
const expect = require('expect');
const GoldenUtils = require('./golden-utils');
const {FlakinessDashboard} = require('../utils/flakiness-dashboard');
const PROJECT_ROOT = fs.existsSync(path.join(__dirname, '..', 'package.json')) ? path.join(__dirname, '..') : path.join(__dirname, '..', '..');
@@ -72,6 +74,19 @@ const utils = module.exports = {
});
},
extendExpectWithToBeGolden: function(goldenDir, outputDir) {
expect.extend({
toBeGolden: (testScreenshot, goldenFilePath) => {
const result = GoldenUtils.compare(goldenDir, outputDir, testScreenshot, goldenFilePath);
return {
message: () => result.message,
pass: result.pass
};
}
});
},
/**
* @return {string}
*/