fix(test-runner): list mode should print tests (#7665)

It was not doing anything before.
This commit is contained in:
Dmitry Gozman 2021-07-16 22:34:55 -07:00 committed by GitHub
parent 167db03f05
commit 602d815981
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 10 deletions

View File

@ -34,7 +34,7 @@ import JUnitReporter from './reporters/junit';
import EmptyReporter from './reporters/empty';
import { ProjectImpl } from './project';
import { Minimatch } from 'minimatch';
import { Config } from './types';
import { Config, FullConfig } from './types';
import { LaunchServers } from './launchServer';
const removeFolderAsync = promisify(rimraf);
@ -93,7 +93,7 @@ export class Runner {
}
async run(list: boolean, filePatternFilters: FilePatternFilter[], projectName?: string): Promise<RunResultStatus> {
this._reporter = await this._createReporter();
this._reporter = list ? new ListModeReporter() : await this._createReporter();
const config = this._loader.fullConfig();
const globalDeadline = config.globalTimeout ? config.globalTimeout + monotonicTime() : undefined;
const { result, timedOut } = await raceAgainstDeadline(this._run(list, filePatternFilters, projectName), globalDeadline);
@ -408,3 +408,20 @@ function getClashingTestsPerSuite(rootSuite: Suite): Map<string, Test[]> {
function buildItemLocation(rootDir: string, testOrSuite: Suite | Test) {
return `${path.relative(rootDir, testOrSuite.location.file)}:${testOrSuite.location.line}`;
}
class ListModeReporter implements Reporter {
onBegin(config: FullConfig, suite: Suite): void {
console.log(`Listing tests:`);
const tests = suite.allTests();
const files = new Set<string>();
for (const test of tests) {
// root, project, file, ...describes, test
const [, projectName, , ...titles] = test.titlePath();
const location = `${path.relative(config.rootDir, test.location.file)}:${test.location.line}:${test.location.column}`;
const projectTitle = projectName ? `[${projectName}] ` : '';
console.log(` ${projectTitle}${location} ${titles.join(' ')}`);
files.add(test.location.file);
}
console.log(`Total: ${tests.length} ${tests.length === 1 ? 'test' : 'tests'} in ${files.size} ${files.size === 1 ? 'file' : 'files'}`);
}
}

View File

@ -139,7 +139,6 @@ export class Test extends Base implements reporterTypes.Test {
}
status(): 'skipped' | 'expected' | 'unexpected' | 'flaky' {
// List mode bail out.
if (!this.results.length)
return 'skipped';
if (this.results.length === 1 && this.expectedStatus === this.results[0].status)

View File

@ -108,3 +108,19 @@ test('should report projects', async ({ runInlineTest }, testInfo) => {
expect(result.report.suites[0].specs[0].tests[0].projectName).toBe('p1');
expect(result.report.suites[0].specs[0].tests[1].projectName).toBe('p2');
});
test('should have relative always-posix paths', async ({ runInlineTest }) => {
const result = await runInlineTest({
'a.test.js': `
const { test } = pwt;
test('math works!', async ({}) => {
expect(1 + 1).toBe(2);
});
`
});
expect(result.exitCode).toBe(0);
expect(result.report.config.rootDir.indexOf(path.win32.sep)).toBe(-1);
expect(result.report.suites[0].specs[0].file).toBe('a.test.js');
expect(result.report.suites[0].specs[0].line).toBe(6);
expect(result.report.suites[0].specs[0].column).toBe(7);
});

View File

@ -14,21 +14,30 @@
* limitations under the License.
*/
import path from 'path';
import { test, expect } from './playwright-test-fixtures';
test('should have relative always-posix paths', async ({ runInlineTest }) => {
test('should list tests', async ({ runInlineTest }) => {
const result = await runInlineTest({
'playwright.config.ts': `
module.exports = { projects: [{ name: 'foo' }, {}] };
`,
'a.test.js': `
const { test } = pwt;
test('math works!', async ({}) => {
test('example1', async ({}) => {
expect(1 + 1).toBe(2);
});
test('example2', async ({}) => {
expect(1 + 1).toBe(2);
});
`
}, { 'list': true });
expect(result.exitCode).toBe(0);
expect(result.report.config.rootDir.indexOf(path.win32.sep)).toBe(-1);
expect(result.report.suites[0].specs[0].file).toBe('a.test.js');
expect(result.report.suites[0].specs[0].line).toBe(6);
expect(result.report.suites[0].specs[0].column).toBe(7);
expect(result.output).toContain([
`Listing tests:`,
` [foo] a.test.js:6:7 example1`,
` [foo] a.test.js:9:7 example2`,
` a.test.js:6:7 example1`,
` a.test.js:9:7 example2`,
`Total: 4 tests in 1 file`
].join('\n'));
});