summaryrefslogtreecommitdiff
path: root/deps/npm/test/lib/utils/display.js
blob: c7332bce8e2492e053bd6a55e695e2ef39b843a7 (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
83
84
85
const t = require('tap')
const log = require('../../../lib/utils/log-shim')
const mockLogs = require('../../fixtures/mock-logs')
const mockGlobals = require('../../fixtures/mock-globals')

const mockDisplay = (t, mocks) => {
  const { logs, logMocks } = mockLogs(mocks)
  const Display = t.mock('../../../lib/utils/display', {
    ...mocks,
    ...logMocks,
  })
  const display = new Display()
  t.teardown(() => display.off())
  return { display, logs }
}

t.test('setup', async (t) => {
  const { display } = mockDisplay(t)

  display.load({ timing: true, loglevel: 'notice' })
  t.equal(log.level, 'notice')

  display.load({ timing: false, loglevel: 'notice' })
  t.equal(log.level, 'notice')

  display.load({ color: true })
  t.equal(log.useColor(), true)

  display.load({ unicode: true })
  t.equal(log.gauge._theme.hasUnicode, true)

  display.load({ unicode: false })
  t.equal(log.gauge._theme.hasUnicode, false)

  mockGlobals(t, { 'process.stderr.isTTY': true })
  display.load({ progress: true })
  t.equal(log.progressEnabled, true)
})

t.test('can log', async (t) => {
  const explains = []
  const { display, logs } = mockDisplay(t, {
    npmlog: {
      error: (...args) => logs.push(['error', ...args]),
      warn: (...args) => logs.push(['warn', ...args]),
    },
    '../../../lib/utils/explain-eresolve.js': {
      explain: (...args) => {
        explains.push(args)
        return 'explanation'
      },
    },
  })

  display.log('error', 'test')
  t.match(logs.error, [['test']])

  display.log('warn', 'ERESOLVE', 'hello', { some: 'object' })
  t.match(logs.warn, [['ERESOLVE', 'hello']])
  t.match(explains, [[{ some: 'object' }, false, 2]])
})

t.test('handles log throwing', async (t) => {
  const errors = []
  mockGlobals(t, {
    'console.error': (...args) => errors.push(args),
  })
  const { display } = mockDisplay(t, {
    npmlog: {
      verbose: () => {
        throw new Error('verbose')
      },
    },
    '../../../lib/utils/explain-eresolve.js': {
      explain: () => {
        throw new Error('explain')
      },
    },
  })

  display.log('warn', 'ERESOLVE', 'hello', { some: 'object' })
  t.match(errors, [
    [/attempt to log .* crashed/, Error('explain'), Error('verbose')],
  ])
})