Skip to content

Commit b85ab89

Browse files
authored
chore(testrunner): make most modifiers external (#1581)
1 parent 4bd46ba commit b85ab89

File tree

6 files changed

+126
-226
lines changed

6 files changed

+126
-226
lines changed

test/playwright.spec.js

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ module.exports.describe = ({testRunner, product, playwrightPath}) => {
9999
ASSETS_DIR,
100100
};
101101

102+
function loadTests(modulePath) {
103+
const module = require(modulePath);
104+
if (typeof module.describe === 'function')
105+
describe('', module.describe, testOptions);
106+
if (typeof module.fdescribe === 'function')
107+
fdescribe('', module.fdescribe, testOptions);
108+
if (typeof module.xdescribe === 'function')
109+
xdescribe('', module.xdescribe, testOptions);
110+
}
111+
102112
describe('', function() {
103113
beforeAll(async state => {
104114
state.browser = await browserType.launch(defaultBrowserOptions);
@@ -154,69 +164,69 @@ module.exports.describe = ({testRunner, product, playwrightPath}) => {
154164

155165
// Page-level tests that are given a browser, a context and a page.
156166
// Each test is launched in a new browser context.
157-
describe('[Accessibility]', () => testRunner.loadTests(require('./accessibility.spec.js'), testOptions));
167+
describe('[Accessibility]', () => loadTests('./accessibility.spec.js'));
158168
describe('[Driver]', () => {
159-
testRunner.loadTests(require('./autowaiting.spec.js'), testOptions);
160-
testRunner.loadTests(require('./click.spec.js'), testOptions);
161-
testRunner.loadTests(require('./cookies.spec.js'), testOptions);
162-
testRunner.loadTests(require('./dialog.spec.js'), testOptions);
163-
testRunner.loadTests(require('./elementhandle.spec.js'), testOptions);
164-
testRunner.loadTests(require('./emulation.spec.js'), testOptions);
165-
testRunner.loadTests(require('./evaluation.spec.js'), testOptions);
166-
testRunner.loadTests(require('./frame.spec.js'), testOptions);
167-
testRunner.loadTests(require('./focus.spec.js'), testOptions);
168-
testRunner.loadTests(require('./input.spec.js'), testOptions);
169-
testRunner.loadTests(require('./jshandle.spec.js'), testOptions);
170-
testRunner.loadTests(require('./keyboard.spec.js'), testOptions);
171-
testRunner.loadTests(require('./mouse.spec.js'), testOptions);
172-
testRunner.loadTests(require('./navigation.spec.js'), testOptions);
173-
testRunner.loadTests(require('./network.spec.js'), testOptions);
174-
testRunner.loadTests(require('./page.spec.js'), testOptions);
175-
testRunner.loadTests(require('./queryselector.spec.js'), testOptions);
176-
testRunner.loadTests(require('./screenshot.spec.js'), testOptions);
177-
testRunner.loadTests(require('./waittask.spec.js'), testOptions);
178-
testRunner.loadTests(require('./interception.spec.js'), testOptions);
179-
testRunner.loadTests(require('./geolocation.spec.js'), testOptions);
180-
testRunner.loadTests(require('./workers.spec.js'), testOptions);
181-
testRunner.loadTests(require('./capabilities.spec.js'), testOptions);
169+
loadTests('./autowaiting.spec.js');
170+
loadTests('./click.spec.js');
171+
loadTests('./cookies.spec.js');
172+
loadTests('./dialog.spec.js');
173+
loadTests('./elementhandle.spec.js');
174+
loadTests('./emulation.spec.js');
175+
loadTests('./evaluation.spec.js');
176+
loadTests('./frame.spec.js');
177+
loadTests('./focus.spec.js');
178+
loadTests('./input.spec.js');
179+
loadTests('./jshandle.spec.js');
180+
loadTests('./keyboard.spec.js');
181+
loadTests('./mouse.spec.js');
182+
loadTests('./navigation.spec.js');
183+
loadTests('./network.spec.js');
184+
loadTests('./page.spec.js');
185+
loadTests('./queryselector.spec.js');
186+
loadTests('./screenshot.spec.js');
187+
loadTests('./waittask.spec.js');
188+
loadTests('./interception.spec.js');
189+
loadTests('./geolocation.spec.js');
190+
loadTests('./workers.spec.js');
191+
loadTests('./capabilities.spec.js');
182192
});
183193
describe('[Permissions]', () => {
184-
testRunner.loadTests(require('./permissions.spec.js'), testOptions);
194+
loadTests('./permissions.spec.js');
185195
});
186196

187197
describe.skip(!CHROMIUM)('[Chromium]', () => {
188-
testRunner.loadTests(require('./chromium/chromium.spec.js'), testOptions);
189-
testRunner.loadTests(require('./chromium/coverage.spec.js'), testOptions);
190-
testRunner.loadTests(require('./chromium/pdf.spec.js'), testOptions);
191-
testRunner.loadTests(require('./chromium/session.spec.js'), testOptions);
198+
loadTests('./chromium/chromium.spec.js');
199+
loadTests('./chromium/coverage.spec.js');
200+
loadTests('./chromium/pdf.spec.js');
201+
loadTests('./chromium/session.spec.js');
192202
});
193203
});
194204

195205
// Browser-level tests that are given a browser.
196206
describe('[Driver]', () => {
197-
testRunner.loadTests(require('./browser.spec.js'), testOptions);
198-
testRunner.loadTests(require('./browsercontext.spec.js'), testOptions);
199-
testRunner.loadTests(require('./ignorehttpserrors.spec.js'), testOptions);
200-
testRunner.loadTests(require('./popup.spec.js'), testOptions);
207+
loadTests('./browser.spec.js');
208+
loadTests('./browsercontext.spec.js');
209+
loadTests('./ignorehttpserrors.spec.js');
210+
loadTests('./popup.spec.js');
201211
});
202212
});
203213

204214
// Top-level tests that launch Browser themselves.
205215
describe('[Driver]', () => {
206-
testRunner.loadTests(require('./defaultbrowsercontext.spec.js'), testOptions);
207-
testRunner.loadTests(require('./fixtures.spec.js'), testOptions);
208-
testRunner.loadTests(require('./launcher.spec.js'), testOptions);
209-
testRunner.loadTests(require('./headful.spec.js'), testOptions);
210-
testRunner.loadTests(require('./multiclient.spec.js'), testOptions);
216+
loadTests('./defaultbrowsercontext.spec.js');
217+
loadTests('./fixtures.spec.js');
218+
loadTests('./launcher.spec.js');
219+
loadTests('./headful.spec.js');
220+
loadTests('./multiclient.spec.js');
211221
});
212222

213223
describe.skip(!CHROMIUM)('[Chromium]', () => {
214-
testRunner.loadTests(require('./chromium/launcher.spec.js'), testOptions);
215-
testRunner.loadTests(require('./chromium/oopif.spec.js'), testOptions);
216-
testRunner.loadTests(require('./chromium/tracing.spec.js'), testOptions);
224+
loadTests('./chromium/launcher.spec.js');
225+
loadTests('./chromium/oopif.spec.js');
226+
loadTests('./chromium/tracing.spec.js');
217227
});
218228

219229
describe('[Driver]', () => {
220-
testRunner.loadTests(require('./web.spec.js'), testOptions);
230+
loadTests('./web.spec.js');
221231
});
222232
};

test/test.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const path = require('path');
1818
const {TestServer} = require('../utils/testserver/');
1919
const {TestRunner, Reporter} = require('../utils/testrunner/');
2020
const utils = require('./utils');
21+
const inspector = require('inspector');
2122

2223
let parallel = 1;
2324
if (process.env.PW_PARALLEL_TESTS)
@@ -30,11 +31,66 @@ require('events').defaultMaxListeners *= parallel;
3031
let timeout = process.env.CI ? 30 * 1000 : 10 * 1000;
3132
if (!isNaN(process.env.TIMEOUT))
3233
timeout = parseInt(process.env.TIMEOUT * 1000, 10);
34+
const MAJOR_NODEJS_VERSION = parseInt(process.version.substring(1).split('.')[0], 10);
35+
if (MAJOR_NODEJS_VERSION >= 8 && inspector.url()) {
36+
console.log('Detected inspector - disabling timeout to be debugger-friendly');
37+
timeout = 0;
38+
}
39+
3340
const testRunner = new TestRunner({
3441
timeout,
3542
parallel,
3643
breakOnFailure: process.argv.indexOf('--break-on-failure') !== -1,
44+
installCommonHelpers: false
45+
});
46+
testRunner.testModifier('skip', (t, condition) => condition && t.setMode(t.Modes.Skip));
47+
testRunner.suiteModifier('skip', (s, condition) => condition && s.setMode(s.Modes.Skip));
48+
testRunner.testModifier('fail', (t, condition) => condition && t.setExpectation(t.Expectations.Fail));
49+
testRunner.suiteModifier('fail', (s, condition) => condition && s.setExpectation(s.Expectations.Fail));
50+
testRunner.testModifier('slow', (t, condition) => condition && t.setTimeout(t.timeout() * 3));
51+
testRunner.testModifier('repeat', (t, count) => t.setRepeat(count));
52+
testRunner.suiteModifier('repeat', (s, count) => s.setRepeat(count));
53+
testRunner.testAttribute('focus', t => t.setMode(t.Modes.Focus));
54+
testRunner.suiteAttribute('focus', s => s.setMode(s.Modes.Focus));
55+
testRunner.testAttribute('debug', t => {
56+
t.setMode(t.Modes.Focus);
57+
t.setTimeout(100000000);
58+
59+
let session;
60+
t.before(async () => {
61+
const util = require('util');
62+
const fs = require('fs');
63+
const url = require('url');
64+
const readFileAsync = util.promisify(fs.readFile.bind(fs));
65+
session = new inspector.Session();
66+
session.connect();
67+
const postAsync = util.promisify(session.post.bind(session));
68+
await postAsync('Debugger.enable');
69+
const setBreakpointCommands = [];
70+
const N = t.body().toString().split('\n').length;
71+
const location = t.location();
72+
const lines = (await readFileAsync(location.filePath, 'utf8')).split('\n');
73+
for (let line = 0; line < N; ++line) {
74+
const lineNumber = line + location.lineNumber;
75+
setBreakpointCommands.push(postAsync('Debugger.setBreakpointByUrl', {
76+
url: url.pathToFileURL(location.filePath),
77+
lineNumber,
78+
condition: `console.log('${String(lineNumber + 1).padStart(6, ' ')} | ' + ${JSON.stringify(lines[lineNumber])})`,
79+
}).catch(e => {}));
80+
}
81+
await Promise.all(setBreakpointCommands);
82+
});
83+
84+
t.after(async () => {
85+
session.disconnect();
86+
});
3787
});
88+
testRunner.fdescribe = testRunner.describe.focus;
89+
testRunner.xdescribe = testRunner.describe.skip(true);
90+
testRunner.fit = testRunner.it.focus;
91+
testRunner.xit = testRunner.it.skip(true);
92+
testRunner.dit = testRunner.it.debug;
93+
3894
const {describe, fdescribe, beforeAll, afterAll, beforeEach, afterEach} = testRunner;
3995

4096
console.log('Testing on Node', process.version);
@@ -106,7 +162,7 @@ for (const browserConfig of BROWSER_CONFIGS) {
106162
continue;
107163
const product = browserConfig.name;
108164
describe(product, () => {
109-
testRunner.loadTests(require('./playwright.spec.js'), {
165+
testRunner.describe('', require('./playwright.spec.js').describe, {
110166
product,
111167
playwrightPath: utils.projectRoot(),
112168
testRunner,

test/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type TestRunner<STATE> = {
3636
fit: ItFunction<STATE>;
3737
dit: ItFunction<STATE>;
3838

39-
beforeAll, beforeEach, afterAll, afterEach, loadTests;
39+
beforeAll, beforeEach, afterAll, afterEach;
4040
};
4141

4242
interface TestSetup<STATE> {

utils/testrunner/Multimap.js

Lines changed: 0 additions & 95 deletions
This file was deleted.

0 commit comments

Comments
 (0)