Skip to content

Commit f2b13c0

Browse files
authored
chore(testrunner): split TestRunner into parts (#1679)
1 parent aeeac55 commit f2b13c0

File tree

13 files changed

+861
-763
lines changed

13 files changed

+861
-763
lines changed

test/playwright.spec.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const fs = require('fs');
1818
const path = require('path');
1919
const rm = require('rimraf').sync;
2020
const GoldenUtils = require('./golden-utils');
21-
const {Matchers} = require('../utils/testrunner/');
21+
const {Matchers} = require('../utils/testrunner/Matchers');
2222
const readline = require('readline');
2323
const {TestServer} = require('../utils/testserver/');
2424

@@ -174,14 +174,13 @@ module.exports.addPlaywrightTests = ({testRunner, platform, products, playwright
174174
state._stderr.close();
175175
});
176176

177-
beforeEach(async(state, test) => {
178-
test.output = [];
179-
const dumpout = data => test.output.push(`\x1b[33m[pw:stdio:out]\x1b[0m ${data}`);
180-
const dumperr = data => test.output.push(`\x1b[31m[pw:stdio:err]\x1b[0m ${data}`);
177+
beforeEach(async(state, testRun) => {
178+
const dumpout = data => testRun.log(`\x1b[33m[pw:stdio:out]\x1b[0m ${data}`);
179+
const dumperr = data => testRun.log(`\x1b[31m[pw:stdio:err]\x1b[0m ${data}`);
181180
state._stdout.on('line', dumpout);
182181
state._stderr.on('line', dumperr);
183182
if (dumpProtocolOnFailure)
184-
state.browser._debugProtocol.log = data => test.output.push(`\x1b[32m[pw:protocol]\x1b[0m ${data}`);
183+
state.browser._debugProtocol.log = data => testRun.log(`\x1b[32m[pw:protocol]\x1b[0m ${data}`);
185184
state.tearDown = async () => {
186185
state._stdout.off('line', dumpout);
187186
state._stderr.off('line', dumperr);

test/test.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
const {TestRunner, Reporter} = require('../utils/testrunner/');
18+
const TestRunner = require('../utils/testrunner/');
1919
const utils = require('./utils');
2020
const os = require('os');
2121

@@ -38,9 +38,13 @@ if (MAJOR_NODEJS_VERSION >= 8 && require('inspector').url()) {
3838

3939
const testRunner = new TestRunner({
4040
timeout,
41+
totalTimeout: process.env.CI ? 15 * 60 * 1000 : 0,
4142
parallel,
4243
breakOnFailure: process.argv.indexOf('--break-on-failure') !== -1,
43-
installCommonHelpers: false
44+
verbose: process.argv.includes('--verbose'),
45+
summary: !process.argv.includes('--verbose'),
46+
showSlowTests: process.env.CI ? 5 : 0,
47+
showMarkedAsFailingTests: 10,
4448
});
4549
utils.setupTestRunner(testRunner);
4650

@@ -68,7 +72,7 @@ require('./playwright.spec.js').addPlaywrightTests({
6872
playwrightPath: utils.projectRoot(),
6973
products,
7074
platform: os.platform(),
71-
testRunner,
75+
testRunner: testRunner.api(),
7276
headless: !!valueFromEnv('HEADLESS', true),
7377
slowMo: valueFromEnv('SLOW_MO', 0),
7478
dumpProtocolOnFailure: valueFromEnv('DEBUGP', false),
@@ -81,15 +85,5 @@ if (filterArgIndex !== -1) {
8185
testRunner.focusMatchingTests(new RegExp(filter, 'i'));
8286
}
8387

84-
new Reporter(testRunner, {
85-
verbose: process.argv.includes('--verbose'),
86-
summary: !process.argv.includes('--verbose'),
87-
showSlowTests: process.env.CI ? 5 : 0,
88-
showMarkedAsFailingTests: 10,
89-
});
90-
9188
// await utils.initializeFlakinessDashboardIfNeeded(testRunner);
92-
testRunner.run({ totalTimeout: process.env.CI ? 15 * 60 * 1000 : 0 }).then(result => {
93-
process.exit(result.exitCode);
94-
});
95-
89+
testRunner.run();

test/utils.js

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ const utils = module.exports = {
8585
const ignoredMethods = new Set(ignoredMethodsArray);
8686
for (const [className, classType] of Object.entries(api))
8787
traceAPICoverage(coverage, events, className, classType);
88-
const focus = testRunner.hasFocusedTestsOrSuites();
89-
(focus ? testRunner.fdescribe : testRunner.describe)(COVERAGE_TESTSUITE_NAME, () => {
90-
(focus ? testRunner.fit : testRunner.it)('should call all API methods', () => {
88+
testRunner.describe(COVERAGE_TESTSUITE_NAME, () => {
89+
testRunner.it('should call all API methods', () => {
9190
const missingMethods = [];
9291
const extraIgnoredMethods = [];
9392
for (const method of coverage.keys()) {
@@ -292,49 +291,46 @@ const utils = module.exports = {
292291
},
293292

294293
setupTestRunner: function(testRunner) {
295-
testRunner.testModifier('skip', (t, condition) => condition && t.setSkipped(true));
296-
testRunner.suiteModifier('skip', (s, condition) => condition && s.setSkipped(true));
297-
testRunner.testModifier('fail', (t, condition) => condition && t.setExpectation(t.Expectations.Fail));
298-
testRunner.suiteModifier('fail', (s, condition) => condition && s.setExpectation(s.Expectations.Fail));
299-
testRunner.testModifier('slow', (t, condition) => condition && t.setTimeout(t.timeout() * 3));
300-
testRunner.testModifier('repeat', (t, count) => t.setRepeat(count));
301-
testRunner.suiteModifier('repeat', (s, count) => s.setRepeat(count));
302-
testRunner.testAttribute('focus', t => t.setFocused(true));
303-
testRunner.suiteAttribute('focus', s => s.setFocused(true));
304-
testRunner.testAttribute('debug', t => {
305-
t.setFocused(true);
294+
const collector = testRunner._collector;
295+
collector.addTestModifier('skip', (t, condition) => condition && t.setSkipped(true));
296+
collector.addSuiteModifier('skip', (s, condition) => condition && s.setSkipped(true));
297+
collector.addTestModifier('fail', (t, condition) => condition && t.setExpectation(t.Expectations.Fail));
298+
collector.addSuiteModifier('fail', (s, condition) => condition && s.setExpectation(s.Expectations.Fail));
299+
collector.addTestModifier('slow', t => t.setTimeout(t.timeout() * 3));
300+
collector.addTestAttribute('debug', t => {
306301
t.setTimeout(100000000);
307302

308303
let session;
309-
t.before(async () => {
304+
t.environment().beforeEach(async () => {
305+
const inspector = require('inspector');
310306
const readFileAsync = util.promisify(fs.readFile.bind(fs));
311-
session = new require('inspector').Session();
307+
session = new inspector.Session();
312308
session.connect();
313309
const postAsync = util.promisify(session.post.bind(session));
314310
await postAsync('Debugger.enable');
315311
const setBreakpointCommands = [];
316312
const N = t.body().toString().split('\n').length;
317313
const location = t.location();
318-
const lines = (await readFileAsync(location.filePath, 'utf8')).split('\n');
314+
const lines = (await readFileAsync(location.filePath(), 'utf8')).split('\n');
319315
for (let line = 0; line < N; ++line) {
320-
const lineNumber = line + location.lineNumber;
316+
const lineNumber = line + location.lineNumber();
321317
setBreakpointCommands.push(postAsync('Debugger.setBreakpointByUrl', {
322-
url: url.pathToFileURL(location.filePath),
318+
url: url.pathToFileURL(location.filePath()),
323319
lineNumber,
324320
condition: `console.log('${String(lineNumber + 1).padStart(6, ' ')} | ' + ${JSON.stringify(lines[lineNumber])})`,
325321
}).catch(e => {}));
326322
}
327323
await Promise.all(setBreakpointCommands);
328324
});
329325

330-
t.after(async () => {
326+
t.environment().afterEach(async () => {
331327
session.disconnect();
332328
});
333329
});
334-
testRunner.fdescribe = testRunner.describe.focus;
335-
testRunner.xdescribe = testRunner.describe.skip(true);
336-
testRunner.fit = testRunner.it.focus;
337-
testRunner.xit = testRunner.it.skip(true);
338-
testRunner.dit = testRunner.it.debug;
330+
testRunner.api().fdescribe = testRunner.api().describe.only;
331+
testRunner.api().xdescribe = testRunner.api().describe.skip(true);
332+
testRunner.api().fit = testRunner.api().it.only;
333+
testRunner.api().xit = testRunner.api().it.skip(true);
334+
testRunner.api().dit = testRunner.api().it.only.debug;
339335
},
340336
};

utils/doclint/check_public_api/test/test.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ const mdBuilder = require('../MDBuilder');
2222
const jsBuilder = require('../JSBuilder');
2323
const GoldenUtils = require('../../../../test/golden-utils');
2424

25-
const {TestRunner, Reporter, Matchers} = require('../../../testrunner/');
25+
const {Matchers} = require('../../../testrunner/Matchers');
26+
const TestRunner = require('../../../testrunner/');
2627
const runner = new TestRunner();
27-
const reporter = new Reporter(runner);
2828

29-
const {describe, xdescribe, fdescribe} = runner;
30-
const {it, fit, xit} = runner;
31-
const {beforeAll, beforeEach, afterAll, afterEach} = runner;
29+
const {describe, xdescribe, fdescribe} = runner.api();
30+
const {it, fit, xit} = runner.api();
31+
const {beforeAll, beforeEach, afterAll, afterEach} = runner.api();
3232

33-
let browserContext;
33+
let browser;
3434
let page;
3535

3636
beforeAll(async function() {
@@ -60,8 +60,8 @@ describe('checkPublicAPI', function() {
6060

6161
runner.run();
6262

63-
async function testLint(state, test) {
64-
const dirPath = path.join(__dirname, test.name());
63+
async function testLint(state, testRun) {
64+
const dirPath = path.join(__dirname, testRun.test().name());
6565
const {expect} = new Matchers({
6666
toBeGolden: GoldenUtils.compare.bind(null, dirPath, dirPath)
6767
});
@@ -74,8 +74,8 @@ async function testLint(state, test) {
7474
expect(errors.join('\n')).toBeGolden('result.txt');
7575
}
7676

77-
async function testMDBuilder(state, test) {
78-
const dirPath = path.join(__dirname, test.name());
77+
async function testMDBuilder(state, testRun) {
78+
const dirPath = path.join(__dirname, testRun.test().name());
7979
const {expect} = new Matchers({
8080
toBeGolden: GoldenUtils.compare.bind(null, dirPath, dirPath)
8181
});
@@ -84,8 +84,8 @@ async function testMDBuilder(state, test) {
8484
expect(serialize(documentation)).toBeGolden('result.txt');
8585
}
8686

87-
async function testJSBuilder(state, test) {
88-
const dirPath = path.join(__dirname, test.name());
87+
async function testJSBuilder(state, testRun) {
88+
const dirPath = path.join(__dirname, testRun.test().name());
8989
const {expect} = new Matchers({
9090
toBeGolden: GoldenUtils.compare.bind(null, dirPath, dirPath)
9191
});

utils/doclint/preprocessor/test.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616

1717
const {runCommands, ensureReleasedAPILinks} = require('.');
1818
const Source = require('../Source');
19-
const {TestRunner, Reporter, Matchers} = require('../../testrunner/');
19+
const TestRunner = require('../../testrunner/');
2020
const runner = new TestRunner();
21-
new Reporter(runner);
2221

23-
const {describe, xdescribe, fdescribe} = runner;
24-
const {it, fit, xit} = runner;
25-
const {beforeAll, beforeEach, afterAll, afterEach} = runner;
26-
const {expect} = new Matchers();
22+
const {describe, xdescribe, fdescribe} = runner.api();
23+
const {it, fit, xit} = runner.api();
24+
const {beforeAll, beforeEach, afterAll, afterEach} = runner.api();
25+
const {expect} = runner.api();
2726

2827
describe('ensureReleasedAPILinks', function() {
2928
it('should work with non-release version', function() {

utils/testrunner/Location.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class Location {
4949
return this._fileName + ':' + this._lineNumber + ':' + this._columnNumber;
5050
}
5151

52+
// TODO: static getCallerLocationIn(glob) vs getCallerLocationIgnoring(glob).
53+
5254
static getCallerLocation(filename) {
5355
const error = new Error();
5456
const stackFrames = error.stack.split('\n').slice(1);

utils/testrunner/Reporter.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,33 @@ const colors = require('colors/safe');
1919
const {MatchError} = require('./Matchers.js');
2020

2121
class Reporter {
22-
constructor(runner, options = {}) {
22+
constructor(delegate, options = {}) {
2323
const {
2424
showSlowTests = 3,
2525
showMarkedAsFailingTests = Infinity,
2626
verbose = false,
2727
summary = true,
2828
} = options;
2929
this._filePathToLines = new Map();
30-
this._runner = runner;
30+
this._delegate = delegate;
3131
this._showSlowTests = showSlowTests;
3232
this._showMarkedAsFailingTests = showMarkedAsFailingTests;
3333
this._verbose = verbose;
3434
this._summary = summary;
3535
this._testCounter = 0;
36-
runner.setDelegate(this);
3736
}
3837

3938
onStarted(testRuns) {
4039
this._testCounter = 0;
4140
this._timestamp = Date.now();
42-
const allTests = this._runner.tests();
43-
if (!this._runner.hasFocusedTestsOrSuites()) {
44-
console.log(`Running all ${colors.yellow(testRuns.length)} tests on ${colors.yellow(this._runner.parallel())} worker${this._runner.parallel() > 1 ? 's' : ''}:\n`);
41+
if (!this._delegate.hasFocusedTestsOrSuites()) {
42+
console.log(`Running all ${colors.yellow(testRuns.length)} tests on ${colors.yellow(this._delegate.parallel())} worker${this._delegate.parallel() > 1 ? 's' : ''}:\n`);
4543
} else {
46-
console.log(`Running ${colors.yellow(testRuns.length)} focused tests out of total ${colors.yellow(allTests.length)} on ${colors.yellow(this._runner.parallel())} worker${this._runner.parallel() > 1 ? 's' : ''}`);
44+
console.log(`Running ${colors.yellow(testRuns.length)} focused tests out of total ${colors.yellow(this._delegate.testCount())} on ${colors.yellow(this._delegate.parallel())} worker${this._delegate.parallel() > 1 ? 's' : ''}`);
4745
console.log('');
4846
const focusedEntities = [
49-
...this._runner.suites().filter(suite => suite.focused()),
50-
...this._runner.tests().filter(test => test.focused()),
47+
...this._delegate.focusedSuites(),
48+
...this._delegate.focusedTests(),
5149
];
5250
if (focusedEntities.length) {
5351
console.log('Focused Suites and Tests:');
@@ -174,7 +172,7 @@ class Reporter {
174172
_printVerboseTestRunResult(resultIndex, testRun) {
175173
const test = testRun.test();
176174
let prefix = `${resultIndex})`;
177-
if (this._runner.parallel() > 1)
175+
if (this._delegate.parallel() > 1)
178176
prefix += ' ' + colors.gray(`[worker = ${testRun.workerId()}]`);
179177
if (testRun.result() === 'ok') {
180178
console.log(`${prefix} ${colors.green('[OK]')} ${test.fullName()} (${formatLocation(test.location())})`);
@@ -187,9 +185,10 @@ class Reporter {
187185
console.log(`${prefix} ${colors.yellow('[MARKED AS FAILING]')} ${test.fullName()} (${formatLocation(test.location())})`);
188186
} else if (testRun.result() === 'timedout') {
189187
console.log(`${prefix} ${colors.red(`[TIMEOUT ${test.timeout()}ms]`)} ${test.fullName()} (${formatLocation(test.location())})`);
190-
if (testRun.output) {
188+
const output = testRun.output();
189+
if (output.length) {
191190
console.log(' Output:');
192-
for (const line of testRun.output)
191+
for (const line of output)
193192
console.log(' ' + line);
194193
}
195194
} else if (testRun.result() === 'failed') {
@@ -235,9 +234,10 @@ class Reporter {
235234
console.log(padLines(stack, 4));
236235
}
237236
}
238-
if (testRun.output) {
237+
const output = testRun.output();
238+
if (output.length) {
239239
console.log(' Output:');
240-
for (const line of testRun.output)
240+
for (const line of output)
241241
console.log(' ' + line);
242242
}
243243
}

0 commit comments

Comments
 (0)