|
2 | 2 |
|
3 | 3 | const debug = require('debug') |
4 | 4 | const sinon = require('sinon') |
5 | | -const { test } = require('tap') |
6 | 5 | const { expect } = require('chai') |
7 | 6 | const nock = require('..') |
8 | 7 | const got = require('./got_client') |
9 | 8 |
|
10 | | -require('./cleanup_after_each')() |
11 | 9 | require('./setup') |
12 | 10 |
|
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() |
17 | 19 | debug.disable('nock.interceptor') |
18 | 20 | }) |
19 | 21 |
|
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 | + }) |
35 | 44 | }) |
36 | 45 |
|
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() |
39 | 49 |
|
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) |
44 | 54 |
|
45 | | - await got('http://example.test/') |
| 55 | + await got('http://example.test/') |
46 | 56 |
|
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 | + ) |
50 | 60 |
|
51 | | - scope.done() |
| 61 | + scope.done() |
| 62 | + }) |
52 | 63 | }) |
0 commit comments