-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbase-updater.spec.ts
81 lines (60 loc) · 1.99 KB
/
base-updater.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { Injectable } from '@nestjs/common'
import { SchedulerRegistry } from '@nestjs/schedule'
import { Test, TestingModule } from '@nestjs/testing'
import { BaseUpdater } from './base-updater'
import { MetricsUpdaterService } from './metrics-updater.service'
@Injectable()
class MyUpdater extends BaseUpdater {
protected interval = 14
update = jest.fn()
constructor(protected readonly scheduler: SchedulerRegistry) {
super()
}
}
jest.useFakeTimers()
describe('BaseUpdater', () => {
let updater: MetricsUpdaterService
let debugSpy: jest.SpyInstance
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [SchedulerRegistry, MyUpdater],
}).compile()
updater = module.get(MyUpdater)
// @ts-ignore
debugSpy = jest.spyOn(updater.logger, 'debug').mockImplementation()
})
afterEach(() => {
updater.stop()
try {
// @ts-ignore
updater.scheduler.deleteInterval(MyUpdater.name)
// eslint-disable-next-line no-empty
} catch {}
})
test('start()', async () => {
const updateSpy = jest.spyOn(updater, 'update').mockImplementation()
expect(updateSpy).not.toHaveBeenCalled()
await updater.start()
expect(debugSpy).toHaveBeenCalledWith('Running MyUpdater every 14 ms ...')
// @ts-ignore
expect(updater.timeout).toBeDefined()
// @ts-ignore
expect(updater.scheduler.getInterval(MyUpdater.name)).toBeDefined()
// @ts-ignore
jest.advanceTimersByTime(100)
expect(updateSpy).toHaveBeenCalledTimes(8)
})
test('stop()', async () => {
const clearIntervalSpy = jest.spyOn(global, 'clearInterval')
await updater.start()
debugSpy.mockClear()
updater.stop()
expect(debugSpy).toHaveBeenCalledWith('Stopping updater MyUpdater ...')
expect(clearIntervalSpy).toHaveBeenCalled()
})
test('onApplicationShutDown()', () => {
const stopSpy = jest.spyOn(updater, 'stop')
updater.onApplicationShutdown()
expect(stopSpy).toHaveBeenCalled()
})
})