-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathasync-context.module.spec.ts
44 lines (37 loc) · 1.75 KB
/
async-context.module.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { AsyncLocalStorage } from 'async_hooks'
import { DynamicModule, ValueProvider } from '@nestjs/common'
import { AsyncContextModule } from './async-context.module'
import { AsyncContext } from './async-context'
function getProviderValueFromModule <T> (acm: DynamicModule): T | undefined {
const provider = (acm.providers ?? [])[0] as ValueProvider<T> | undefined
if (provider === undefined) {
return undefined
}
return provider.useValue
}
describe('AsyncContextModule', () => {
describe('#forRoot', () => {
it("should use 'true' as default value for 'global' module property", () => {
const acm = AsyncContextModule.forRoot()
expect(acm.global).toStrictEqual(true)
})
it("should use 'isGlobal' option for 'global' module property", () => {
const acm = AsyncContextModule.forRoot({ isGlobal: false })
expect(acm.global).toStrictEqual(false)
})
it("should use new instance of 'AsyncLocalStorage' as default value for 'AsyncContext' class", () => {
const firstAC = getProviderValueFromModule<AsyncContext<any, any>>(AsyncContextModule.forRoot())
const secondAC = getProviderValueFromModule<AsyncContext<any, any>>(AsyncContextModule.forRoot())
expect(firstAC?.als).toBeDefined()
expect(secondAC?.als).toBeDefined()
expect(firstAC?.als === secondAC?.als).toBeFalsy()
})
it("should use instance of 'AsyncLocalStorage' as value for 'AsyncContext' class", () => {
const alsInstance = new AsyncLocalStorage<any>()
const acm = AsyncContextModule.forRoot({ alsInstance })
const asyncContext = getProviderValueFromModule<AsyncContext<any, any>>(acm)
expect(asyncContext?.als).toBeDefined()
expect(alsInstance === asyncContext?.als).toBeTruthy()
})
})
})