Skip to content

Commit

Permalink
chore: rely upon playwright source map handling (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman authored Nov 30, 2023
1 parent 39d4875 commit a7dd533
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 40 deletions.
6 changes: 3 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,10 +608,10 @@ located next to Run / Debug Tests toolbar buttons.`);
await this._listTestsInAllModels(files);
}

private _listTestsInAllModels(files: string[]): Promise<void> {
private _listTestsInAllModels(inputFiles: string[]): Promise<void> {
// Perform coalescing listTests calls to avoid multiple
// 'list tests' processes running at the same time.
if (!files.length)
if (!inputFiles.length)
return Promise.resolve();

if (!this._filesPendingListTests) {
Expand Down Expand Up @@ -647,7 +647,7 @@ located next to Run / Debug Tests toolbar buttons.`);
};
}

for (const file of files)
for (const file of inputFiles)
this._filesPendingListTests.files.add(file);

return this._filesPendingListTests.promise;
Expand Down
33 changes: 3 additions & 30 deletions src/testModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export class TestModel {
if (!sourcesToLoad.length)
return [];

const { entries, errors } = await this._playwrightTest.listTests(this.config, this._mapSourcesToFiles(sourcesToLoad));
const { entries, errors } = await this._playwrightTest.listTests(this.config, sourcesToLoad);
this._updateProjects(entries, sourcesToLoad);
return errors;
}
Expand Down Expand Up @@ -261,42 +261,15 @@ export class TestModel {
}

async runTests(projects: TestProject[], locations: string[] | null, testListener: TestListener, parametrizedTestTitle: string | undefined, token: vscodeTypes.CancellationToken) {
locations = locations ? this._mapSourcesToFiles(locations) : [];
locations = locations || [];
await this._playwrightTest.runTests(this.config, projects.map(p => p.name), locations, testListener, parametrizedTestTitle, token);
}

async debugTests(projects: TestProject[], locations: string[] | null, testListener: TestListener, parametrizedTestTitle: string | undefined, token: vscodeTypes.CancellationToken) {
locations = locations ? this._mapSourcesToFiles(locations) : [];
locations = locations || [];
await this._playwrightTest.debugTests(this._vscode, this.config, projects.map(p => p.name), projects.map(p => p.testDir), this._envProvider(), locations, testListener, parametrizedTestTitle, token);
}

private _mapSourcesToFiles(sources: string[]): string[] {
const result: string[] = [];

// When we see
// src/foo.ts in the source,
// we want to pass
// out/bundle.js:0 src/foo.ts
// This way we'll parse bundle.js and filter source-mapped tests by src/foo.ts

// When we see
// src/foo.ts:14 in the source,
// we want to pass
// out/bundle.js:0 src/foo.ts:14
// Same idea here, we'll parse bundle and filter by source-mapped location.
// It looks wrong, but it actually achieves the right result.

for (const source of sources) {
const match = source.match(/^(.*)([:]\d+)$/);
const sourceFile = match ? match[1] : source;
const bundleFile = this._sourceToFile.get(sourceFile);
if (bundleFile)
result.push(bundleFile + ':0');
result.push(source);
}
return result;
}

private _mapFilesToSources(files: Set<string>): Set<string> {
const result = new Set<string>();
for (const file of files) {
Expand Down
14 changes: 7 additions & 7 deletions tests/source-map.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ test('should list tests on expand', async ({ activate }) => {

expect(vscode.renderExecLog(' ')).toBe(`
> playwright list-files -c playwright.config.js
> playwright test -c playwright.config.js --list build/test.spec.js:0 tests/test.spec.ts
> playwright test -c playwright.config.js --list tests/test.spec.ts
`);
});

Expand All @@ -74,7 +74,7 @@ test('should list tests for visible editors', async ({ activate }) => {

expect(vscode.renderExecLog(' ')).toBe(`
> playwright list-files -c playwright.config.js
> playwright test -c playwright.config.js --list build/test.spec.js:0 tests/test.spec.ts
> playwright test -c playwright.config.js --list tests/test.spec.ts
`);
});

Expand Down Expand Up @@ -169,7 +169,7 @@ test('should discover new tests', async ({ activate }) => {

expect(vscode.renderExecLog(' ')).toBe(`
> playwright list-files -c playwright.config.js
> playwright test -c playwright.config.js --list build/test.spec.js:0 tests/test.spec.ts
> playwright test -c playwright.config.js --list tests/test.spec.ts
`);

await Promise.all([
Expand All @@ -189,8 +189,8 @@ test('should discover new tests', async ({ activate }) => {

expect(vscode.renderExecLog(' ')).toBe(`
> playwright list-files -c playwright.config.js
> playwright test -c playwright.config.js --list build/test.spec.js:0 tests/test.spec.ts
> playwright test -c playwright.config.js --list build/test.spec.js:0 tests/test.spec.ts
> playwright test -c playwright.config.js --list tests/test.spec.ts
> playwright test -c playwright.config.js --list tests/test.spec.ts
`);
});

Expand Down Expand Up @@ -242,8 +242,8 @@ test('should run one test', async ({ activate }) => {

expect(vscode.renderExecLog(' ')).toBe(`
> playwright list-files -c playwright.config.js
> playwright test -c playwright.config.js --list build/test.spec.js:0 tests/test.spec.ts
> playwright test -c playwright.config.js build/test.spec.js:0 tests/test.spec.ts:3
> playwright test -c playwright.config.js --list tests/test.spec.ts
> playwright test -c playwright.config.js tests/test.spec.ts:3
`);
});

Expand Down

0 comments on commit a7dd533

Please sign in to comment.