Skip to content

Commit

Permalink
fix(test-runner): accept relative paths for outputDir
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelEinbinder committed Jun 15, 2021
1 parent 2b980da commit 21358bf
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/src/test-advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ In addition to everything from the [`workerInfo`](#workerinfo), the following in
- `annotations` - [Annotations](./test-annotations.md) that were added to the test.
- `snapshotSuffix: string` - Suffix used to locate snapshots for the test.
- `snapshotPath(snapshotName: string)` - Function that returns the full path to a particular snapshot for the test.
- `outputDir: string` - Absolute path to the output directory for this test run.
- `outputDir: string` - Path to the output directory for this test run.
- `outputPath(...pathSegments: string[])` - Function that returns the full path to a particular output artifact for the test.

The following information is accessible after the test body has finished, in fixture teardown:
Expand Down
8 changes: 4 additions & 4 deletions src/test/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,13 @@ export class Loader {
let testDir = takeFirst(projectConfig.testDir, rootDir);
if (!path.isAbsolute(testDir))
testDir = path.resolve(rootDir, testDir);

let outputDir = takeFirst(this._configOverrides.outputDir, projectConfig.outputDir, this._config.outputDir, path.resolve(process.cwd(), 'test-results'))
if (!path.isAbsolute(outputDir))
outputDir = path.resolve(rootDir, outputDir);
const fullProject: FullProject = {
define: takeFirst(this._configOverrides.define, projectConfig.define, this._config.define, []),
expect: takeFirst(this._configOverrides.expect, projectConfig.expect, this._config.expect, undefined),
outputDir: takeFirst(this._configOverrides.outputDir, projectConfig.outputDir, this._config.outputDir, path.resolve(process.cwd(), 'test-results')),
outputDir,
repeatEach: takeFirst(this._configOverrides.repeatEach, projectConfig.repeatEach, this._config.repeatEach, 1),
retries: takeFirst(this._configOverrides.retries, projectConfig.retries, this._config.retries, 0),
metadata: takeFirst(this._configOverrides.metadata, projectConfig.metadata, this._config.metadata, undefined),
Expand Down Expand Up @@ -347,8 +349,6 @@ function validateProject(project: Project, title: string) {
if ('outputDir' in project && project.outputDir !== undefined) {
if (typeof project.outputDir !== 'string')
throw new Error(`${title}.outputDir must be a string`);
if (!path.isAbsolute(project.outputDir))
throw new Error(`${title}.outputDir must be an absolute path`);
}

if ('repeatEach' in project && project.repeatEach !== undefined) {
Expand Down
5 changes: 4 additions & 1 deletion tests/playwright-test/playwright-test-fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ async function runPlaywrightTest(baseDir: string, params: any, env: Env): Promis
additionalArgs = params[key];
continue;
}
if (key === 'usesCustomOutputDir')
continue;
for (const value of Array.isArray(params[key]) ? params[key] : [params[key]]) {
const k = key.startsWith('-') ? key : '--' + key;
paramList.push(params[key] === true ? `${k}` : `${k}=${value}`);
Expand All @@ -123,8 +125,9 @@ async function runPlaywrightTest(baseDir: string, params: any, env: Env): Promis
const outputDir = path.join(baseDir, 'test-results');
const reportFile = path.join(outputDir, 'report.json');
const args = [path.join(__dirname, '..', '..', 'lib', 'cli', 'cli.js'), 'test'];
if (!params.usesCustomOutputDir)
args.push('--output=' + outputDir)
args.push(
'--output=' + outputDir,
'--reporter=dot,json',
'--workers=2',
...paramList
Expand Down
18 changes: 18 additions & 0 deletions tests/playwright-test/test-output-dir.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,21 @@ test('should not remove folders on non-CI', async ({ runInlineTest }, testInfo)
expect(fs.existsSync(testInfo.outputPath('test-results', 'dir-my-test-test-1-retry1'))).toBe(true);
expect(fs.existsSync(testInfo.outputPath('test-results', 'dir-my-test-test-1-retry2'))).toBe(true);
});


test('should accept a relative path for outputDir', async ({ runInlineTest }, testInfo) => {
const result = await runInlineTest({
'my-test.spec.js': `
const { test } = pwt;
test('test', async ({}, testInfo) => {
expect(testInfo.outputDir).toBe('${path.join(testInfo.outputDir, './my-output-dir', 'my-test-test')}');
});
`,
'playwright.config.js': `
module.exports = { projects: [
{ outputDir: './my-output-dir' },
] };
`,
}, {usesCustomOutputDir: true});
expect(result.exitCode).toBe(0);
});

0 comments on commit 21358bf

Please sign in to comment.