test: move fixtures to jest (#3010)

This commit is contained in:
Pavel Feldman 2020-07-17 13:53:23 -07:00 committed by GitHub
parent 24f6d19e27
commit 096ec4c44f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 35 deletions

View File

@ -47,13 +47,13 @@ function parseStackFrame(frame: string): ParsedStackFrame | null {
export function getCallerFilePath(ignorePrefix = PLAYWRIGHT_LIB_PATH): string | null {
const error = new Error();
const stackFrames = (error.stack || '').split('\n').slice(1);
const stackFrames = (error.stack || '').split('\n').slice(2);
// Find first stackframe that doesn't point to ignorePrefix.
for (const frame of stackFrames) {
const parsed = parseStackFrame(frame);
if (!parsed)
return null;
if (parsed.filePath.startsWith(ignorePrefix) || parsed.filePath === __filename)
if (parsed.filePath.startsWith(ignorePrefix))
continue;
return parsed.filePath;
}

View File

@ -17,24 +17,26 @@
const path = require('path');
const {spawn, execSync} = require('child_process');
const {FFOX, CHROMIUM, WEBKIT, WIN, LINUX} = require('./utils').testOptions(browserType);
const {FFOX, CHROMIUM, WEBKIT, WIN, LINUX, HEADLESS} = testOptions;
const playwrightPath = path.join(__dirname, '..');
class Wrapper {
constructor(state, extraOptions) {
constructor(browserType, defaultBrowserOptions, extraOptions) {
this._output = new Map();
this._outputCallback = new Map();
this._browserType = state.browserType;
const launchOptions = {...state.defaultBrowserOptions,
this._browserType = browserType;
const launchOptions = {...defaultBrowserOptions,
handleSIGINT: true,
handleSIGTERM: true,
handleSIGHUP: true,
executablePath: state.browserType.executablePath(),
executablePath: browserType.executablePath(),
logger: undefined,
};
const options = {
playwrightFile: path.join(state.playwrightPath, 'index'),
browserTypeName: state.browserType.name(),
playwrightFile: path.join(playwrightPath, 'index'),
browserTypeName: browserType.name(),
launchOptions,
...extraOptions,
};
@ -89,15 +91,20 @@ class Wrapper {
}
}
async function setup(state, options = {}) {
const wrapper = new Wrapper(state, options);
registerFixture('wrapper', async ({browserType, defaultBrowserOptions}, test) => {
const wrapper = new Wrapper(browserType, defaultBrowserOptions);
await wrapper.connect();
return wrapper;
}
await test(wrapper);
});
registerFixture('stallingWrapper', async ({browserType, defaultBrowserOptions}, test) => {
const wrapper = new Wrapper(browserType, defaultBrowserOptions, { stallOnClose: true });
await wrapper.connect();
await test(wrapper);
});
describe('Fixtures', function() {
it.slow()('should close the browser when the node process closes', async state => {
const wrapper = await setup(state);
it.slow()('should close the browser when the node process closes', async ({wrapper}) => {
if (WIN)
execSync(`taskkill /pid ${wrapper.child().pid} /T /F`);
else
@ -107,10 +114,9 @@ describe('Fixtures', function() {
// so we don't check it here.
});
describe.skip(WIN).skip(!HEADLESS)('signals', () => {
describe.skip(WIN || !HEADLESS)('signals', () => {
// Cannot reliably send signals on Windows.
it.slow()('should report browser close signal', async state => {
const wrapper = await setup(state);
it.slow()('should report browser close signal', async ({wrapper}) => {
const pid = await wrapper.out('pid');
process.kill(-pid, 'SIGTERM');
expect(await wrapper.out('exitCode')).toBe('null');
@ -118,8 +124,7 @@ describe('Fixtures', function() {
process.kill(wrapper.child().pid);
await wrapper.childExitCode();
});
it.slow()('should report browser close signal 2', async state => {
const wrapper = await setup(state);
it.slow()('should report browser close signal 2', async ({wrapper}) => {
const pid = await wrapper.out('pid');
process.kill(-pid, 'SIGKILL');
expect(await wrapper.out('exitCode')).toBe('null');
@ -127,29 +132,26 @@ describe('Fixtures', function() {
process.kill(wrapper.child().pid);
await wrapper.childExitCode();
});
it.slow()('should close the browser on SIGINT', async state => {
const wrapper = await setup(state);
it.slow()('should close the browser on SIGINT', async ({wrapper}) => {
process.kill(wrapper.child().pid, 'SIGINT');
expect(await wrapper.out('exitCode')).toBe('0');
expect(await wrapper.out('signal')).toBe('null');
expect(await wrapper.childExitCode()).toBe(130);
});
it.slow()('should close the browser on SIGTERM', async state => {
const wrapper = await setup(state);
it.slow()('should close the browser on SIGTERM', async ({wrapper}) => {
process.kill(wrapper.child().pid, 'SIGTERM');
expect(await wrapper.out('exitCode')).toBe('0');
expect(await wrapper.out('signal')).toBe('null');
expect(await wrapper.childExitCode()).toBe(0);
});
it.slow()('should close the browser on SIGHUP', async state => {
const wrapper = await setup(state);
it.slow()('should close the browser on SIGHUP', async ({wrapper}) => {
process.kill(wrapper.child().pid, 'SIGHUP');
expect(await wrapper.out('exitCode')).toBe('0');
expect(await wrapper.out('signal')).toBe('null');
expect(await wrapper.childExitCode()).toBe(0);
});
it.slow()('should kill the browser on double SIGINT', async state => {
const wrapper = await setup(state, { stallOnClose: true });
it.slow()('should kill the browser on double SIGINT', async ({stallingWrapper}) => {
const wrapper = stallingWrapper;
process.kill(wrapper.child().pid, 'SIGINT');
await wrapper.out('stalled');
process.kill(wrapper.child().pid, 'SIGINT');
@ -157,8 +159,8 @@ describe('Fixtures', function() {
expect(await wrapper.out('signal')).toBe('SIGKILL');
expect(await wrapper.childExitCode()).toBe(130);
});
it.slow()('should kill the browser on SIGINT + SIGTERM', async state => {
const wrapper = await setup(state, { stallOnClose: true });
it.slow()('should kill the browser on SIGINT + SIGTERM', async ({stallingWrapper}) => {
const wrapper = stallingWrapper;
process.kill(wrapper.child().pid, 'SIGINT');
await wrapper.out('stalled');
process.kill(wrapper.child().pid, 'SIGTERM');
@ -166,8 +168,8 @@ describe('Fixtures', function() {
expect(await wrapper.out('signal')).toBe('SIGKILL');
expect(await wrapper.childExitCode()).toBe(0);
});
it.slow()('should kill the browser on SIGTERM + SIGINT', async state => {
const wrapper = await setup(state, { stallOnClose: true });
it.slow()('should kill the browser on SIGTERM + SIGINT', async ({stallingWrapper}) => {
const wrapper = stallingWrapper;
process.kill(wrapper.child().pid, 'SIGTERM');
await wrapper.out('stalled');
process.kill(wrapper.child().pid, 'SIGINT');
@ -179,8 +181,8 @@ describe('Fixtures', function() {
});
describe('StackTrace', () => {
it('caller file path', async state => {
const stackTrace = require(path.join(state.playwrightPath, 'lib', 'utils', 'stackTrace'));
it('caller file path', async ({}) => {
const stackTrace = require(path.join(playwrightPath, 'lib', 'utils', 'stackTrace'));
const callme = require('./fixtures/callback');
const filePath = callme(() => {
return stackTrace.getCallerFilePath(path.join(__dirname, 'fixtures') + path.sep);

View File

@ -72,7 +72,6 @@ module.exports = {
{
files: [
'./defaultbrowsercontext.spec.js',
'./fixtures.spec.js',
],
environments: [customEnvironment],
},