|
| 1 | +'use strict'; |
| 2 | +require('../common'); |
| 3 | +const assert = require('node:assert'); |
| 4 | +const dc = require('node:diagnostics_channel'); |
| 5 | +const { test } = require('node:test'); |
| 6 | +const { spawnSync } = require('child_process'); |
| 7 | +const { join } = require('path'); |
| 8 | + |
| 9 | +const events = []; |
| 10 | + |
| 11 | +dc.subscribe('tracing:node.test:start', (data) => events.push({ event: 'start', name: data.name })); |
| 12 | +dc.subscribe('tracing:node.test:end', (data) => events.push({ event: 'end', name: data.name })); |
| 13 | +dc.subscribe('tracing:node.test:error', (data) => events.push({ event: 'error', name: data.name })); |
| 14 | + |
| 15 | +test('passing test fires start and end', async () => {}); |
| 16 | + |
| 17 | +// Validate events were emitted (check after all tests via process.on('exit')) |
| 18 | +process.on('exit', () => { |
| 19 | + // Check passing test |
| 20 | + const testName1 = 'passing test fires start and end'; |
| 21 | + const startEvents = events.filter((e) => e.event === 'start' && e.name === testName1); |
| 22 | + const endEvents = events.filter((e) => e.event === 'end' && e.name === testName1); |
| 23 | + assert.strictEqual(startEvents.length, 1); |
| 24 | + assert.strictEqual(endEvents.length, 1); |
| 25 | + |
| 26 | + // Check nested tests fire events |
| 27 | + const nested1Start = events.filter((e) => e.event === 'start' && e.name === 'nested test 1'); |
| 28 | + const nested1End = events.filter((e) => e.event === 'end' && e.name === 'nested test 1'); |
| 29 | + const nested2Start = events.filter((e) => e.event === 'start' && e.name === 'nested test 2'); |
| 30 | + const nested2End = events.filter((e) => e.event === 'end' && e.name === 'nested test 2'); |
| 31 | + assert.strictEqual(nested1Start.length, 1); |
| 32 | + assert.strictEqual(nested1End.length, 1); |
| 33 | + assert.strictEqual(nested2Start.length, 1); |
| 34 | + assert.strictEqual(nested2End.length, 1); |
| 35 | + |
| 36 | + // Check describe block tests fire events |
| 37 | + const describeStart = events.filter((e) => e.event === 'start' && e.name === 'test inside describe'); |
| 38 | + const describeEnd = events.filter((e) => e.event === 'end' && e.name === 'test inside describe'); |
| 39 | + const describeStart2 = events.filter( |
| 40 | + (e) => e.event === 'start' && e.name === 'another test inside describe', |
| 41 | + ); |
| 42 | + const describeEnd2 = events.filter( |
| 43 | + (e) => e.event === 'end' && e.name === 'another test inside describe', |
| 44 | + ); |
| 45 | + assert.strictEqual(describeStart.length, 1); |
| 46 | + assert.strictEqual(describeEnd.length, 1); |
| 47 | + assert.strictEqual(describeStart2.length, 1); |
| 48 | + assert.strictEqual(describeEnd2.length, 1); |
| 49 | + |
| 50 | + // Check async operations test fires events |
| 51 | + const asyncTestName = 'context is available in async operations within test'; |
| 52 | + const asyncStart = events.filter((e) => e.event === 'start' && e.name === asyncTestName); |
| 53 | + const asyncEnd = events.filter((e) => e.event === 'end' && e.name === asyncTestName); |
| 54 | + assert.strictEqual(asyncStart.length, 1); |
| 55 | + assert.strictEqual(asyncEnd.length, 1); |
| 56 | +}); |
| 57 | + |
| 58 | +// Test bindStore context propagation |
| 59 | +const { AsyncLocalStorage } = require('node:async_hooks'); |
| 60 | +const testStorage = new AsyncLocalStorage(); |
| 61 | + |
| 62 | +// bindStore on the start channel: whenever a test fn runs, set testStorage to the test name |
| 63 | +dc.channel('tracing:node.test:start').bindStore(testStorage, (data) => data.name); |
| 64 | + |
| 65 | +test('bindStore propagates into test body via start channel', async () => { |
| 66 | + const expectedName = 'bindStore propagates into test body via start channel'; |
| 67 | + const storedValueDuringTest = testStorage.getStore(); |
| 68 | + assert.strictEqual(storedValueDuringTest, expectedName); |
| 69 | + |
| 70 | + // Propagates into async operations inside the test |
| 71 | + const valueInSetImmediate = await new Promise((resolve) => { |
| 72 | + setImmediate(() => resolve(testStorage.getStore())); |
| 73 | + }); |
| 74 | + assert.strictEqual(valueInSetImmediate, expectedName); |
| 75 | +}); |
| 76 | + |
| 77 | +test('bindStore value is isolated between tests', async () => { |
| 78 | + assert.strictEqual(testStorage.getStore(), 'bindStore value is isolated between tests'); |
| 79 | +}); |
| 80 | + |
| 81 | +test('nested tests fire events with correct names', async (t) => { |
| 82 | + await t.test('nested test 1', async () => { |
| 83 | + const stored = testStorage.getStore(); |
| 84 | + assert.strictEqual(stored, 'nested test 1'); |
| 85 | + }); |
| 86 | + |
| 87 | + await t.test('nested test 2', async () => { |
| 88 | + const stored = testStorage.getStore(); |
| 89 | + assert.strictEqual(stored, 'nested test 2'); |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | +const { describe, it } = require('node:test'); |
| 94 | + |
| 95 | +describe('describe block with tests', () => { |
| 96 | + it('test inside describe', async () => { |
| 97 | + const stored = testStorage.getStore(); |
| 98 | + assert.strictEqual(stored, 'test inside describe'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('another test inside describe', async () => { |
| 102 | + const stored = testStorage.getStore(); |
| 103 | + assert.strictEqual(stored, 'another test inside describe'); |
| 104 | + }); |
| 105 | +}); |
| 106 | + |
| 107 | +test('context is available in async operations within test', async () => { |
| 108 | + const testName = 'context is available in async operations within test'; |
| 109 | + assert.strictEqual(testStorage.getStore(), testName); |
| 110 | + |
| 111 | + // Verify context is available in setImmediate |
| 112 | + const valueInImmediate = await new Promise((resolve) => { |
| 113 | + setImmediate(() => resolve(testStorage.getStore())); |
| 114 | + }); |
| 115 | + assert.strictEqual(valueInImmediate, testName); |
| 116 | + |
| 117 | + // Verify context is available in setTimeout |
| 118 | + const valueInTimeout = await new Promise((resolve) => { |
| 119 | + setTimeout(() => resolve(testStorage.getStore()), 0); |
| 120 | + }); |
| 121 | + assert.strictEqual(valueInTimeout, testName); |
| 122 | +}); |
| 123 | + |
| 124 | +test('error events fire for failing tests in fixture', async () => { |
| 125 | + // Run the fixture test that intentionally fails |
| 126 | + const fixturePath = join(__dirname, '../fixtures/test-runner/diagnostics-channel-error-test.js'); |
| 127 | + const result = spawnSync(process.execPath, [fixturePath], { encoding: 'utf8' }); |
| 128 | + |
| 129 | + // The fixture test intentionally fails, so exit code should be non-zero |
| 130 | + assert.notStrictEqual(result.status, 0); |
| 131 | + |
| 132 | + // Extract and verify error events from fixture output |
| 133 | + // The fixture outputs JSON with errorEvents array on exit |
| 134 | + const lines = result.stdout.split('\n'); |
| 135 | + const eventLine = lines.find((line) => line.includes('errorEvents')); |
| 136 | + assert.ok(eventLine, 'Expected errorEvents line in fixture output'); |
| 137 | + const { errorEvents } = JSON.parse(eventLine); |
| 138 | + assert.strictEqual(errorEvents.includes('test that intentionally fails'), true); |
| 139 | +}); |
0 commit comments