Testing
Mock requests with nock (recommended)
nock (recommended)// We'll mock a request to fetch some products
nock('https://api.commercetools.com')
.defaultReplyHeaders({ 'Content-Type': 'application/json' })
.get('/my-project/products')
.reply(200, {
total: 2,
count: 2,
results: [{ id: '1' }, { id: '2' }],
})
// or mock a request to create a channel
nock('https://api.commercetools.com')
.defaultReplyHeaders({ 'Content-Type': 'application/json' })
.filteringRequestBody(() => '*')
.post('/my-project/channels', '*')
.reply(201, { id: '1' })
// we could also do assertions on the body payload
.filteringRequestBody((body) => {
expect(body).toBe(JSON.stringify({ foo: 'bar' }))
return '*'
})
// Note that by default the defined mocks (or interceptors) are used only once
// https://github.com/node-nock/nock#read-this---about-interceptors
// To re-use the same interceptor for multiple requests simply call `persist()`
// https://github.com/node-nock/nock#persistMock middlewares
Mock modules
Last updated