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 fs = require('fs');
const path = require('path'); const path = require('path');
const rm = require('rimraf').sync; const rm = require('rimraf').sync;
const GoldenUtils = require('./golden-utils');
const {Matchers} = require('../utils/testrunner/Matchers');
const readline = require('readline'); const readline = require('readline');
const {TestServer} = require('../utils/testserver/'); 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}) => { module.exports.addPlaywrightTests = ({testRunner, platform, products, playwrightPath, headless, slowMo, dumpProtocolOnFailure, coverage}) => {
const {describe, xdescribe, fdescribe} = testRunner; const {describe, xdescribe, fdescribe} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner; const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
const {expect} = testRunner;
const MAC = platform === 'darwin'; const MAC = platform === 'darwin';
const LINUX = platform === 'linux'; const LINUX = platform === 'linux';
@ -125,9 +124,7 @@ module.exports.addPlaywrightTests = ({testRunner, platform, products, playwright
const ASSETS_DIR = path.join(__dirname, 'assets'); const ASSETS_DIR = path.join(__dirname, 'assets');
if (fs.existsSync(OUTPUT_DIR)) if (fs.existsSync(OUTPUT_DIR))
rm(OUTPUT_DIR); rm(OUTPUT_DIR);
const {expect} = new Matchers({ expect.setupGolden(GOLDEN_DIR, OUTPUT_DIR);
toBeGolden: GoldenUtils.compare.bind(null, GOLDEN_DIR, OUTPUT_DIR)
});
const testOptions = { const testOptions = {
testRunner, testRunner,

View File

@ -20,15 +20,14 @@ const checkPublicAPI = require('..');
const Source = require('../../Source'); const Source = require('../../Source');
const mdBuilder = require('../MDBuilder'); const mdBuilder = require('../MDBuilder');
const jsBuilder = require('../JSBuilder'); const jsBuilder = require('../JSBuilder');
const GoldenUtils = require('../../../../test/golden-utils');
const {Matchers} = require('../../../testrunner/Matchers');
const TestRunner = require('../../../testrunner/'); const TestRunner = require('../../../testrunner/');
const runner = new TestRunner(); const runner = new TestRunner();
const {describe, xdescribe, fdescribe} = runner.api(); const {describe, xdescribe, fdescribe} = runner.api();
const {it, fit, xit} = runner.api(); const {it, fit, xit} = runner.api();
const {beforeAll, beforeEach, afterAll, afterEach} = runner.api(); const {beforeAll, beforeEach, afterAll, afterEach} = runner.api();
const {expect} = runner.api();
let browser; let browser;
let page; let page;
@ -62,10 +61,7 @@ runner.run();
async function testLint(state, testRun) { async function testLint(state, testRun) {
const dirPath = path.join(__dirname, testRun.test().name()); const dirPath = path.join(__dirname, testRun.test().name());
const {expect} = new Matchers({ expect.setupGolden(dirPath);
toBeGolden: GoldenUtils.compare.bind(null, dirPath, dirPath)
});
const mdSources = await Source.readdir(dirPath, '.md'); const mdSources = await Source.readdir(dirPath, '.md');
const tsSources = await Source.readdir(dirPath, '.ts'); const tsSources = await Source.readdir(dirPath, '.ts');
const jsSources = await Source.readdir(dirPath, '.js'); const jsSources = await Source.readdir(dirPath, '.js');
@ -76,9 +72,7 @@ async function testLint(state, testRun) {
async function testMDBuilder(state, testRun) { async function testMDBuilder(state, testRun) {
const dirPath = path.join(__dirname, testRun.test().name()); const dirPath = path.join(__dirname, testRun.test().name());
const {expect} = new Matchers({ expect.setupGolden(dirPath);
toBeGolden: GoldenUtils.compare.bind(null, dirPath, dirPath)
});
const sources = await Source.readdir(dirPath, '.md'); const sources = await Source.readdir(dirPath, '.md');
const {documentation} = await mdBuilder(page, sources); const {documentation} = await mdBuilder(page, sources);
expect(serialize(documentation)).toBeGolden('result.txt'); expect(serialize(documentation)).toBeGolden('result.txt');
@ -86,9 +80,7 @@ async function testMDBuilder(state, testRun) {
async function testJSBuilder(state, testRun) { async function testJSBuilder(state, testRun) {
const dirPath = path.join(__dirname, testRun.test().name()); const dirPath = path.join(__dirname, testRun.test().name());
const {expect} = new Matchers({ expect.setupGolden(dirPath);
toBeGolden: GoldenUtils.compare.bind(null, dirPath, dirPath)
});
const jsSources = await Source.readdir(dirPath, '.js'); const jsSources = await Source.readdir(dirPath, '.js');
const tsSources = await Source.readdir(dirPath, '.ts'); const tsSources = await Source.readdir(dirPath, '.ts');
const {documentation} = await jsBuilder.checkSources(jsSources.concat(tsSources)); const {documentation} = await jsBuilder.checkSources(jsSources.concat(tsSources));

View File

@ -17,6 +17,7 @@
const Location = require('./Location.js'); const Location = require('./Location.js');
const colors = require('colors/safe'); const colors = require('colors/safe');
const Diff = require('text-diff'); const Diff = require('text-diff');
const GoldenUtils = require('./GoldenUtils');
class Matchers { class Matchers {
constructor(customMatchers = {}) { constructor(customMatchers = {}) {
@ -24,6 +25,9 @@ class Matchers {
Object.assign(this._matchers, DefaultMatchers); Object.assign(this._matchers, DefaultMatchers);
Object.assign(this._matchers, customMatchers); Object.assign(this._matchers, customMatchers);
this.expect = this.expect.bind(this); this.expect = this.expect.bind(this);
this.expect.setupGolden = (expected, output = expected) => {
this._matchers.toBeGolden = GoldenUtils.compare.bind(null, expected, output);
};
} }
addMatcher(name, matcher) { addMatcher(name, matcher) {