Welcome to mirror list, hosted at ThFree Co, Russian Federation.

timers.js « utils « lib « test - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6127f346b179e0cdb8572f4f51f97aa9984594c8 (plain)
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
82
const t = require('tap')
const { resolve } = require('path')
const fs = require('graceful-fs')
const mockLogs = require('../../fixtures/mock-logs')

const mockTimers = (t, options) => {
  const { logs, logMocks } = mockLogs()
  const Timers = t.mock('../../../lib/utils/timers', {
    ...logMocks,
  })
  const timers = new Timers(options)
  t.teardown(() => timers.off())
  return { timers, logs }
}

t.test('getters', async (t) => {
  const { timers } = mockTimers(t)
  t.match(timers.unfinished, new Map())
  t.match(timers.finished, {})
})

t.test('listens/stops on process', async (t) => {
  const { timers } = mockTimers(t)
  process.emit('time', 'foo')
  process.emit('time', 'bar')
  process.emit('timeEnd', 'bar')
  t.match(timers.unfinished, new Map([['foo', Number]]))
  t.match(timers.finished, { bar: Number })
  timers.off()
  process.emit('time', 'baz')
  t.notOk(timers.unfinished.get('baz'))
})

t.test('initial timer', async (t) => {
  const { timers } = mockTimers(t, { start: 'foo' })
  process.emit('timeEnd', 'foo')
  t.match(timers.finished, { foo: Number })
})

t.test('initial listener', async (t) => {
  const events = []
  const listener = (...args) => events.push(args)
  const { timers } = mockTimers(t, { listener })
  process.emit('time', 'foo')
  process.emit('time', 'bar')
  process.emit('timeEnd', 'bar')
  timers.off(listener)
  process.emit('timeEnd', 'foo')
  t.equal(events.length, 1)
  t.match(events, [['bar', Number]])
})

t.test('finish unstarted timer', async (t) => {
  const { logs } = mockTimers(t)
  process.emit('timeEnd', 'foo')
  t.match(logs.silly, [['timing', /^Tried to end timer/, 'foo']])
})

t.test('writes file', async (t) => {
  const { timers } = mockTimers(t)
  const dir = t.testdir()
  process.emit('time', 'foo')
  process.emit('timeEnd', 'foo')
  timers.load({ dir })
  timers.writeFile({ some: 'data' })
  const data = JSON.parse(fs.readFileSync(resolve(dir, '_timing.json')))
  t.match(data, {
    some: 'data',
    foo: Number,
    unfinished: {
      npm: [Number, Number],
    },
  })
})

t.test('fails to write file', async (t) => {
  const { logs, timers } = mockTimers(t)
  timers.writeFile()
  t.match(logs.warn, [
    ['timing', 'could not write timing file', Error],
  ])
})