Skip to content

Commit

Permalink
test: Improve tests for header matching with function; adopt new prom…
Browse files Browse the repository at this point in the history
…ise-rejection assertion (#1481)

1. Assert the expected argument is passed. (It's the correct argument, though not the expected type!)
2. When the function returns true, assert the request matches.
3. When the function returns false, assert the request doesn't match.
4. When the function returns false, assert the mock is not consumed.
5. I added copies of these tests for `matchHeader` called on the interceptor instead of the scope.

Test 4 uncovered an unexpected behavior in tap, which is that `t.rejects` causes our `afterEach` hook to fire – since it's implemented as a subtest.

Not knowing how to work around this, I replaced our uses of `t.rejects` with a promise-rejection assertion that does not rely on a subtest.

Ref: #1305 (comment) and tapjs/tapjs#525
  • Loading branch information
paulmelnikow committed Mar 26, 2019
1 parent 9e9e65d commit 75cceb6
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 45 deletions.
71 changes: 43 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"devDependencies": {
"acorn": "^6.0.4",
"assert-rejects": "^1.0.0",
"async": "^2.6.0",
"aws-sdk": "^2.202.0",
"coveralls": "^3.0.0",
Expand Down
17 changes: 11 additions & 6 deletions tests/test_basic_auth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const { test } = require('tap')
const assertRejects = require('assert-rejects')
const got = require('got')
const nock = require('..')

Expand All @@ -24,9 +25,11 @@ test('basic auth with username and password', async t => {
})

await t.test('fails when it doesnt match', async tt => {
await tt.rejects(() => got('http://example.test/test'), {
message: 'Nock: No match for request',
})
await assertRejects(
got('http://example.test/test'),
Error,
'Nock: No match for request'
)
})
})

Expand All @@ -46,8 +49,10 @@ test('basic auth with username only', async t => {
})

await t.test('fails when it doesnt match', async tt => {
await tt.rejects(() => got('http://example.test/test'), {
message: 'Nock: No match for request',
})
await assertRejects(
got('http://example.test/test'),
Error,
'Nock: No match for request'
)
})
})
9 changes: 6 additions & 3 deletions tests/test_delay.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fs = require('fs')
const path = require('path')
const http = require('http')
const stream = require('stream')
const assertRejects = require('assert-rejects')
const got = require('got')
const mikealRequest = require('request')
const { test } = require('tap')
Expand Down Expand Up @@ -364,9 +365,11 @@ test('delay with replyWithError: response is delayed', async t => {
await resolvesInAtLeast(
t,
async () =>
t.rejects(() => got('http://example.test'), {
message: 'this is an error message',
}),
assertRejects(
got('http://example.test'),
Error,
'this is an error message'
),
100
)
})
Expand Down

0 comments on commit 75cceb6

Please sign in to comment.