|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// This test verifies that the --test-inspect-depth CLI option controls |
| 4 | +// the depth of util.inspect() used when formatting errors in test reporters. |
| 5 | + |
| 6 | +const { spawnSync } = require('node:child_process'); |
| 7 | +const { assertIncludes } = require('../common'); |
| 8 | +const { writeFileSync, mkdtempSync } = require('node:fs'); |
| 9 | +const { tmpdir } = require('node:os'); |
| 10 | +const { join } = require('node:path'); |
| 11 | + |
| 12 | +const tmp = mkdtempSync(join(tmpdir(), 'node-test-inspect-depth-')); |
| 13 | + |
| 14 | +// Create a test file that throws an error with a deeply nested object. |
| 15 | +const testFile = join(tmp, 'test.js'); |
| 16 | +writeFileSync(testFile, ` |
| 17 | +const test = require('node:test'); |
| 18 | +
|
| 19 | +test('nested error', () => { |
| 20 | + const err = new Error('fail'); |
| 21 | + err.data = { level1: { level2: { level3: { level4: { value: 42 } } } } }; |
| 22 | + throw err; |
| 23 | +}); |
| 24 | +`); |
| 25 | + |
| 26 | +// Run without --test-inspect-depth (default depth = 2, level3+ should be [Object]) |
| 27 | +{ |
| 28 | + const result = spawnSync(process.execPath, ['--test', testFile], { |
| 29 | + encoding: 'utf8', |
| 30 | + }); |
| 31 | + assertIncludes(result.stderr + result.stdout, '[Object]'); |
| 32 | +} |
| 33 | + |
| 34 | +// Run with --test-inspect-depth=10 (all levels should be expanded) |
| 35 | +{ |
| 36 | + const result = spawnSync(process.execPath, ['--test', '--test-inspect-depth=10', testFile], { |
| 37 | + encoding: 'utf8', |
| 38 | + }); |
| 39 | + assertIncludes(result.stderr + result.stdout, 'level4'); |
| 40 | + assertIncludes(result.stderr + result.stdout, 'value: 42'); |
| 41 | +} |
| 42 | + |
| 43 | +// Run with --test-inspect-depth=0 (no expansion, all objects [Object]) |
| 44 | +{ |
| 45 | + const result = spawnSync(process.execPath, ['--test', '--test-inspect-depth=0', testFile], { |
| 46 | + encoding: 'utf8', |
| 47 | + }); |
| 48 | + assertIncludes(result.stderr + result.stdout, '[Object]'); |
| 49 | +} |
0 commit comments