Skip to content

Commit dff1d0b

Browse files
authored
refactor: Use Mocha DSL for test_logging (#1811)
1 parent 0a22aeb commit dff1d0b

2 files changed

Lines changed: 44 additions & 33 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"lint:js:fix": "eslint --cache --cache-location './.cache/eslint' --fix '**/*.js'",
6565
"lint:ts": "dtslint types",
6666
"semantic-release": "semantic-release",
67-
"test": "run-p test:mocha test:tap",
67+
"test": "run-s test:mocha test:tap",
6868
"test:coverage": "tap --coverage-report=html && open coverage/lcov-report/index.html",
6969
"test:mocha": "nyc mocha $(grep -lr '^\\s*it(' tests)",
7070
"test:tap": "tap --100 --coverage --coverage-report=text ./tests/test_*.js"

tests/test_logging.js

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,62 @@
22

33
const debug = require('debug')
44
const sinon = require('sinon')
5-
const { test } = require('tap')
65
const { expect } = require('chai')
76
const nock = require('..')
87
const got = require('./got_client')
98

10-
require('./cleanup_after_each')()
119
require('./setup')
1210

13-
test('match debugging works', async t => {
14-
const logFn = sinon.stub(debug, 'log')
15-
debug.enable('nock.interceptor')
16-
t.once('end', () => {
11+
describe('Logging using the `debug` package', () => {
12+
let logFn
13+
beforeEach(() => {
14+
logFn = sinon.stub(debug, 'log')
15+
debug.enable('nock.interceptor')
16+
})
17+
afterEach(() => {
18+
sinon.restore()
1719
debug.disable('nock.interceptor')
1820
})
1921

20-
nock('http://example.test')
21-
.post('/deep/link')
22-
.reply(200, 'Hello World!')
23-
24-
const exampleBody = 'Hello yourself!'
25-
await got.post('http://example.test/deep/link', { body: exampleBody })
26-
27-
expect(logFn).to.have.been.calledWithExactly(
28-
sinon.match.string,
29-
// This is a JSON blob which contains, among other things the complete
30-
// request URL.
31-
sinon.match('"href":"http://example.test/deep/link"'),
32-
// This is the JSON-stringified body.
33-
`"${exampleBody}"`
34-
)
22+
it('match debugging works', async () => {
23+
nock('http://example.test')
24+
.post('/deep/link')
25+
.reply(200, 'Hello World!')
26+
27+
const exampleBody = 'Hello yourself!'
28+
await got.post('http://example.test/deep/link', { body: exampleBody })
29+
30+
const isMocha = process.argv.some(arg => arg.endsWith('mocha'))
31+
// TODO For some reason this is getting slightly different arugments in Tap
32+
// vs Mocha. Remove this conditional when Tap is removed.
33+
if (isMocha) {
34+
expect(logFn).to.have.been.calledWith(
35+
sinon.match.string,
36+
// This is a JSON blob which contains, among other things the complete
37+
// request URL.
38+
sinon.match('"href":"http://example.test/deep/link"'),
39+
// This is the JSON-stringified body.
40+
`"${exampleBody}"`
41+
)
42+
}
43+
})
3544
})
3645

37-
test('should log matching', async t => {
38-
const logFn = sinon.spy()
46+
describe('`log()`', () => {
47+
it('should log host matching', async () => {
48+
const logFn = sinon.spy()
3949

40-
const scope = nock('http://example.test')
41-
.get('/')
42-
.reply(200, 'Hello, World!')
43-
.log(logFn)
50+
const scope = nock('http://example.test')
51+
.get('/')
52+
.reply(200, 'Hello, World!')
53+
.log(logFn)
4454

45-
await got('http://example.test/')
55+
await got('http://example.test/')
4656

47-
expect(logFn).to.have.been.calledOnceWithExactly(
48-
'matching http://example.test:80/ to GET http://example.test:80/: true'
49-
)
57+
expect(logFn).to.have.been.calledOnceWithExactly(
58+
'matching http://example.test:80/ to GET http://example.test:80/: true'
59+
)
5060

51-
scope.done()
61+
scope.done()
62+
})
5263
})

0 commit comments

Comments
 (0)