From c5183c9d8299cca8c32e4421340a246852d06aa8 Mon Sep 17 00:00:00 2001 From: Daniel Kuschny Date: Sun, 19 Jan 2025 22:08:12 +0100 Subject: [PATCH] fix: Pending tests are not shown in Tests list (#231) Fixes #161 --- src/discoverer/evaluate.ts | 8 ++-- src/discoverer/syntax.ts | 2 +- src/runner.ts | 14 +----- .../unit/extract/evaluate-typescript.test.ts | 33 ++++++++++++++ src/test/unit/extract/evaluate.test.ts | 44 +++++++++++++++++++ src/test/unit/extract/syntax.test.ts | 44 +++++++++++++++++++ 6 files changed, 128 insertions(+), 17 deletions(-) diff --git a/src/discoverer/evaluate.ts b/src/discoverer/evaluate.ts index 871e394..95c894a 100644 --- a/src/discoverer/evaluate.ts +++ b/src/discoverer/evaluate.ts @@ -98,8 +98,8 @@ export class EvaluationTestDiscoverer implements ITestDiscoverer { sourceMap?: TraceMap | undefined, directive?: string, ) { - const fn = (name: string, callback: () => void) => { - if (typeof name !== 'string' || typeof callback !== 'function') { + const fn = (name: string, callback: (() => void) | undefined) => { + if (typeof name !== 'string') { return placeholder(); } @@ -114,7 +114,7 @@ export class EvaluationTestDiscoverer implements ITestDiscoverer { let startColumn = frame.columnNumber || 1; // approximate the length of the test case: - const functionLines = String(callback).split('\n'); + const functionLines = String(callback ?? '').split('\n'); let endLine = startLine + functionLines.length - 1; let endColumn = functionLines[functionLines.length - 1].length + 1; @@ -167,7 +167,7 @@ export class EvaluationTestDiscoverer implements ITestDiscoverer { if (kind === NodeKind.Suite) { stack.push(node); try { - return callback.call(placeholder()); + return typeof callback === 'function' ? callback.call(placeholder()) : placeholder(); } catch (e) { node.error = e instanceof Error ? e.message : String(e); } finally { diff --git a/src/discoverer/syntax.ts b/src/discoverer/syntax.ts index e545fb9..7a68a76 100644 --- a/src/discoverer/syntax.ts +++ b/src/discoverer/syntax.ts @@ -104,7 +104,7 @@ export class SyntaxTestDiscoverer implements ITestDiscoverer { traverse(ast, { enter(node) { - if (node.type !== C.CallExpression || node.arguments.length < 2) { + if (node.type !== C.CallExpression || node.arguments.length === 0) { return; } diff --git a/src/runner.ts b/src/runner.ts index 87051be..d187a0e 100644 --- a/src/runner.ts +++ b/src/runner.ts @@ -237,18 +237,8 @@ export class TestRunner { } if (!spawnCts.token.isCancellationRequested) { - if (ranAnyTest) { - for (const t of leafTests) { - run.skipped(t); - } - } else { - const md = new vscode.MarkdownString( - 'Test process exited unexpectedly, [view output](command:testing.showMostRecentOutput)', - ); - md.isTrusted = true; - for (const t of leafTests) { - run.errored(t, new vscode.TestMessage(md)); - } + for (const t of leafTests) { + run.skipped(t); } } diff --git a/src/test/unit/extract/evaluate-typescript.test.ts b/src/test/unit/extract/evaluate-typescript.test.ts index 78ce15c..cece24f 100644 --- a/src/test/unit/extract/evaluate-typescript.test.ts +++ b/src/test/unit/extract/evaluate-typescript.test.ts @@ -197,4 +197,37 @@ describe('evaluate typescript', () => { }, ]); }); + + it('works with pending test', async () => { + const src = await extractWithEvaluation( + 'function topLevel(a: number): string {', // + ' return a.toString() as string;', + '}', + '', + "suite('hello', () => {", // + " it('works');", + '})', + ); + expect(src).to.deep.equal([ + { + name: 'hello', + kind: NodeKind.Suite, + startLine: 4, + startColumn: 0, + endColumn: 1, + endLine: 6, + children: [ + { + name: 'works', + kind: NodeKind.Test, + startLine: 5, + startColumn: 2, + endColumn: Number.MAX_SAFE_INTEGER, + endLine: 5, + children: [], + }, + ], + }, + ]); + }); }); diff --git a/src/test/unit/extract/evaluate.test.ts b/src/test/unit/extract/evaluate.test.ts index 0074635..0c5ab0f 100644 --- a/src/test/unit/extract/evaluate.test.ts +++ b/src/test/unit/extract/evaluate.test.ts @@ -162,6 +162,50 @@ describe('evaluate', () => { ]); }); + it('works with pending test', async () => { + const src = await extractWithEvaluation( + "suite('hello', () => {", // + " it('works');", + '})', + ); + expect(src).to.deep.equal([ + { + name: 'hello', + startLine: 0, + kind: NodeKind.Suite, + startColumn: 0, + endColumn: 1, + endLine: 2, + children: [ + { + name: 'works', + kind: NodeKind.Test, + startLine: 1, + startColumn: 2, + endColumn: Number.MAX_SAFE_INTEGER, + endLine: 1, + children: [], + }, + ], + }, + ]); + }); + + it('works with pending suite', async () => { + const src = await extractWithEvaluation("suite('hello')"); + expect(src).to.deep.equal([ + { + name: 'hello', + startLine: 0, + kind: NodeKind.Suite, + startColumn: 0, + endColumn: Number.MAX_SAFE_INTEGER, + endLine: 0, + children: [], + }, + ]); + }); + it('stubs out requires and placeholds correctly', async () => { const src = await extractWithEvaluation( 'require("some invalid module").doing().other.things()', diff --git a/src/test/unit/extract/syntax.test.ts b/src/test/unit/extract/syntax.test.ts index 63a8d3b..9606ff8 100644 --- a/src/test/unit/extract/syntax.test.ts +++ b/src/test/unit/extract/syntax.test.ts @@ -94,6 +94,50 @@ describe('syntax', () => { ]); }); + it('works with pending test', async () => { + const src = await extractWithAst( + "suite('hello', () => {", // + " it('works');", + '})', + ); + expect(src).to.deep.equal([ + { + name: 'hello', + startLine: 0, + kind: NodeKind.Suite, + startColumn: 0, + endColumn: 2, + endLine: 2, + children: [ + { + name: 'works', + kind: NodeKind.Test, + startLine: 1, + startColumn: 2, + endColumn: 13, + endLine: 1, + children: [], + }, + ], + }, + ]); + }); + + it('works with pending suite', async () => { + const src = await extractWithAst("suite('hello')"); + expect(src).to.deep.equal([ + { + name: 'hello', + startLine: 0, + kind: NodeKind.Suite, + startColumn: 0, + endColumn: 14, + endLine: 0, + children: [], + }, + ]); + }); + it('can detect suite but not dynamic tests', async () => { const src = await extractWithAst( "suite('hello', () => {", //