From 3688e74e3e836c206b281dc9580c1d6ae125c977 Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Thu, 24 Mar 2022 07:33:33 -0700 Subject: [PATCH] fix(list mode): print errors to stderr (#13016) `--list` mode now prints any errors encountered during test collection, for example syntax errors, to `stderr`. --- packages/playwright-test/src/runner.ts | 9 +++++++++ tests/playwright-test/list-mode.spec.ts | 13 ++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/playwright-test/src/runner.ts b/packages/playwright-test/src/runner.ts index 68c00e2ff8..51af2d9d1d 100644 --- a/packages/playwright-test/src/runner.ts +++ b/packages/playwright-test/src/runner.ts @@ -25,6 +25,7 @@ import { TestCase, Suite } from './test'; import { Loader } from './loader'; import { FullResult, Reporter, TestError } from '../types/testReporter'; import { Multiplexer } from './reporters/multiplexer'; +import { formatError } from './reporters/base'; import DotReporter from './reporters/dot'; import GitHubReporter from './reporters/github'; import LineReporter from './reporters/line'; @@ -684,7 +685,10 @@ function createTestGroups(rootSuite: Suite): TestGroup[] { } class ListModeReporter implements Reporter { + private config!: FullConfig; + onBegin(config: FullConfig, suite: Suite): void { + this.config = config; // eslint-disable-next-line no-console console.log(`Listing tests:`); const tests = suite.allTests(); @@ -701,6 +705,11 @@ class ListModeReporter implements Reporter { // eslint-disable-next-line no-console console.log(`Total: ${tests.length} ${tests.length === 1 ? 'test' : 'tests'} in ${files.size} ${files.size === 1 ? 'file' : 'files'}`); } + + onError(error: TestError) { + // eslint-disable-next-line no-console + console.error('\n' + formatError(this.config, error, false).message); + } } function createForbidOnlyError(config: FullConfig, onlyTestsAndSuites: (TestCase | Suite)[]): TestError { diff --git a/tests/playwright-test/list-mode.spec.ts b/tests/playwright-test/list-mode.spec.ts index c6eaac536d..1c93ea32f1 100644 --- a/tests/playwright-test/list-mode.spec.ts +++ b/tests/playwright-test/list-mode.spec.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { test, expect } from './playwright-test-fixtures'; +import { test, expect, stripAnsi } from './playwright-test-fixtures'; import path from 'path'; import fs from 'fs'; @@ -140,3 +140,14 @@ test('outputDir should not be removed', async ({ runInlineTest }, testInfo) => { expect(result2.exitCode).toBe(0); expect(fs.existsSync(path.join(outputDir, 'a-my-test', 'myfile.txt'))).toBe(true); }); + +test('should report errors', async ({ runInlineTest }) => { + const result = await runInlineTest({ + 'a.test.js': ` + const oh = ''; + oh = 2; + ` + }, { 'list': true }); + expect(result.exitCode).toBe(1); + expect(stripAnsi(result.output)).toContain('> 6 | oh = 2;'); +});