From 3847bd4f49e14d011a4e7a6679c69cc4e2b03441 Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Mon, 21 Mar 2022 11:39:51 +0800 Subject: [PATCH] fix: cannot get test result correctly --- samples/basic/test/add.test.ts | 2 +- src/discover.ts | 5 ++--- src/pure/runner.ts | 4 ++-- src/run_test.ts | 17 ++++++++--------- src/test_data.ts | 1 - 5 files changed, 13 insertions(+), 16 deletions(-) diff --git a/samples/basic/test/add.test.ts b/samples/basic/test/add.test.ts index c3a2d6f7..9af83c12 100644 --- a/samples/basic/test/add.test.ts +++ b/samples/basic/test/add.test.ts @@ -6,7 +6,7 @@ describe("addition", () => { }); it("failed", () => { - expect(1 + 1).toBe(2); + expect(1 + 2).toBe(2); }); it.skip("skipped", () => { diff --git a/src/discover.ts b/src/discover.ts index 7578008d..3b5f94e9 100644 --- a/src/discover.ts +++ b/src/discover.ts @@ -55,7 +55,6 @@ export function discoverTestFromFileContent( item: vscode.TestItem, data: TestFile ) { - data.testCases.length = 0; if (testItemIdMap.get(controller) == null) { testItemIdMap.set(controller, new Map()); } @@ -99,9 +98,10 @@ export function discoverTestFromFileContent( const arr: NamedBlock[] = [...result.describeBlocks, ...result.itBlocks]; arr.sort((a, b) => (a.start?.line || 0) - (b.start?.line || 0)); let testCaseIndex = 0; + let index = 0; for (const block of arr) { const parent = getParent(block); - const id = `${item.uri}/${block.name}`; + const id = `${item.uri}/${block.name}@${index++}`; const caseItem = controller.createTestItem(id, block.name!, item.uri); idMap.set(id, caseItem); caseItem.range = new vscode.Range( @@ -121,7 +121,6 @@ export function discoverTestFromFileContent( testCaseIndex++ ); WEAKMAP_TEST_DATA.set(caseItem, testCase); - data.testCases.push(caseItem); } else { throw new Error(); } diff --git a/src/pure/runner.ts b/src/pure/runner.ts index be1f5462..f34cf53a 100644 --- a/src/pure/runner.ts +++ b/src/pure/runner.ts @@ -83,7 +83,6 @@ export class TestRunner { } try { - console.log(args); if (this.vitePath) { await execa(this.vitePath, args, { windowsHide: false, @@ -99,7 +98,8 @@ export class TestRunner { } const file = await readFile(path, "utf-8"); - return JSON.parse(file) as AggregatedResult; + const out = JSON.parse(file) as AggregatedResult; + return out; } finally { release(); } diff --git a/src/run_test.ts b/src/run_test.ts index 54fce7f4..3a1cdb17 100644 --- a/src/run_test.ts +++ b/src/run_test.ts @@ -17,33 +17,32 @@ export async function runTest( throw new Error("file item not found"); } - let file: TestFile; + let file: vscode.TestItem; if (testingData instanceof TestFile) { - file = testingData; + file = item; } else { - file = WEAKMAP_TEST_DATA.get(testingData.fileItem) as TestFile; + file = testingData.fileItem; if (!file) { throw new Error("file item not found"); } } + const fileTestCases = getAllTestCases(file); const testCaseSet = new Set(getAllTestCases(item)); - const idMap = new Map(); testCaseSet.forEach((testCase) => { run.started(testCase); - idMap.set(testCase.id, testCase); }); const data = WEAKMAP_TEST_DATA.get(item)!; const out = await runner.scheduleRun(item.uri!.fsPath, data.getFullPattern()); out.testResults.forEach((result, index) => { - let child: undefined | vscode.TestItem = file.testCases[index]; + let child: undefined | vscode.TestItem = fileTestCases[index]; const id = getTestCaseId(item, result.displayName!) || ""; - if (!child || child.id !== id) { - child = idMap.get(id); + if (!child || !child.id.startsWith(id)) { console.error("not match"); console.dir(out.testResults); - console.dir(file.testCases); + console.dir(fileTestCases); + throw new Error(); } if (!child || !testCaseSet.has(child)) { diff --git a/src/test_data.ts b/src/test_data.ts index 1ee1e15a..1b38cc42 100644 --- a/src/test_data.ts +++ b/src/test_data.ts @@ -64,7 +64,6 @@ export class TestCase { export class TestFile { resolved = false; pattern = ""; - testCases: vscode.TestItem[] = []; public async updateFromDisk( controller: vscode.TestController, item: vscode.TestItem