|
4 | 4 | * SPDX-License-Identifier: Apache-2.0 |
5 | 5 | */ |
6 | 6 |
|
| 7 | +import assert from 'node:assert'; |
7 | 8 | import {describe, it} from 'node:test'; |
8 | 9 |
|
9 | 10 | import {ConsoleFormatter} from '../../src/formatters/ConsoleFormatter.js'; |
@@ -163,4 +164,77 @@ describe('ConsoleFormatter', () => { |
163 | 164 | t.assert.snapshot?.(result); |
164 | 165 | }); |
165 | 166 | }); |
| 167 | + describe('toJSON', () => { |
| 168 | + it('formats a console.log message', async () => { |
| 169 | + const message = createMockMessage({ |
| 170 | + type: () => 'log', |
| 171 | + text: () => 'Hello, world!', |
| 172 | + }); |
| 173 | + const result = (await ConsoleFormatter.from(message, {id: 1})).toJSON(); |
| 174 | + assert.deepStrictEqual(result, { |
| 175 | + type: 'log', |
| 176 | + text: 'Hello, world!', |
| 177 | + argsCount: 0, |
| 178 | + id: 1, |
| 179 | + }); |
| 180 | + }); |
| 181 | + |
| 182 | + it('formats a console.log message with args', async () => { |
| 183 | + const message = createMockMessage({ |
| 184 | + type: () => 'log', |
| 185 | + text: () => 'Processing file:', |
| 186 | + args: () => [ |
| 187 | + {jsonValue: async () => 'file.txt'}, |
| 188 | + {jsonValue: async () => 'another file'}, |
| 189 | + ], |
| 190 | + }); |
| 191 | + const result = (await ConsoleFormatter.from(message, {id: 1})).toJSON(); |
| 192 | + assert.deepStrictEqual(result, { |
| 193 | + type: 'log', |
| 194 | + text: 'Processing file:', |
| 195 | + argsCount: 2, |
| 196 | + id: 1, |
| 197 | + }); |
| 198 | + }); |
| 199 | + }); |
| 200 | + |
| 201 | + describe('toJSONDetailed', () => { |
| 202 | + it('formats a console.log message', async () => { |
| 203 | + const message = createMockMessage({ |
| 204 | + type: () => 'log', |
| 205 | + text: () => 'Hello, world!', |
| 206 | + }); |
| 207 | + const result = ( |
| 208 | + await ConsoleFormatter.from(message, {id: 1}) |
| 209 | + ).toJSONDetailed(); |
| 210 | + assert.deepStrictEqual(result, { |
| 211 | + id: 1, |
| 212 | + type: 'log', |
| 213 | + text: 'Hello, world!', |
| 214 | + args: [], |
| 215 | + stackTrace: undefined, |
| 216 | + }); |
| 217 | + }); |
| 218 | + |
| 219 | + it('formats a console.log message with args', async () => { |
| 220 | + const message = createMockMessage({ |
| 221 | + type: () => 'log', |
| 222 | + text: () => 'Processing file:', |
| 223 | + args: () => [ |
| 224 | + {jsonValue: async () => 'file.txt'}, |
| 225 | + {jsonValue: async () => 'another file'}, |
| 226 | + ], |
| 227 | + }); |
| 228 | + const result = ( |
| 229 | + await ConsoleFormatter.from(message, {id: 2, fetchDetailedData: true}) |
| 230 | + ).toJSONDetailed(); |
| 231 | + assert.deepStrictEqual(result, { |
| 232 | + id: 2, |
| 233 | + type: 'log', |
| 234 | + text: 'Processing file:', |
| 235 | + args: ['file.txt', 'another file'], |
| 236 | + stackTrace: undefined, |
| 237 | + }); |
| 238 | + }); |
| 239 | + }); |
166 | 240 | }); |
0 commit comments