feat(test.info): expose information on the currently running test (#10708)

This commit is contained in:
Pavel Feldman 2021-12-06 09:25:11 -08:00 committed by GitHub
parent 6aab64b02a
commit 518d67add5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 1 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -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 };

View File

@ -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');
});

View File

@ -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 };