Skip to content

Commit

Permalink
chore: Converted context-manager unit tests to node:test (#2508)
Browse files Browse the repository at this point in the history
Co-authored-by: Bob Evans <[email protected]>
  • Loading branch information
jsumners-nr and bizob2828 authored Aug 22, 2024
1 parent 57e6be9 commit 9363eb0
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 186 deletions.
154 changes: 145 additions & 9 deletions test/unit/context-manager/async-local-context-manager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,153 @@

'use strict'

const { test } = require('tap')

const runContextManagerTests = require('./context-manager-tests')
const test = require('node:test')
const assert = require('node:assert')
const AsyncLocalContextManager = require('../../../lib/context-manager/async-local-context-manager')

test('Async Local Context Manager', (t) => {
t.autoend()
test('Should default to null context', () => {
const contextManager = new AsyncLocalContextManager({})

const context = contextManager.getContext()

runContextManagerTests(t, createContextManager)
assert.equal(context, null)
})

function createContextManager() {
return new AsyncLocalContextManager({})
}
test('setContext should update the current context', () => {
const contextManager = new AsyncLocalContextManager({})

const expectedContext = { name: 'new context' }

contextManager.setContext(expectedContext)
const context = contextManager.getContext()

assert.equal(context, expectedContext)
})

test('runInContext()', async (t) => {
await t.test('should execute callback synchronously', () => {
const contextManager = new AsyncLocalContextManager({})

let callbackCalled = false
contextManager.runInContext({}, () => {
callbackCalled = true
})

assert.equal(callbackCalled, true)
})

await t.test('should set context to active for life of callback', (t, end) => {
const contextManager = new AsyncLocalContextManager({})

const previousContext = { name: 'previous' }
contextManager.setContext(previousContext)

const newContext = { name: 'new' }

contextManager.runInContext(newContext, () => {
const context = contextManager.getContext()

assert.equal(context, newContext)
end()
})
})

await t.test('should restore previous context when callback completes', () => {
const contextManager = new AsyncLocalContextManager({})

const previousContext = { name: 'previous' }
contextManager.setContext(previousContext)

const newContext = { name: 'new' }
contextManager.runInContext(newContext, () => {})

const context = contextManager.getContext()

assert.equal(context, previousContext)
})

await t.test('should restore previous context on exception', () => {
const contextManager = new AsyncLocalContextManager({})

const previousContext = { name: 'previous' }
contextManager.setContext(previousContext)

const newContext = { name: 'new' }

try {
contextManager.runInContext(newContext, () => {
throw new Error('Something went bad')
})
} catch (error) {
assert.ok(error)
// swallowing error
}

const context = contextManager.getContext()

assert.equal(context, previousContext)
})

await t.test('should apply `cbThis` arg to execution', (t, end) => {
const contextManager = new AsyncLocalContextManager({})

const previousContext = { name: 'previous' }
contextManager.setContext(previousContext)

const newContext = { name: 'new' }
const expectedThis = () => {}

contextManager.runInContext(newContext, functionRunInContext, expectedThis)

function functionRunInContext() {
assert.equal(this, expectedThis)
end()
}
})

await t.test('should apply args array to execution', (t, end) => {
const contextManager = new AsyncLocalContextManager({})

const previousContext = { name: 'previous' }
contextManager.setContext(previousContext)

const newContext = { name: 'new' }
const expectedArg1 = 'first arg'
const expectedArg2 = 'second arg'
const args = [expectedArg1, expectedArg2]

contextManager.runInContext(newContext, functionRunInContext, null, args)

function functionRunInContext(arg1, arg2) {
assert.equal(arg1, expectedArg1)
assert.equal(arg2, expectedArg2)
end()
}
})

await t.test('should apply arguments construct to execution', (t, end) => {
const contextManager = new AsyncLocalContextManager({})

const previousContext = { name: 'previous' }
contextManager.setContext(previousContext)

const newContext = { name: 'new' }
const expectedArg1 = 'first arg'
const expectedArg2 = 'second arg'

executingFunction(expectedArg1, expectedArg2)

function executingFunction() {
contextManager.runInContext(
newContext,
function functionRunInContext(arg1, arg2) {
assert.equal(arg1, expectedArg1)
assert.equal(arg2, expectedArg2)
end()
},
null,
arguments
)
}
})
})
173 changes: 0 additions & 173 deletions test/unit/context-manager/context-manager-tests.js

This file was deleted.

8 changes: 4 additions & 4 deletions test/unit/context-manager/create-context-manager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@

'use strict'

const { test } = require('tap')
const test = require('node:test')
const assert = require('node:assert')

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

test('Should return AsyncLocalContextManager by default', (t) => {
test('Should return AsyncLocalContextManager by default', () => {
const contextManager = createImplementation({
logging: {},
feature_flag: {}
})

t.ok(contextManager instanceof AsyncLocalContextManager)
t.end()
assert.equal(contextManager instanceof AsyncLocalContextManager, true)
})

0 comments on commit 9363eb0

Please sign in to comment.