fix(test runner): update TestInfo.duration before running afterEach hooks (#10228)
This commit is contained in:
parent
9ec3e7cd52
commit
9622704a8a
|
|
@ -81,7 +81,7 @@ Processed configuration from the [configuration file](./test-configuration.md).
|
||||||
## property: TestInfo.duration
|
## property: TestInfo.duration
|
||||||
- type: <[int]>
|
- type: <[int]>
|
||||||
|
|
||||||
The number of milliseconds the test took to finish. Always zero before the test finishes, either successfully or not.
|
The number of milliseconds the test took to finish. Always zero before the test finishes, either successfully or not. Can be used in [`method: Test.afterEach`] hook.
|
||||||
|
|
||||||
|
|
||||||
## property: TestInfo.error
|
## property: TestInfo.error
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,5 @@ export interface TestStepInternal {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface TestInfoImpl extends TestInfo {
|
export interface TestInfoImpl extends TestInfo {
|
||||||
_testFinished: Promise<void>;
|
|
||||||
_addStep: (data: Omit<TestStepInternal, 'complete'>) => TestStepInternal;
|
_addStep: (data: Omit<TestStepInternal, 'complete'>) => TestStepInternal;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -246,7 +246,6 @@ export class WorkerRunner extends EventEmitter {
|
||||||
return path.join(this._project.config.snapshotDir, relativeTestFilePath + '-snapshots');
|
return path.join(this._project.config.snapshotDir, relativeTestFilePath + '-snapshots');
|
||||||
})();
|
})();
|
||||||
|
|
||||||
let testFinishedCallback = () => {};
|
|
||||||
let lastStepId = 0;
|
let lastStepId = 0;
|
||||||
const testInfo: TestInfoImpl = {
|
const testInfo: TestInfoImpl = {
|
||||||
workerIndex: this._params.workerIndex,
|
workerIndex: this._params.workerIndex,
|
||||||
|
|
@ -299,7 +298,6 @@ export class WorkerRunner extends EventEmitter {
|
||||||
if (deadlineRunner)
|
if (deadlineRunner)
|
||||||
deadlineRunner.updateDeadline(deadline());
|
deadlineRunner.updateDeadline(deadline());
|
||||||
},
|
},
|
||||||
_testFinished: new Promise(f => testFinishedCallback = f),
|
|
||||||
_addStep: data => {
|
_addStep: data => {
|
||||||
const stepId = `${data.category}@${data.title}@${++lastStepId}`;
|
const stepId = `${data.category}@${data.title}@${++lastStepId}`;
|
||||||
let callbackHandled = false;
|
let callbackHandled = false;
|
||||||
|
|
@ -389,7 +387,7 @@ export class WorkerRunner extends EventEmitter {
|
||||||
// Do not overwrite test failure upon hook timeout.
|
// Do not overwrite test failure upon hook timeout.
|
||||||
if (result.timedOut && testInfo.status === 'passed')
|
if (result.timedOut && testInfo.status === 'passed')
|
||||||
testInfo.status = 'timedOut';
|
testInfo.status = 'timedOut';
|
||||||
testFinishedCallback();
|
testInfo.duration = monotonicTime() - startTime;
|
||||||
|
|
||||||
if (!result.timedOut) {
|
if (!result.timedOut) {
|
||||||
this._currentDeadlineRunner = deadlineRunner = new DeadlineRunner(this._runAfterHooks(test, testInfo), deadline());
|
this._currentDeadlineRunner = deadlineRunner = new DeadlineRunner(this._runAfterHooks(test, testInfo), deadline());
|
||||||
|
|
|
||||||
1
packages/playwright-test/types/test.d.ts
vendored
1
packages/playwright-test/types/test.d.ts
vendored
|
|
@ -1329,6 +1329,7 @@ export interface TestInfo {
|
||||||
retry: number;
|
retry: number;
|
||||||
/**
|
/**
|
||||||
* The number of milliseconds the test took to finish. Always zero before the test finishes, either successfully or not.
|
* The number of milliseconds the test took to finish. Always zero before the test finishes, either successfully or not.
|
||||||
|
* Can be used in [test.afterEach(hookFunction)](https://playwright.dev/docs/api/class-test#test-after-each) hook.
|
||||||
*/
|
*/
|
||||||
duration: number;
|
duration: number;
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -494,12 +494,13 @@ test('beforeAll and afterAll timeouts at the same time should be reported', asyn
|
||||||
expect(result.output).toContain('Timeout of 1000ms exceeded in beforeAll hook.');
|
expect(result.output).toContain('Timeout of 1000ms exceeded in beforeAll hook.');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('afterEach should get the test status right away', async ({ runInlineTest }) => {
|
test('afterEach should get the test status and duration right away', async ({ runInlineTest }) => {
|
||||||
const result = await runInlineTest({
|
const result = await runInlineTest({
|
||||||
'a.test.js': `
|
'a.test.js': `
|
||||||
const { test } = pwt;
|
const { test } = pwt;
|
||||||
test.afterEach(({}, testInfo) => {
|
test.afterEach(({}, testInfo) => {
|
||||||
console.log('\\n%%' + testInfo.title + ': ' + testInfo.status);
|
const duration = testInfo.duration ? 'XXms' : 'none';
|
||||||
|
console.log('\\n%%' + testInfo.title + ': ' + testInfo.status + '; ' + duration);
|
||||||
});
|
});
|
||||||
test('failing', () => {
|
test('failing', () => {
|
||||||
throw new Error('Oh my!');
|
throw new Error('Oh my!');
|
||||||
|
|
@ -513,8 +514,8 @@ test('afterEach should get the test status right away', async ({ runInlineTest }
|
||||||
expect(result.exitCode).toBe(1);
|
expect(result.exitCode).toBe(1);
|
||||||
expect(result.failed).toBe(2);
|
expect(result.failed).toBe(2);
|
||||||
expect(result.output.split('\n').filter(line => line.startsWith('%%'))).toEqual([
|
expect(result.output.split('\n').filter(line => line.startsWith('%%'))).toEqual([
|
||||||
'%%failing: failed',
|
'%%failing: failed; XXms',
|
||||||
'%%timing out: timedOut',
|
'%%timing out: timedOut; XXms',
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue