summaryrefslogtreecommitdiffstats
path: root/test/mock-interceptor-unused-assertions.js
blob: bfa2275777f7f861240294e5ca53f6a410bd100a (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
'use strict'

const { test, beforeEach, afterEach } = require('tap')
const { MockAgent, setGlobalDispatcher } = require('..')
const PendingInterceptorsFormatter = require('../lib/mock/pending-interceptors-formatter')

// Avoid colors in the output for inline snapshots.
const pendingInterceptorsFormatter = new PendingInterceptorsFormatter({ disableColors: true })

let originalGlobalDispatcher

const origin = 'https://localhost:9999'

beforeEach(() => {
  // Disallow all network activity by default by using a mock agent as the global dispatcher
  const globalDispatcher = new MockAgent()
  globalDispatcher.disableNetConnect()
  setGlobalDispatcher(globalDispatcher)
  originalGlobalDispatcher = globalDispatcher
})

afterEach(() => {
  setGlobalDispatcher(originalGlobalDispatcher)
})

function mockAgentWithOneInterceptor () {
  const agent = new MockAgent()
  agent.disableNetConnect()

  agent
    .get('https://example.com')
    .intercept({ method: 'GET', path: '/' })
    .reply(200, '')

  return agent
}

test('1 pending interceptor', t => {
  t.plan(2)

  const err = t.throws(() => mockAgentWithOneInterceptor().assertNoPendingInterceptors({ pendingInterceptorsFormatter }))

  t.same(err.message, `
1 interceptor is pending:

┌─────────┬────────┬───────────────────────┬──────┬─────────────┬────────────┬─────────────┬───────────┐
│ (index) │ Method │        Origin         │ Path │ Status code │ Persistent │ Invocations │ Remaining │
├─────────┼────────┼───────────────────────┼──────┼─────────────┼────────────┼─────────────┼───────────┤
│    0    │ 'GET'  │ 'https://example.com' │ '/'  │     200     │    '❌'    │      0      │     1     │
└─────────┴────────┴───────────────────────┴──────┴─────────────┴────────────┴─────────────┴───────────┘
`.trim())
})

test('2 pending interceptors', t => {
  t.plan(2)

  const withTwoInterceptors = mockAgentWithOneInterceptor()
  withTwoInterceptors
    .get(origin)
    .intercept({ method: 'get', path: '/some/path' })
    .reply(204, 'OK')
  const err = t.throws(() => withTwoInterceptors.assertNoPendingInterceptors({ pendingInterceptorsFormatter }))

  t.same(err.message, `
2 interceptors are pending:

┌─────────┬────────┬──────────────────────────┬──────────────┬─────────────┬────────────┬─────────────┬───────────┐
│ (index) │ Method │          Origin          │     Path     │ Status code │ Persistent │ Invocations │ Remaining │
├─────────┼────────┼──────────────────────────┼──────────────┼─────────────┼────────────┼─────────────┼───────────┤
│    0    │ 'GET'  │  'https://example.com'   │     '/'      │     200     │    '❌'    │      0      │     1     │
│    1    │ 'GET'  │ 'https://localhost:9999' │ '/some/path' │     204     │    '❌'    │      0      │     1     │
└─────────┴────────┴──────────────────────────┴──────────────┴─────────────┴────────────┴─────────────┴───────────┘
`.trim())
})

test('Variations of persist(), times(), and pending status', async t => {
  t.plan(7)

  // Agent with unused interceptor
  const agent = mockAgentWithOneInterceptor()

  // Unused with persist()
  agent
    .get(origin)
    .intercept({ method: 'get', path: '/persistent/unused' })
    .reply(200, 'OK')
    .persist()

  // Used with persist()
  agent
    .get(origin)
    .intercept({ method: 'GET', path: '/persistent/used' })
    .reply(200, 'OK')
    .persist()
  t.same((await agent.request({ origin, method: 'GET', path: '/persistent/used' })).statusCode, 200)

  // Consumed without persist()
  agent.get(origin)
    .intercept({ method: 'post', path: '/transient/pending' })
    .reply(201, 'Created')
  t.same((await agent.request({ origin, method: 'POST', path: '/transient/pending' })).statusCode, 201)

  // Partially pending with times()
  agent.get(origin)
    .intercept({ method: 'get', path: '/times/partial' })
    .reply(200, 'OK')
    .times(5)
  t.same((await agent.request({ origin, method: 'GET', path: '/times/partial' })).statusCode, 200)

  // Unused with times()
  agent.get(origin)
    .intercept({ method: 'get', path: '/times/unused' })
    .reply(200, 'OK')
    .times(2)

  // Fully pending with times()
  agent.get(origin)
    .intercept({ method: 'get', path: '/times/pending' })
    .reply(200, 'OK')
    .times(2)
  t.same((await agent.request({ origin, method: 'GET', path: '/times/pending' })).statusCode, 200)
  t.same((await agent.request({ origin, method: 'GET', path: '/times/pending' })).statusCode, 200)

  const err = t.throws(() => agent.assertNoPendingInterceptors({ pendingInterceptorsFormatter }))

  t.same(err.message, `
4 interceptors are pending:

┌─────────┬────────┬──────────────────────────┬──────────────────────┬─────────────┬────────────┬─────────────┬───────────┐
│ (index) │ Method │          Origin          │         Path         │ Status code │ Persistent │ Invocations │ Remaining │
├─────────┼────────┼──────────────────────────┼──────────────────────┼─────────────┼────────────┼─────────────┼───────────┤
│    0    │ 'GET'  │  'https://example.com'   │         '/'          │     200     │    '❌'    │      0      │     1     │
│    1    │ 'GET'  │ 'https://localhost:9999' │ '/persistent/unused' │     200     │    '✅'    │      0      │ Infinity  │
│    2    │ 'GET'  │ 'https://localhost:9999' │   '/times/partial'   │     200     │    '❌'    │      1      │     4     │
│    3    │ 'GET'  │ 'https://localhost:9999' │   '/times/unused'    │     200     │    '❌'    │      0      │     2     │
└─────────┴────────┴──────────────────────────┴──────────────────────┴─────────────┴────────────┴─────────────┴───────────┘
`.trim())
})

test('works when no interceptors are registered', t => {
  t.plan(2)

  const agent = new MockAgent()
  agent.disableNetConnect()

  t.same(agent.pendingInterceptors(), [])
  t.doesNotThrow(() => agent.assertNoPendingInterceptors())
})

test('works when all interceptors are pending', async t => {
  t.plan(4)

  const agent = new MockAgent()
  agent.disableNetConnect()

  agent.get(origin).intercept({ method: 'get', path: '/' }).reply(200, 'OK')
  t.same((await agent.request({ origin, method: 'GET', path: '/' })).statusCode, 200)

  agent.get(origin).intercept({ method: 'get', path: '/persistent' }).reply(200, 'OK')
  t.same((await agent.request({ origin, method: 'GET', path: '/persistent' })).statusCode, 200)

  t.same(agent.pendingInterceptors(), [])
  t.doesNotThrow(() => agent.assertNoPendingInterceptors())
})

test('defaults to rendering output with terminal color when process.env.CI is unset', t => {
  t.plan(2)

  // This ensures that the test works in an environment where the CI env var is set.
  const oldCiEnvVar = process.env.CI
  delete process.env.CI

  const err = t.throws(
    () => mockAgentWithOneInterceptor().assertNoPendingInterceptors())
  t.same(err.message, `
1 interceptor is pending:

┌─────────┬────────┬───────────────────────┬──────┬─────────────┬────────────┬─────────────┬───────────┐
│ (index) │ Method │        Origin         │ Path │ Status code │ Persistent │ Invocations │ Remaining │
├─────────┼────────┼───────────────────────┼──────┼─────────────┼────────────┼─────────────┼───────────┤
│    0    │ \u001b[32m'GET'\u001b[39m  │ \u001b[32m'https://example.com'\u001b[39m │ \u001b[32m'/'\u001b[39m  │     \u001b[33m200\u001b[39m     │    \u001b[32m'❌'\u001b[39m    │      \u001b[33m0\u001b[39m      │     \u001b[33m1\u001b[39m     │
└─────────┴────────┴───────────────────────┴──────┴─────────────┴────────────┴─────────────┴───────────┘
`.trim())

  // Re-set the CI env var if it were set.
  // Assigning `undefined` does not work,
  // because reading the env var afterwards yields the string 'undefined',
  // so we need to re-set it conditionally.
  if (oldCiEnvVar != null) {
    process.env.CI = oldCiEnvVar
  }
})

test('returns unused interceptors', t => {
  t.plan(1)

  t.same(mockAgentWithOneInterceptor().pendingInterceptors(), [
    {
      timesInvoked: 0,
      times: 1,
      persist: false,
      consumed: false,
      pending: true,
      path: '/',
      method: 'GET',
      body: undefined,
      query: undefined,
      headers: undefined,
      data: {
        error: null,
        statusCode: 200,
        data: '',
        headers: {},
        trailers: {}
      },
      origin: 'https://example.com'
    }
  ])
})