Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Converted context-manager unit tests to node:test #2508

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
bizob2828 marked this conversation as resolved.
Show resolved Hide resolved
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)
})
Loading