mirror of
https://github.com/microsoft/playwright.git
synced 2024-11-24 14:55:38 +03:00
feat(test.info): expose information on the currently running test (#10708)
This commit is contained in:
parent
6aab64b02a
commit
518d67add5
@ -800,6 +800,10 @@ A function that returns whether to mark as "fixme", based on test fixtures. Test
|
||||
An optional description that will be reflected in a test report.
|
||||
|
||||
|
||||
## method: Test.info
|
||||
- returns: <[TestInfo]>
|
||||
|
||||
Returns information about the currently running test. This method can only be called during the test execution, otherwise it throws.
|
||||
|
||||
## method: Test.only
|
||||
|
||||
|
@ -54,6 +54,12 @@ export class TestTypeImpl {
|
||||
test.use = wrapFunctionWithLocation(this._use.bind(this));
|
||||
test.extend = wrapFunctionWithLocation(this._extend.bind(this));
|
||||
test.extendTest = wrapFunctionWithLocation(this._extendTest.bind(this));
|
||||
test.info = () => {
|
||||
const result = currentTestInfo();
|
||||
if (!result)
|
||||
throw new Error('test.info() can only be called while test is running');
|
||||
return result;
|
||||
};
|
||||
this.test = test;
|
||||
}
|
||||
|
||||
|
5
packages/playwright-test/types/test.d.ts
vendored
5
packages/playwright-test/types/test.d.ts
vendored
@ -2599,6 +2599,11 @@ export interface TestType<TestArgs extends KeyValue, WorkerArgs extends KeyValue
|
||||
*/
|
||||
extend<T, W extends KeyValue = {}>(fixtures: Fixtures<T, W, TestArgs, WorkerArgs>): TestType<TestArgs & T, WorkerArgs & W>;
|
||||
extendTest<T, W>(other: TestType<T, W>): TestType<TestArgs & T, WorkerArgs & W>;
|
||||
/**
|
||||
* Returns information about the currently running test. This method can only be called during the test execution,
|
||||
* otherwise it throws.
|
||||
*/
|
||||
info(): TestInfo;
|
||||
}
|
||||
|
||||
type KeyValue = { [key: string]: any };
|
||||
|
@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { test, expect } from './playwright-test-fixtures';
|
||||
import { test, expect, stripAscii } from './playwright-test-fixtures';
|
||||
|
||||
test('should work directly', async ({ runInlineTest }) => {
|
||||
const result = await runInlineTest({
|
||||
@ -52,3 +52,39 @@ test('should work via fixture', async ({ runInlineTest }) => {
|
||||
});
|
||||
expect(result.exitCode).toBe(0);
|
||||
});
|
||||
|
||||
test('should work via test.info', async ({ runInlineTest }) => {
|
||||
const result = await runInlineTest({
|
||||
'helper.ts': `
|
||||
export const test = pwt.test.extend({
|
||||
title: async ({}, run) => {
|
||||
await run(pwt.test.info().title);
|
||||
},
|
||||
});
|
||||
`,
|
||||
'a.test.js': `
|
||||
const { test } = require('./helper');
|
||||
test('test 1', async ({title}) => {
|
||||
expect(test.info().title).toBe('test 1');
|
||||
expect(title).toBe('test 1');
|
||||
});
|
||||
test('test 2', async ({title}) => {
|
||||
expect(title).toBe('test 2');
|
||||
});
|
||||
`,
|
||||
});
|
||||
expect(result.exitCode).toBe(0);
|
||||
});
|
||||
|
||||
test('should throw outside test', async ({ runInlineTest }) => {
|
||||
const result = await runInlineTest({
|
||||
'a.test.js': `
|
||||
const { test } = pwt;
|
||||
test.info();
|
||||
test('test 1', async ({title}) => {});
|
||||
`,
|
||||
});
|
||||
const output = stripAscii(result.output);
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(output).toContain('test.info() can only be called while test is running');
|
||||
});
|
||||
|
1
utils/generate_types/overrides-test.d.ts
vendored
1
utils/generate_types/overrides-test.d.ts
vendored
@ -267,6 +267,7 @@ export interface TestType<TestArgs extends KeyValue, WorkerArgs extends KeyValue
|
||||
expect: Expect;
|
||||
extend<T, W extends KeyValue = {}>(fixtures: Fixtures<T, W, TestArgs, WorkerArgs>): TestType<TestArgs & T, WorkerArgs & W>;
|
||||
extendTest<T, W>(other: TestType<T, W>): TestType<TestArgs & T, WorkerArgs & W>;
|
||||
info(): TestInfo;
|
||||
}
|
||||
|
||||
type KeyValue = { [key: string]: any };
|
||||
|
Loading…
Reference in New Issue
Block a user