Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

test: move fixtures to jest (#3010)

Pavel Feldman096ec4c44f0d
diff --git a/src/utils/stackTrace.ts b/src/utils/stackTrace.ts
index 45fe0190d8..993a8d7468 100644
--- a/src/utils/stackTrace.ts
+++ b/src/utils/stackTrace.ts
@@ -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;
}
diff --git a/test/fixtures.spec.js b/test/fixtures.jest.js
similarity index 77%
rename from test/fixtures.spec.js
rename to test/fixtures.jest.js
index 81136a4810..c0d36f2ace 100644
--- a/test/fixtures.spec.js
+++ b/test/fixtures.jest.js
@@ -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);
diff --git a/test/test.config.js b/test/test.config.js
index f84987b183..ffa6285373 100644
--- a/test/test.config.js
+++ b/test/test.config.js
@@ -72,7 +72,6 @@ module.exports = {
{
files: [
'./defaultbrowsercontext.spec.js',
- './fixtures.spec.js',
],
environments: [customEnvironment],
},