tests/mocks.js   A
last analyzed

Complexity

Total Complexity 5
Complexity/F 1.25

Size

Lines of Code 42
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 25
mnd 1
bc 1
fnc 4
dl 0
loc 42
rs 10
bpm 0.25
cpm 1.25
noi 0
c 0
b 0
f 0
1
'use strict'
2
3
// eslint-disable-next-line @typescript-eslint/no-var-requires
4
const bluebirdModule = require('bluebird')
5
6
const globalPromise = global.Promise
7
const globalSetTimeout = global.setTimeout
8
9
exports.__esModule = true
10
11
exports.mockSetTimeout = expectedPause => {
12
  global.Promise = bluebirdModule.Promise
13
14
  const mockState = {
15
    expectedPause: expectedPause,
16
    passedTimeout: null,
17
    numTimeoutCalls: 0,
18
    // eslint-disable-next-line @typescript-eslint/no-empty-function
19
    pendingCallback: () => {},
20
    pendingCallbackArgs: [],
21
  }
22
23
  global.setTimeout = (callback, ms, ...args) => {
24
    if (ms !== mockState.expectedPause) {
25
      // We use a "magic number" to allow all the other cases work as expected
26
      return globalSetTimeout(callback, ms, ...args)
27
    }
28
29
    mockState.passedTimeout = ms
30
    mockState.numTimeoutCalls += 1
31
    mockState.pendingCallback = callback
32
    mockState.pendingCallbackArgs = args
33
    return null
34
  }
35
36
  return mockState
37
}
38
39
exports.restoreSetTimeout = () => {
40
  global.setTimeout = globalSetTimeout
41
  global.Promise = globalPromise
42
}
43