mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
test: move fixtures to jest (#3010)
This commit is contained in:
parent
24f6d19e27
commit
096ec4c44f
@ -47,13 +47,13 @@ function parseStackFrame(frame: string): ParsedStackFrame | null {
|
|||||||
|
|
||||||
export function getCallerFilePath(ignorePrefix = PLAYWRIGHT_LIB_PATH): string | null {
|
export function getCallerFilePath(ignorePrefix = PLAYWRIGHT_LIB_PATH): string | null {
|
||||||
const error = new Error();
|
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.
|
// Find first stackframe that doesn't point to ignorePrefix.
|
||||||
for (const frame of stackFrames) {
|
for (const frame of stackFrames) {
|
||||||
const parsed = parseStackFrame(frame);
|
const parsed = parseStackFrame(frame);
|
||||||
if (!parsed)
|
if (!parsed)
|
||||||
return null;
|
return null;
|
||||||
if (parsed.filePath.startsWith(ignorePrefix) || parsed.filePath === __filename)
|
if (parsed.filePath.startsWith(ignorePrefix))
|
||||||
continue;
|
continue;
|
||||||
return parsed.filePath;
|
return parsed.filePath;
|
||||||
}
|
}
|
||||||
|
@ -17,24 +17,26 @@
|
|||||||
|
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const {spawn, execSync} = require('child_process');
|
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 {
|
class Wrapper {
|
||||||
constructor(state, extraOptions) {
|
constructor(browserType, defaultBrowserOptions, extraOptions) {
|
||||||
this._output = new Map();
|
this._output = new Map();
|
||||||
this._outputCallback = new Map();
|
this._outputCallback = new Map();
|
||||||
|
|
||||||
this._browserType = state.browserType;
|
this._browserType = browserType;
|
||||||
const launchOptions = {...state.defaultBrowserOptions,
|
const launchOptions = {...defaultBrowserOptions,
|
||||||
handleSIGINT: true,
|
handleSIGINT: true,
|
||||||
handleSIGTERM: true,
|
handleSIGTERM: true,
|
||||||
handleSIGHUP: true,
|
handleSIGHUP: true,
|
||||||
executablePath: state.browserType.executablePath(),
|
executablePath: browserType.executablePath(),
|
||||||
logger: undefined,
|
logger: undefined,
|
||||||
};
|
};
|
||||||
const options = {
|
const options = {
|
||||||
playwrightFile: path.join(state.playwrightPath, 'index'),
|
playwrightFile: path.join(playwrightPath, 'index'),
|
||||||
browserTypeName: state.browserType.name(),
|
browserTypeName: browserType.name(),
|
||||||
launchOptions,
|
launchOptions,
|
||||||
...extraOptions,
|
...extraOptions,
|
||||||
};
|
};
|
||||||
@ -89,15 +91,20 @@ class Wrapper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function setup(state, options = {}) {
|
registerFixture('wrapper', async ({browserType, defaultBrowserOptions}, test) => {
|
||||||
const wrapper = new Wrapper(state, options);
|
const wrapper = new Wrapper(browserType, defaultBrowserOptions);
|
||||||
await wrapper.connect();
|
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() {
|
describe('Fixtures', function() {
|
||||||
it.slow()('should close the browser when the node process closes', async state => {
|
it.slow()('should close the browser when the node process closes', async ({wrapper}) => {
|
||||||
const wrapper = await setup(state);
|
|
||||||
if (WIN)
|
if (WIN)
|
||||||
execSync(`taskkill /pid ${wrapper.child().pid} /T /F`);
|
execSync(`taskkill /pid ${wrapper.child().pid} /T /F`);
|
||||||
else
|
else
|
||||||
@ -107,10 +114,9 @@ describe('Fixtures', function() {
|
|||||||
// so we don't check it here.
|
// so we don't check it here.
|
||||||
});
|
});
|
||||||
|
|
||||||
describe.skip(WIN).skip(!HEADLESS)('signals', () => {
|
describe.skip(WIN || !HEADLESS)('signals', () => {
|
||||||
// Cannot reliably send signals on Windows.
|
// Cannot reliably send signals on Windows.
|
||||||
it.slow()('should report browser close signal', async state => {
|
it.slow()('should report browser close signal', async ({wrapper}) => {
|
||||||
const wrapper = await setup(state);
|
|
||||||
const pid = await wrapper.out('pid');
|
const pid = await wrapper.out('pid');
|
||||||
process.kill(-pid, 'SIGTERM');
|
process.kill(-pid, 'SIGTERM');
|
||||||
expect(await wrapper.out('exitCode')).toBe('null');
|
expect(await wrapper.out('exitCode')).toBe('null');
|
||||||
@ -118,8 +124,7 @@ describe('Fixtures', function() {
|
|||||||
process.kill(wrapper.child().pid);
|
process.kill(wrapper.child().pid);
|
||||||
await wrapper.childExitCode();
|
await wrapper.childExitCode();
|
||||||
});
|
});
|
||||||
it.slow()('should report browser close signal 2', async state => {
|
it.slow()('should report browser close signal 2', async ({wrapper}) => {
|
||||||
const wrapper = await setup(state);
|
|
||||||
const pid = await wrapper.out('pid');
|
const pid = await wrapper.out('pid');
|
||||||
process.kill(-pid, 'SIGKILL');
|
process.kill(-pid, 'SIGKILL');
|
||||||
expect(await wrapper.out('exitCode')).toBe('null');
|
expect(await wrapper.out('exitCode')).toBe('null');
|
||||||
@ -127,29 +132,26 @@ describe('Fixtures', function() {
|
|||||||
process.kill(wrapper.child().pid);
|
process.kill(wrapper.child().pid);
|
||||||
await wrapper.childExitCode();
|
await wrapper.childExitCode();
|
||||||
});
|
});
|
||||||
it.slow()('should close the browser on SIGINT', async state => {
|
it.slow()('should close the browser on SIGINT', async ({wrapper}) => {
|
||||||
const wrapper = await setup(state);
|
|
||||||
process.kill(wrapper.child().pid, 'SIGINT');
|
process.kill(wrapper.child().pid, 'SIGINT');
|
||||||
expect(await wrapper.out('exitCode')).toBe('0');
|
expect(await wrapper.out('exitCode')).toBe('0');
|
||||||
expect(await wrapper.out('signal')).toBe('null');
|
expect(await wrapper.out('signal')).toBe('null');
|
||||||
expect(await wrapper.childExitCode()).toBe(130);
|
expect(await wrapper.childExitCode()).toBe(130);
|
||||||
});
|
});
|
||||||
it.slow()('should close the browser on SIGTERM', async state => {
|
it.slow()('should close the browser on SIGTERM', async ({wrapper}) => {
|
||||||
const wrapper = await setup(state);
|
|
||||||
process.kill(wrapper.child().pid, 'SIGTERM');
|
process.kill(wrapper.child().pid, 'SIGTERM');
|
||||||
expect(await wrapper.out('exitCode')).toBe('0');
|
expect(await wrapper.out('exitCode')).toBe('0');
|
||||||
expect(await wrapper.out('signal')).toBe('null');
|
expect(await wrapper.out('signal')).toBe('null');
|
||||||
expect(await wrapper.childExitCode()).toBe(0);
|
expect(await wrapper.childExitCode()).toBe(0);
|
||||||
});
|
});
|
||||||
it.slow()('should close the browser on SIGHUP', async state => {
|
it.slow()('should close the browser on SIGHUP', async ({wrapper}) => {
|
||||||
const wrapper = await setup(state);
|
|
||||||
process.kill(wrapper.child().pid, 'SIGHUP');
|
process.kill(wrapper.child().pid, 'SIGHUP');
|
||||||
expect(await wrapper.out('exitCode')).toBe('0');
|
expect(await wrapper.out('exitCode')).toBe('0');
|
||||||
expect(await wrapper.out('signal')).toBe('null');
|
expect(await wrapper.out('signal')).toBe('null');
|
||||||
expect(await wrapper.childExitCode()).toBe(0);
|
expect(await wrapper.childExitCode()).toBe(0);
|
||||||
});
|
});
|
||||||
it.slow()('should kill the browser on double SIGINT', async state => {
|
it.slow()('should kill the browser on double SIGINT', async ({stallingWrapper}) => {
|
||||||
const wrapper = await setup(state, { stallOnClose: true });
|
const wrapper = stallingWrapper;
|
||||||
process.kill(wrapper.child().pid, 'SIGINT');
|
process.kill(wrapper.child().pid, 'SIGINT');
|
||||||
await wrapper.out('stalled');
|
await wrapper.out('stalled');
|
||||||
process.kill(wrapper.child().pid, 'SIGINT');
|
process.kill(wrapper.child().pid, 'SIGINT');
|
||||||
@ -157,8 +159,8 @@ describe('Fixtures', function() {
|
|||||||
expect(await wrapper.out('signal')).toBe('SIGKILL');
|
expect(await wrapper.out('signal')).toBe('SIGKILL');
|
||||||
expect(await wrapper.childExitCode()).toBe(130);
|
expect(await wrapper.childExitCode()).toBe(130);
|
||||||
});
|
});
|
||||||
it.slow()('should kill the browser on SIGINT + SIGTERM', async state => {
|
it.slow()('should kill the browser on SIGINT + SIGTERM', async ({stallingWrapper}) => {
|
||||||
const wrapper = await setup(state, { stallOnClose: true });
|
const wrapper = stallingWrapper;
|
||||||
process.kill(wrapper.child().pid, 'SIGINT');
|
process.kill(wrapper.child().pid, 'SIGINT');
|
||||||
await wrapper.out('stalled');
|
await wrapper.out('stalled');
|
||||||
process.kill(wrapper.child().pid, 'SIGTERM');
|
process.kill(wrapper.child().pid, 'SIGTERM');
|
||||||
@ -166,8 +168,8 @@ describe('Fixtures', function() {
|
|||||||
expect(await wrapper.out('signal')).toBe('SIGKILL');
|
expect(await wrapper.out('signal')).toBe('SIGKILL');
|
||||||
expect(await wrapper.childExitCode()).toBe(0);
|
expect(await wrapper.childExitCode()).toBe(0);
|
||||||
});
|
});
|
||||||
it.slow()('should kill the browser on SIGTERM + SIGINT', async state => {
|
it.slow()('should kill the browser on SIGTERM + SIGINT', async ({stallingWrapper}) => {
|
||||||
const wrapper = await setup(state, { stallOnClose: true });
|
const wrapper = stallingWrapper;
|
||||||
process.kill(wrapper.child().pid, 'SIGTERM');
|
process.kill(wrapper.child().pid, 'SIGTERM');
|
||||||
await wrapper.out('stalled');
|
await wrapper.out('stalled');
|
||||||
process.kill(wrapper.child().pid, 'SIGINT');
|
process.kill(wrapper.child().pid, 'SIGINT');
|
||||||
@ -179,8 +181,8 @@ describe('Fixtures', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('StackTrace', () => {
|
describe('StackTrace', () => {
|
||||||
it('caller file path', async state => {
|
it('caller file path', async ({}) => {
|
||||||
const stackTrace = require(path.join(state.playwrightPath, 'lib', 'utils', 'stackTrace'));
|
const stackTrace = require(path.join(playwrightPath, 'lib', 'utils', 'stackTrace'));
|
||||||
const callme = require('./fixtures/callback');
|
const callme = require('./fixtures/callback');
|
||||||
const filePath = callme(() => {
|
const filePath = callme(() => {
|
||||||
return stackTrace.getCallerFilePath(path.join(__dirname, 'fixtures') + path.sep);
|
return stackTrace.getCallerFilePath(path.join(__dirname, 'fixtures') + path.sep);
|
@ -72,7 +72,6 @@ module.exports = {
|
|||||||
{
|
{
|
||||||
files: [
|
files: [
|
||||||
'./defaultbrowsercontext.spec.js',
|
'./defaultbrowsercontext.spec.js',
|
||||||
'./fixtures.spec.js',
|
|
||||||
],
|
],
|
||||||
environments: [customEnvironment],
|
environments: [customEnvironment],
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user