Skip to content

Commit 7b3f253

Browse files
committed
test: add test for console in node.js
1 parent faca199 commit 7b3f253

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { test } from 'vitest';
2+
3+
test('logging to stdout', () => {
4+
console.log('log with trace')
5+
console.info('info with trace')
6+
console.debug('debug with trace')
7+
console.dir({ hello: 'from dir with trace' })
8+
console.warn('warn with trace')
9+
console.assert(false, 'assert with trace')
10+
console.error('error with trace')
11+
console.trace('trace with trace')
12+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
printConsoleTrace: true,
6+
}
7+
})

test/cli/test/console.test.ts

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { expect, test } from 'vitest'
2+
import { DefaultReporter } from 'vitest/reporters'
3+
import { resolve } from 'pathe'
4+
import { runVitest } from '../../test-utils'
5+
6+
test('can run custom pools with Vitest', async () => {
7+
const reporter = new DefaultReporter()
8+
const vitest = await runVitest({
9+
root: './fixtures/console',
10+
reporters: [
11+
{
12+
onInit(ctx) {
13+
reporter.onInit(ctx as any)
14+
},
15+
onUserConsoleLog(ctx) {
16+
reporter.onUserConsoleLog(ctx)
17+
},
18+
},
19+
],
20+
})
21+
// removed the banner with version and timestamp
22+
expect(vitest.stdout.split('\n').slice(2).join('\n')).toMatchInlineSnapshot(`
23+
"
24+
stdout | trace.test.ts > logging to stdout
25+
log with trace
26+
❯ trace.test.ts:4:11
27+
28+
stdout | trace.test.ts > logging to stdout
29+
info with trace
30+
❯ trace.test.ts:5:11
31+
32+
stdout | trace.test.ts > logging to stdout
33+
debug with trace
34+
❯ trace.test.ts:6:11
35+
36+
stdout | trace.test.ts > logging to stdout
37+
{ hello: 'from dir with trace' }
38+
❯ trace.test.ts:7:11
39+
40+
"
41+
`)
42+
const stderrArray = vitest.stderr.split('\n')
43+
// remove stack trace
44+
const stderr = stderrArray.slice(0, -9).join('\n')
45+
const stackStderr = stderrArray.slice(-9).join('\n')
46+
expect(stderr).toMatchInlineSnapshot(`
47+
"stderr | trace.test.ts > logging to stdout
48+
warn with trace
49+
❯ trace.test.ts:8:11
50+
51+
stderr | trace.test.ts > logging to stdout
52+
Assertion failed: assert with trace
53+
❯ trace.test.ts:9:11
54+
55+
stderr | trace.test.ts > logging to stdout
56+
error with trace
57+
❯ trace.test.ts:10:11
58+
"
59+
`)
60+
// shows built-in stack because we don't intercept it, but doesn't show the new one
61+
expect(stackStderr).toMatch('Trace: trace with trace')
62+
expect(stackStderr).toMatch('trace.test.ts:11:11')
63+
expect(stackStderr).toMatch(' at ')
64+
expect(stackStderr).not.toMatch('❯ ')
65+
if (process.platform !== 'win32') {
66+
const root = resolve(process.cwd(), '../..')
67+
expect(stackStderr.replace(new RegExp(root, 'g'), '<root>')).toMatchInlineSnapshot(`
68+
"stderr | trace.test.ts > logging to stdout
69+
Trace: trace with trace
70+
at <root>/test/cli/fixtures/console/trace.test.ts:11:11
71+
at file://<root>/packages/runner/dist/index.js:135:14
72+
at file://<root>/packages/runner/dist/index.js:60:26
73+
at runTest (file://<root>/packages/runner/dist/index.js:798:17)
74+
at processTicksAndRejections (node:internal/process/task_queues:95:5)
75+
76+
"
77+
`)
78+
}
79+
})

0 commit comments

Comments
 (0)