Skip to content

Commit

Permalink
List interceptors recorded in context
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenprater committed Nov 3, 2023
1 parent 26bbb93 commit bc9d975
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/back.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,23 @@ function load(fixture, options) {
assertScopesFinished: function () {
assertScopes(this.scopes, fixture)
},
query: function () {
const nested = this.scopes.map(scope =>
scope.interceptors.map(interceptor => ({
method: interceptor.method,
uri: interceptor.uri,
basePath: interceptor.basePath,
path: interceptor.path,
queries: interceptor.queries,
counter: interceptor.counter,
body: interceptor.body,
statusCode: interceptor.statusCode,
optional: interceptor.optional,
})),
)

return [].concat.apply([], nested)
},
}

if (fixture && fixtureExists(fixture)) {
Expand Down
18 changes: 18 additions & 0 deletions tests/test_back.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,24 @@ describe('Nock Back', () => {
})
})

it('`query` returns all of the interceptors recorded to the cassette', done => {
nockBack('good_request.json').then(({ nockDone, context }) => {
const interceptor = context.query()
nockDone()
expect(interceptor.length).to.equal(1)
expect(interceptor[0].method).to.equal('GET')
expect(interceptor[0].uri).to.equal('/')
expect(interceptor[0].basePath).to.equal('http://www.example.test:80')
expect(interceptor[0].path).to.equal('/')
expect(interceptor[0].queries).to.be.null()
expect(interceptor[0].counter).to.equal(1)
expect(interceptor[0]).to.have.property('body')
expect(interceptor[0].statusCode).to.equal(200)
expect(interceptor[0].optional).to.equal(false)
done()
})
})

describe('wild mode', () => {
beforeEach(() => {
nockBack.setMode('wild')
Expand Down
13 changes: 13 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,23 @@ declare namespace nock {
}>
}

interface InterceptorSurface {
method: string
uri: string
basePath: string
path: string
queries?: string
counter: number
body: string
statusCode: number
optional: boolean
}

interface BackContext {
isLoaded: boolean
scopes: Scope[]
assertScopesFinished(): void
query: InterceptorSurface[]
}

interface BackOptions {
Expand Down

0 comments on commit bc9d975

Please sign in to comment.