Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: show console logs in "Test Output" #228

Merged
merged 4 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions samples/basic/test/console.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, it } from "vitest";

describe("console", () => {
it("basic", () => {
console.log([
"string",
{ hello: "world" },
1234,
/regex/g,
true,
false,
null,
]);
});

it("async", async () => {
console.log("1st");
await sleep(200);
console.log("2nd");
await sleep(200);
console.log("3rd");
});
});

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
8 changes: 1 addition & 7 deletions src/runHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,7 @@ async function runTest(
? WEAKMAP_TEST_DATA.get(items[0])!.getFullPattern()
: '',
{
info: (msg: string) => {
if (items.length === 1)
run.appendOutput(msg, undefined, items[0])
else
run.appendOutput(msg)
},
info: log.info,
error: log.error,
},
config.env || undefined,
Expand Down Expand Up @@ -339,4 +334,3 @@ async function runTest(
}
}
}

9 changes: 9 additions & 0 deletions src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,15 @@ export function syncTestStatusToVsCode(
for (const task of vitest) {
const data = matchTask(task, set)
if (task.type === 'test' || task.type === 'custom') {
// for now, display logs after all tests are finished.
// TODO: append logs during test execution using `onUserConsoleLog` rpc.
if (finished) {
for (const log of task.logs ?? []) {
// LF to CRLF https://code.visualstudio.com/api/extension-guides/testing#test-output
const output = log.content.replace(/(?<!\r)\n/g, "\r\n");
run.appendOutput(output, undefined, data.item);
}
}
if (task.result == null) {
if (finished) {
finishedTest && finishedTest.add(data.item)
Expand Down