Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add option to force fail dependent tests #34081

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/playwright/src/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
reporter: takeFirst(configCLIOverrides.reporter, resolveReporters(userConfig.reporter, configDir), [[defaultReporter]]),
reportSlowTests: takeFirst(userConfig.reportSlowTests, { max: 5, threshold: 15000 }),
quiet: takeFirst(configCLIOverrides.quiet, userConfig.quiet, false),
failDependentTests: takeFirst(configCLIOverrides.failDependentTests, userConfig.failDependentTests, false),

Check failure on line 97 in packages/playwright/src/common/config.ts

View workflow job for this annotation

GitHub Actions / docs & lint

Object literal may only specify known properties, and 'failDependentTests' does not exist in type 'FullConfig<{}, {}>'.

Check failure on line 97 in packages/playwright/src/common/config.ts

View workflow job for this annotation

GitHub Actions / docs & lint

Property 'failDependentTests' does not exist on type 'Config<{}, {}>'.
projects: [],
shard: takeFirst(configCLIOverrides.shard, userConfig.shard, null),
updateSnapshots: takeFirst(configCLIOverrides.updateSnapshots, userConfig.updateSnapshots, 'missing'),
Expand Down
1 change: 1 addition & 0 deletions packages/playwright/src/common/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export type ConfigCLIOverrides = {
outputDir?: string;
preserveOutputDir?: boolean;
quiet?: boolean;
failDependentTests?: boolean;
repeatEach?: number;
retries?: number;
reporter?: ReporterDescription[];
Expand Down
18 changes: 18 additions & 0 deletions packages/playwright/src/common/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,24 @@ export class TestCase extends Base implements reporterTypes.TestCase {
return result;
}

_appendFailedTestResult(): reporterTypes.TestResult {
const result: reporterTypes.TestResult = {
retry: this.results.length,
parallelIndex: -1,
workerIndex: -1,
duration: 0,
startTime: new Date(),
stdout: [],
stderr: [],
attachments: [],
status: 'failed',
steps: [],
errors: [{ message: 'dependency test failed' }]
};
this.results.push(result);
return result;
}

_grepTitle() {
const path: string[] = [];
this.parent._collectGrepTitlePath(path);
Expand Down
1 change: 1 addition & 0 deletions packages/playwright/src/isomorphic/teleReceiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@
configFile: '',
rootDir: '',
quiet: false,
failDependentTests: false,

Check failure on line 595 in packages/playwright/src/isomorphic/teleReceiver.ts

View workflow job for this annotation

GitHub Actions / docs & lint

Object literal may only specify known properties, and 'failDependentTests' does not exist in type 'FullConfig<{}, {}>'.
shard: null,
updateSnapshots: 'missing',
updateSourceMethod: 'patch',
Expand Down
2 changes: 2 additions & 0 deletions packages/playwright/src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ function overridesFromOptions(options: { [key: string]: any }): ConfigCLIOverrid
maxFailures: options.x ? 1 : (options.maxFailures ? parseInt(options.maxFailures, 10) : undefined),
outputDir: options.output ? path.resolve(process.cwd(), options.output) : undefined,
quiet: options.quiet ? options.quiet : undefined,
failDependentTests: options.failDependentTests ? options.failDependentTests : undefined,
repeatEach: options.repeatEach ? parseInt(options.repeatEach, 10) : undefined,
retries: options.retries ? parseInt(options.retries, 10) : undefined,
reporter: resolveReporterOption(options.reporter),
Expand Down Expand Up @@ -375,6 +376,7 @@ const testOptions: [string, string][] = [
['--pass-with-no-tests', `Makes test run succeed even if no tests were found`],
['--project <project-name...>', `Only run tests from the specified list of projects, supports '*' wildcard (default: run all projects)`],
['--quiet', `Suppress stdio`],
['--failDependentTests', `Automatically fail all project tests if project dependencies fail`],
['--repeat-each <N>', `Run each test N times (default: 1)`],
['--reporter <reporter>', `Reporter to use, comma-separated, can be ${builtInReporters.map(name => `"${name}"`).join(', ')} (default: "${defaultReporter}")`],
['--retries <retries>', `Maximum retry count for flaky tests, zero for no retries (default: no retries)`],
Expand Down
5 changes: 3 additions & 2 deletions packages/playwright/src/runner/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@
// We don't want to run the test groups belonging to the projects
// that depend on the projects that failed previously.
const phaseTestGroups: TestGroup[] = [];
for (const { project, testGroups } of projects) {
for (const { project, testGroups, projectSuite } of projects) {
// Inherit extra environment variables from dependencies.
let extraEnv: Record<string, string | undefined> = {};
for (const dep of project.deps)
Expand All @@ -367,8 +367,9 @@
const hasFailedDeps = project.deps.some(p => !successfulProjects.has(p));
if (!hasFailedDeps)
phaseTestGroups.push(...testGroups);
else if (project.fullConfig.config.failDependentTests)

Check failure on line 370 in packages/playwright/src/runner/tasks.ts

View workflow job for this annotation

GitHub Actions / docs & lint

Property 'failDependentTests' does not exist on type 'FullConfig<{}, {}>'.
projectSuite.allTests().forEach(test => test._appendFailedTestResult());
}

if (phaseTestGroups.length) {
await dispatcher!.run(phaseTestGroups, extraEnvByProjectId);
await dispatcher.stop();
Expand Down
19 changes: 19 additions & 0 deletions packages/playwright/types/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,23 @@ interface TestConfig<TestArgs = {}, WorkerArgs = {}> {
*/
quiet?: boolean;

/**
* Whether to automatically fail all project tests if one of its dependencies fail
*
* **Usage**
*
* ```js
* // playwright.config.ts
* import { defineConfig } from '@playwright/test';
*
* export default defineConfig({
* failDependentTests: true
* });
* ```
*
*/
failDependentTests?: boolean;

/**
* The number of times to repeat each test, useful for debugging flaky tests.
*
Expand Down Expand Up @@ -1805,6 +1822,8 @@ export interface FullConfig<TestArgs = {}, WorkerArgs = {}> {
*/
quiet: boolean;

failDependentTests: boolean;

/**
* See [testConfig.reportSlowTests](https://playwright.dev/docs/api/class-testconfig#test-config-report-slow-tests).
*/
Expand Down
27 changes: 27 additions & 0 deletions tests/playwright-test/playwright.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -894,3 +894,30 @@ test('page.pause() should disable test timeout', async ({ runInlineTest }) => {
expect(result.passed).toBe(1);
expect(result.output).toContain('success!');
});

test('should automatically fail tests when their dependency tests fail', async ({ runInlineTest }) => {
const result = await runInlineTest({
'playwright.config.js': `
module.exports = {
failDependentTests: true,
projects: [ { testMatch: /dependent\.test\.ts/, dependencies: ['a'] }, { name: 'a', testMatch: /setup\.test\.ts/ } ]
}
`,
'setup.test.ts': `
import { test, expect } from '@playwright/test';
test('fail', async ({ page }) => {
test.expect(1).toBeFalsy();
});
`,
'dependent.test.ts': `
import { test, expect } from '@playwright/test';
test('pass', async ({ page }) => {
test.expect(0).toBeFalsy();
});
`
}, { workers: 1 });
expect(result.exitCode).toBe(1);
expect(result.passed).toBe(0);
expect(result.failed).toBe(2);
expect(result.skipped).toBe(0);
});
Loading