Skip to content

Commit

Permalink
simplify a whole bunch
Browse files Browse the repository at this point in the history
  • Loading branch information
theneva committed Apr 25, 2022
1 parent afa0814 commit 11ff74f
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/api/MockAgent.md
Expand Up @@ -484,7 +484,7 @@ const pendingInterceptors = agent.pendingInterceptors()
// ]
```

### `MockAgent.assertNoUnusedInterceptors([options])`
### `MockAgent.assertNoPendingInterceptors([options])`

This method throws if the mock agent has any pending (i.e., non-persistent and not fully consumed) interceptors.

Expand Down
5 changes: 2 additions & 3 deletions lib/mock/mock-agent.js
Expand Up @@ -11,8 +11,7 @@ const {
kNetConnect,
kGetNetConnect,
kOptions,
kFactory,
kGetMockAgentClientsByConsumedStatus
kFactory
} = require('./mock-symbols')
const MockClient = require('./mock-client')
const MockPool = require('./mock-pool')
Expand Down Expand Up @@ -147,7 +146,7 @@ class MockAgent extends Dispatcher {
// .filter(({ consumed, persist, timesInvoked }) => timesInvoked === 0 || !consumed)
}

assertNoUnusedInterceptors (options = {}) {
assertNoPendingInterceptors (options = {}) {
const pending = this.pendingInterceptors()

if (pending.length === 0) {
Expand Down
3 changes: 1 addition & 2 deletions lib/mock/mock-symbols.js
Expand Up @@ -19,6 +19,5 @@ module.exports = {
kIsMockActive: Symbol('is mock active'),
kNetConnect: Symbol('net connect'),
kGetNetConnect: Symbol('get net connect'),
kConnected: Symbol('connected'),
kGetMockAgentClientsByConsumedStatus: Symbol('get mock agent clients by consumed status')
kConnected: Symbol('connected')
}
12 changes: 6 additions & 6 deletions test/mock-interceptor-unused-assertions.js
Expand Up @@ -38,7 +38,7 @@ function mockAgentWithOneInterceptor () {
test('1 pending interceptor', t => {
t.plan(2)

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

t.same(err.message, `
1 interceptor is pending:
Expand All @@ -59,7 +59,7 @@ test('2 pending interceptors', t => {
.get(origin)
.intercept({ method: 'get', path: '/some/path' })
.reply(204, 'OK')
const err = t.throws(() => withTwoInterceptors.assertNoUnusedInterceptors({ tableFormatter }))
const err = t.throws(() => withTwoInterceptors.assertNoPendingInterceptors({ tableFormatter }))

t.same(err.message, `
2 interceptors are pending:
Expand Down Expand Up @@ -121,7 +121,7 @@ test('Variations of persist(), times(), and pending status', async t => {
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.assertNoUnusedInterceptors({ tableFormatter }))
const err = t.throws(() => agent.assertNoPendingInterceptors({ tableFormatter }))

t.same(err.message, `
4 interceptors are pending:
Expand All @@ -144,7 +144,7 @@ test('works when no interceptors are registered', t => {
agent.disableNetConnect()

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

test('works when all interceptors are pending', async t => {
Expand All @@ -160,7 +160,7 @@ test('works when all interceptors are pending', async t => {
t.same((await agent.request({ origin, method: 'GET', path: '/persistent' })).statusCode, 200)

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

test('defaults to rendering output with terminal color when process.env.CI is unset', t => {
Expand All @@ -171,7 +171,7 @@ test('defaults to rendering output with terminal color when process.env.CI is un
delete process.env.CI

const err = t.throws(
() => mockAgentWithOneInterceptor().assertNoUnusedInterceptors())
() => mockAgentWithOneInterceptor().assertNoPendingInterceptors())
t.same(err.message, `
1 interceptor is pending:
Expand Down
2 changes: 1 addition & 1 deletion test/types/mock-agent.test-d.ts
Expand Up @@ -70,5 +70,5 @@ expectAssignable<MockAgent>(new MockAgent({}))
properties?: readonly string[]
): string;
}
}) => void>(agent.assertNoUnusedInterceptors)
}) => void>(agent.assertNoPendingInterceptors)
}
2 changes: 1 addition & 1 deletion types/mock-agent.d.ts
Expand Up @@ -28,7 +28,7 @@ declare class MockAgent<TMockAgentOptions extends MockAgent.Options = MockAgent.
/** Causes all requests to throw when requests are not matched in a MockAgent intercept. */
disableNetConnect(): void;
pendingInterceptors(): MockDispatch[];
assertNoUnusedInterceptors(options?: {tableFormatter?: UnusedInterceptorFormatter}): void;
assertNoPendingInterceptors(options?: {tableFormatter?: UnusedInterceptorFormatter}): void;
}

interface UnusedInterceptorFormatter {
Expand Down

0 comments on commit 11ff74f

Please sign in to comment.