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

This commit is contained in:
Dmitry Gozman 2020-04-06 18:01:56 -07:00 committed by GitHub
parent e519a3d762
commit cd39053ccc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 10 additions and 17 deletions

View File

@ -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,

View File

@ -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));

View File

@ -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) {