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 1 commit
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
8 changes: 3 additions & 5 deletions test/unit/context-manager/async-local-context-manager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@

'use strict'

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

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

test('Async Local Context Manager', (t) => {
t.autoend()

runContextManagerTests(t, createContextManager)
bizob2828 marked this conversation as resolved.
Show resolved Hide resolved
test('Async Local Context Manager', async (t) => {
await runContextManagerTests(t, createContextManager)
})

function createContextManager() {
Expand Down
68 changes: 29 additions & 39 deletions test/unit/context-manager/context-manager-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,45 @@

'use strict'

const assert = require('node:assert')

/**
* Add a standard set of Legacy Context Manager test cases for testing
* either the standard or diagnostic versions.
*/
function runContextMangerTests(t, createContextManager) {
t.test('Should default to null context', (t) => {
async function runContextMangerTests(t, createContextManager) {
await t.test('Should default to null context', () => {
const contextManager = createContextManager()

const context = contextManager.getContext()

t.equal(context, null)

t.end()
assert.equal(context, null)
})

t.test('setContext should update the current context', (t) => {
await t.test('setContext should update the current context', () => {
const contextManager = createContextManager()

const expectedContext = { name: 'new context' }

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

t.equal(context, expectedContext)

t.end()
assert.equal(context, expectedContext)
})

t.test('runInContext()', (t) => {
t.autoend()

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

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

t.equal(callbackCalled, true)

t.end()
assert.equal(callbackCalled, true)
})

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

const previousContext = { name: 'previous' }
Expand All @@ -60,12 +54,12 @@ function runContextMangerTests(t, createContextManager) {
contextManager.runInContext(newContext, () => {
const context = contextManager.getContext()

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

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

const previousContext = { name: 'previous' }
Expand All @@ -76,12 +70,10 @@ function runContextMangerTests(t, createContextManager) {

const context = contextManager.getContext()

t.equal(context, previousContext)

t.end()
assert.equal(context, previousContext)
})

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

const previousContext = { name: 'previous' }
Expand All @@ -94,18 +86,16 @@ function runContextMangerTests(t, createContextManager) {
throw new Error('Something went bad')
})
} catch (error) {
t.ok(error)
assert.ok(error)
// swallowing error
}

const context = contextManager.getContext()

t.equal(context, previousContext)

t.end()
assert.equal(context, previousContext)
})

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

const previousContext = { name: 'previous' }
Expand All @@ -117,12 +107,12 @@ function runContextMangerTests(t, createContextManager) {
contextManager.runInContext(newContext, functionRunInContext, expectedThis)

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

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

const previousContext = { name: 'previous' }
Expand All @@ -136,13 +126,13 @@ function runContextMangerTests(t, createContextManager) {
contextManager.runInContext(newContext, functionRunInContext, null, args)

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

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

const previousContext = { name: 'previous' }
Expand All @@ -158,9 +148,9 @@ function runContextMangerTests(t, createContextManager) {
contextManager.runInContext(
newContext,
function functionRunInContext(arg1, arg2) {
t.equal(arg1, expectedArg1)
t.equal(arg2, expectedArg2)
t.end()
assert.equal(arg1, expectedArg1)
assert.equal(arg2, expectedArg2)
end()
},
null,
arguments
Expand Down
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