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

ping.js « lib « test - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cf47530749b33451ddd084390f8b6d4f0a69ee56 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const { test } = require('tap')
const requireInject = require('require-inject')

test('pings', (t) => {
  t.plan(8)

  const flatOptions = { registry: 'https://registry.npmjs.org' }
  let noticeCalls = 0
  const Ping = requireInject('../../lib/ping.js', {
    '../../lib/utils/ping.js': function (spec) {
      t.equal(spec, flatOptions, 'passes flatOptions')
      return {}
    },
    npmlog: {
      notice: (type, spec) => {
        ++noticeCalls
        if (noticeCalls === 1) {
          t.equal(type, 'PING', 'should log a PING')
          t.equal(spec, flatOptions.registry, 'should log the registry url')
        } else {
          t.equal(type, 'PONG', 'should log a PONG')
          t.match(spec, /\d+ms/, 'should log the elapsed milliseconds')
        }
      },
    },
  })
  const ping = new Ping({ flatOptions })

  ping.exec([], (err) => {
    t.equal(noticeCalls, 2, 'should have logged 2 lines')
    t.ifError(err, 'npm ping')
    t.ok('should be able to ping')
  })
})

test('pings and logs details', (t) => {
  t.plan(10)

  const flatOptions = { registry: 'https://registry.npmjs.org' }
  const details = { extra: 'data' }
  let noticeCalls = 0
  const Ping = requireInject('../../lib/ping.js', {
    '../../lib/utils/ping.js': function (spec) {
      t.equal(spec, flatOptions, 'passes flatOptions')
      return details
    },
    npmlog: {
      notice: (type, spec) => {
        ++noticeCalls
        if (noticeCalls === 1) {
          t.equal(type, 'PING', 'should log a PING')
          t.equal(spec, flatOptions.registry, 'should log the registry url')
        } else if (noticeCalls === 2) {
          t.equal(type, 'PONG', 'should log a PONG')
          t.match(spec, /\d+ms/, 'should log the elapsed milliseconds')
        } else {
          t.equal(type, 'PONG', 'should log a PONG')
          const parsed = JSON.parse(spec)
          t.match(parsed, details, 'should log JSON stringified details')
        }
      },
    },
  })
  const ping = new Ping({ flatOptions })

  ping.exec([], (err) => {
    t.equal(noticeCalls, 3, 'should have logged 3 lines')
    t.ifError(err, 'npm ping')
    t.ok('should be able to ping')
  })
})

test('pings and returns json', (t) => {
  t.plan(11)

  const flatOptions = { registry: 'https://registry.npmjs.org', json: true }
  const details = { extra: 'data' }
  let noticeCalls = 0
  const Ping = requireInject('../../lib/ping.js', {
    '../../lib/utils/ping.js': function (spec) {
      t.equal(spec, flatOptions, 'passes flatOptions')
      return details
    },
    '../../lib/utils/output.js': function (spec) {
      const parsed = JSON.parse(spec)
      t.equal(parsed.registry, flatOptions.registry, 'returns the correct registry url')
      t.match(parsed.details, details, 'prints returned details')
      t.type(parsed.time, 'number', 'returns time as a number')
    },
    npmlog: {
      notice: (type, spec) => {
        ++noticeCalls
        if (noticeCalls === 1) {
          t.equal(type, 'PING', 'should log a PING')
          t.equal(spec, flatOptions.registry, 'should log the registry url')
        } else {
          t.equal(type, 'PONG', 'should log a PONG')
          t.match(spec, /\d+ms/, 'should log the elapsed milliseconds')
        }
      },
    },
  })
  const ping = new Ping({ flatOptions })

  ping.exec([], (err) => {
    t.equal(noticeCalls, 2, 'should have logged 2 lines')
    t.ifError(err, 'npm ping')
    t.ok('should be able to ping')
  })
})