Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

feat(testrunner): make it easier to setup golden matcher (#1682)

Dmitry Gozmancd39053ccc6d
diff --git a/test/playwright.spec.js b/test/playwright.spec.js
index b872538f7e..5428fb2726 100644
--- a/test/playwright.spec.js
+++ b/test/playwright.spec.js
@@ -17,8 +17,6 @@
const fs = require('fs');
const path = require('path');
const rm = require('rimraf').sync;
-const GoldenUtils = require('./golden-utils');
-const {Matchers} = require('../utils/testrunner/Matchers');
const readline = require('readline');
const {TestServer} = require('../utils/testserver/');
@@ -54,6 +52,7 @@ const browserNames = BROWSER_CONFIGS.map(config => config.name);
module.exports.addPlaywrightTests = ({testRunner, platform, products, playwrightPath, headless, slowMo, dumpProtocolOnFailure, coverage}) => {
const {describe, xdescribe, fdescribe} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
+ const {expect} = testRunner;
const MAC = platform === 'darwin';
const LINUX = platform === 'linux';
@@ -125,9 +124,7 @@ module.exports.addPlaywrightTests = ({testRunner, platform, products, playwright
const ASSETS_DIR = path.join(__dirname, 'assets');
if (fs.existsSync(OUTPUT_DIR))
rm(OUTPUT_DIR);
- const {expect} = new Matchers({
- toBeGolden: GoldenUtils.compare.bind(null, GOLDEN_DIR, OUTPUT_DIR)
- });
+ expect.setupGolden(GOLDEN_DIR, OUTPUT_DIR);
const testOptions = {
testRunner,
diff --git a/utils/doclint/check_public_api/test/test.js b/utils/doclint/check_public_api/test/test.js
index 9634668eba..5d34653d43 100644
--- a/utils/doclint/check_public_api/test/test.js
+++ b/utils/doclint/check_public_api/test/test.js
@@ -20,15 +20,14 @@ const checkPublicAPI = require('..');
const Source = require('../../Source');
const mdBuilder = require('../MDBuilder');
const jsBuilder = require('../JSBuilder');
-const GoldenUtils = require('../../../../test/golden-utils');
-const {Matchers} = require('../../../testrunner/Matchers');
const TestRunner = require('../../../testrunner/');
const runner = new TestRunner();
const {describe, xdescribe, fdescribe} = runner.api();
const {it, fit, xit} = runner.api();
const {beforeAll, beforeEach, afterAll, afterEach} = runner.api();
+const {expect} = runner.api();
let browser;
let page;
@@ -62,10 +61,7 @@ runner.run();
async function testLint(state, testRun) {
const dirPath = path.join(__dirname, testRun.test().name());
- const {expect} = new Matchers({
- toBeGolden: GoldenUtils.compare.bind(null, dirPath, dirPath)
- });
-
+ expect.setupGolden(dirPath);
const mdSources = await Source.readdir(dirPath, '.md');
const tsSources = await Source.readdir(dirPath, '.ts');
const jsSources = await Source.readdir(dirPath, '.js');
@@ -76,9 +72,7 @@ async function testLint(state, testRun) {
async function testMDBuilder(state, testRun) {
const dirPath = path.join(__dirname, testRun.test().name());
- const {expect} = new Matchers({
- toBeGolden: GoldenUtils.compare.bind(null, dirPath, dirPath)
- });
+ expect.setupGolden(dirPath);
const sources = await Source.readdir(dirPath, '.md');
const {documentation} = await mdBuilder(page, sources);
expect(serialize(documentation)).toBeGolden('result.txt');
@@ -86,9 +80,7 @@ async function testMDBuilder(state, testRun) {
async function testJSBuilder(state, testRun) {
const dirPath = path.join(__dirname, testRun.test().name());
- const {expect} = new Matchers({
- toBeGolden: GoldenUtils.compare.bind(null, dirPath, dirPath)
- });
+ expect.setupGolden(dirPath);
const jsSources = await Source.readdir(dirPath, '.js');
const tsSources = await Source.readdir(dirPath, '.ts');
const {documentation} = await jsBuilder.checkSources(jsSources.concat(tsSources));
diff --git a/test/golden-utils.js b/utils/testrunner/GoldenUtils.js
similarity index 100%
rename from test/golden-utils.js
rename to utils/testrunner/GoldenUtils.js
diff --git a/utils/testrunner/Matchers.js b/utils/testrunner/Matchers.js
index ac1ee8b889..b8c5c5c5c6 100644
--- a/utils/testrunner/Matchers.js
+++ b/utils/testrunner/Matchers.js
@@ -17,6 +17,7 @@
const Location = require('./Location.js');
const colors = require('colors/safe');
const Diff = require('text-diff');
+const GoldenUtils = require('./GoldenUtils');
class Matchers {
constructor(customMatchers = {}) {
@@ -24,6 +25,9 @@ class Matchers {
Object.assign(this._matchers, DefaultMatchers);
Object.assign(this._matchers, customMatchers);
this.expect = this.expect.bind(this);
+ this.expect.setupGolden = (expected, output = expected) => {
+ this._matchers.toBeGolden = GoldenUtils.compare.bind(null, expected, output);
+ };
}
addMatcher(name, matcher) {