feat(test runner): add --trace cli option (#16277)

This commit is contained in:
Dmitry Gozman 2022-08-05 09:20:39 -07:00 committed by GitHub
parent e424a36945
commit cb30cb4eb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -26,6 +26,7 @@ import { stopProfiling, startProfiling } from './profiler';
import type { TestFileFilter } from './util';
import { showHTMLReport } from './reporters/html';
import { baseFullConfig, defaultTimeout, fileIsModule } from './loader';
import type { TraceMode } from './types';
export function addTestCommands(program: Command) {
addTestCommand(program);
@ -56,6 +57,7 @@ function addTestCommand(program: Command) {
command.option('--shard <shard>', `Shard tests and execute only the selected shard, specify in the form "current/all", 1-based, for example "3/5"`);
command.option('--project <project-name...>', `Only run tests from the specified list of projects (default: run all projects)`);
command.option('--timeout <timeout>', `Specify test timeout threshold in milliseconds, zero for unlimited (default: ${defaultTimeout})`);
command.option('--trace <mode>', `Force tracing mode, can be ${kTraceModes.map(mode => `"${mode}"`).join(', ')}`);
command.option('-u, --update-snapshots', `Update snapshots with actual results (default: only create missing snapshots)`);
command.option('-x', `Stop after the first failure`);
command.action(async (args, opts) => {
@ -130,6 +132,12 @@ async function runTests(args: string[], opts: { [key: string]: any }) {
overrides.workers = 1;
process.env.PWDEBUG = '1';
}
if (opts.trace) {
if (!kTraceModes.includes(opts.trace))
throw new Error(`Unsupported trace mode "${opts.trace}", must be one of ${kTraceModes.map(mode => `"${mode}"`).join(', ')}`);
overrides.use = overrides.use || {};
overrides.use.trace = opts.trace;
}
// When no --config option is passed, let's look for the config file in the current directory.
const configFileOrDirectory = opts.config ? path.resolve(process.cwd(), opts.config) : process.cwd();
@ -250,3 +258,5 @@ function restartWithExperimentalTsEsm(configFile: string | null): boolean {
});
return true;
}
const kTraceModes: TraceMode[] = ['on', 'off', 'on-first-retry', 'retain-on-failure'];

View File

@ -267,6 +267,20 @@ test('should retain traces for interrupted tests', async ({ runInlineTest }, tes
expect(fs.existsSync(testInfo.outputPath('test-results', 'b-test-2', 'trace.zip'))).toBeTruthy();
});
test('should respect --trace', async ({ runInlineTest }, testInfo) => {
const result = await runInlineTest({
'a.spec.ts': `
pwt.test('test 1', async ({ page }) => {
await page.goto('about:blank');
});
`,
}, { trace: 'on' });
expect(result.exitCode).toBe(0);
expect(result.passed).toBe(1);
expect(fs.existsSync(testInfo.outputPath('test-results', 'a-test-1', 'trace.zip'))).toBeTruthy();
});
async function parseTrace(file: string): Promise<Map<string, Buffer>> {
const zipFS = new ZipFile(file);
const resources = new Map<string, Buffer>();