Skip to content

Commit 9363eb0

Browse files
chore: Converted context-manager unit tests to node:test (#2508)
Co-authored-by: Bob Evans <[email protected]>
1 parent 57e6be9 commit 9363eb0

File tree

3 files changed

+149
-186
lines changed

3 files changed

+149
-186
lines changed

test/unit/context-manager/async-local-context-manager.test.js

Lines changed: 145 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,153 @@
55

66
'use strict'
77

8-
const { test } = require('tap')
9-
10-
const runContextManagerTests = require('./context-manager-tests')
8+
const test = require('node:test')
9+
const assert = require('node:assert')
1110
const AsyncLocalContextManager = require('../../../lib/context-manager/async-local-context-manager')
1211

13-
test('Async Local Context Manager', (t) => {
14-
t.autoend()
12+
test('Should default to null context', () => {
13+
const contextManager = new AsyncLocalContextManager({})
14+
15+
const context = contextManager.getContext()
1516

16-
runContextManagerTests(t, createContextManager)
17+
assert.equal(context, null)
1718
})
1819

19-
function createContextManager() {
20-
return new AsyncLocalContextManager({})
21-
}
20+
test('setContext should update the current context', () => {
21+
const contextManager = new AsyncLocalContextManager({})
22+
23+
const expectedContext = { name: 'new context' }
24+
25+
contextManager.setContext(expectedContext)
26+
const context = contextManager.getContext()
27+
28+
assert.equal(context, expectedContext)
29+
})
30+
31+
test('runInContext()', async (t) => {
32+
await t.test('should execute callback synchronously', () => {
33+
const contextManager = new AsyncLocalContextManager({})
34+
35+
let callbackCalled = false
36+
contextManager.runInContext({}, () => {
37+
callbackCalled = true
38+
})
39+
40+
assert.equal(callbackCalled, true)
41+
})
42+
43+
await t.test('should set context to active for life of callback', (t, end) => {
44+
const contextManager = new AsyncLocalContextManager({})
45+
46+
const previousContext = { name: 'previous' }
47+
contextManager.setContext(previousContext)
48+
49+
const newContext = { name: 'new' }
50+
51+
contextManager.runInContext(newContext, () => {
52+
const context = contextManager.getContext()
53+
54+
assert.equal(context, newContext)
55+
end()
56+
})
57+
})
58+
59+
await t.test('should restore previous context when callback completes', () => {
60+
const contextManager = new AsyncLocalContextManager({})
61+
62+
const previousContext = { name: 'previous' }
63+
contextManager.setContext(previousContext)
64+
65+
const newContext = { name: 'new' }
66+
contextManager.runInContext(newContext, () => {})
67+
68+
const context = contextManager.getContext()
69+
70+
assert.equal(context, previousContext)
71+
})
72+
73+
await t.test('should restore previous context on exception', () => {
74+
const contextManager = new AsyncLocalContextManager({})
75+
76+
const previousContext = { name: 'previous' }
77+
contextManager.setContext(previousContext)
78+
79+
const newContext = { name: 'new' }
80+
81+
try {
82+
contextManager.runInContext(newContext, () => {
83+
throw new Error('Something went bad')
84+
})
85+
} catch (error) {
86+
assert.ok(error)
87+
// swallowing error
88+
}
89+
90+
const context = contextManager.getContext()
91+
92+
assert.equal(context, previousContext)
93+
})
94+
95+
await t.test('should apply `cbThis` arg to execution', (t, end) => {
96+
const contextManager = new AsyncLocalContextManager({})
97+
98+
const previousContext = { name: 'previous' }
99+
contextManager.setContext(previousContext)
100+
101+
const newContext = { name: 'new' }
102+
const expectedThis = () => {}
103+
104+
contextManager.runInContext(newContext, functionRunInContext, expectedThis)
105+
106+
function functionRunInContext() {
107+
assert.equal(this, expectedThis)
108+
end()
109+
}
110+
})
111+
112+
await t.test('should apply args array to execution', (t, end) => {
113+
const contextManager = new AsyncLocalContextManager({})
114+
115+
const previousContext = { name: 'previous' }
116+
contextManager.setContext(previousContext)
117+
118+
const newContext = { name: 'new' }
119+
const expectedArg1 = 'first arg'
120+
const expectedArg2 = 'second arg'
121+
const args = [expectedArg1, expectedArg2]
122+
123+
contextManager.runInContext(newContext, functionRunInContext, null, args)
124+
125+
function functionRunInContext(arg1, arg2) {
126+
assert.equal(arg1, expectedArg1)
127+
assert.equal(arg2, expectedArg2)
128+
end()
129+
}
130+
})
131+
132+
await t.test('should apply arguments construct to execution', (t, end) => {
133+
const contextManager = new AsyncLocalContextManager({})
134+
135+
const previousContext = { name: 'previous' }
136+
contextManager.setContext(previousContext)
137+
138+
const newContext = { name: 'new' }
139+
const expectedArg1 = 'first arg'
140+
const expectedArg2 = 'second arg'
141+
142+
executingFunction(expectedArg1, expectedArg2)
143+
144+
function executingFunction() {
145+
contextManager.runInContext(
146+
newContext,
147+
function functionRunInContext(arg1, arg2) {
148+
assert.equal(arg1, expectedArg1)
149+
assert.equal(arg2, expectedArg2)
150+
end()
151+
},
152+
null,
153+
arguments
154+
)
155+
}
156+
})
157+
})

test/unit/context-manager/context-manager-tests.js

Lines changed: 0 additions & 173 deletions
This file was deleted.

test/unit/context-manager/create-context-manager.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55

66
'use strict'
77

8-
const { test } = require('tap')
8+
const test = require('node:test')
9+
const assert = require('node:assert')
910

1011
const createImplementation = require('../../../lib/context-manager/create-context-manager')
1112
const AsyncLocalContextManager = require('../../../lib/context-manager/async-local-context-manager')
1213

13-
test('Should return AsyncLocalContextManager by default', (t) => {
14+
test('Should return AsyncLocalContextManager by default', () => {
1415
const contextManager = createImplementation({
1516
logging: {},
1617
feature_flag: {}
1718
})
1819

19-
t.ok(contextManager instanceof AsyncLocalContextManager)
20-
t.end()
20+
assert.equal(contextManager instanceof AsyncLocalContextManager, true)
2121
})

0 commit comments

Comments
 (0)