I wish to mock testing an Dropbox API using nock
. My issue is with scoping function .isDone()
, which determines if the expectation were met.
The console.log added to nock.post()
is showing matching as true:
matching https://api.dropboxapi.com:443/2/files/create_folder_v2 to POST https://api.dropboxapi.com:443/2/files/create_folder_v2: true
However expect(scope.isDone()).to.be.true;
is failing.
How can I determine what expectation is not getting met?
import { v4 } from 'uuid';
const request = require('request');
import nock = require('nock');
import { expect } from 'chai';
const accessToken = v4();
const folderName = v4();
const folderPath = `/${folderName}`;
let params = {
'path': folderPath,
'autorename': false
};
describe('Run Dropbox Archive Tests', function() {
it('Create a Random Folder using Request', async function() {
const scope = nock('https://api.dropboxapi.com/2', {
reqheaders: {
'authorization': `Bearer ${accessToken}`
}
})
.log(console.log)
.post('/files/create_folder_v2', params)
.reply(200, (uri, requestBody) => {
return {
metadata: {
name: folderName,
path_lower: folderPath,
path_display: folderPath,
id: `id:${v4()}`
}
};
});
request.post({
url: 'https://api.dropboxapi.com/2/files/create_folder_v2',
headers: {
authorization: `Bearer ${accessToken}`
},
json: params
}, function(err, res) {
if (err) {
console.error(`err: ${JSON.stringify(err, null, 2)}`);
} else {
console.log(`res: ${JSON.stringify(res, null, 2)}`);
}
});
expect(scope.isDone()).to.be.true;
});
});
The response from request.post
is as follows:
res: {
"statusCode": 200,
"body": {
"metadata": {
"name": "f303bd77-792a-44b5-844b-5ee29a5d4d44",
"path_lower": "/f303bd77-792a-44b5-844b-5ee29a5d4d44",
"path_display": "/f303bd77-792a-44b5-844b-5ee29a5d4d44",
"id": "id:5760a2b4-73e2-4c85-8d2a-17450bd70e69"
}
},
"headers": {
"content-type": "application/json"
},
"request": {
"uri": {
"protocol": "https:",
"slashes": true,
"auth": null,
"host": "api.dropboxapi.com",
"port": 443,
"hostname": "api.dropboxapi.com",
"hash": null,
"search": null,
"query": null,
"pathname": "/2/files/create_folder_v2",
"path": "/2/files/create_folder_v2",
"href": "https://api.dropboxapi.com/2/files/create_folder_v2"
},
"method": "POST",
"headers": {
"authorization": "Bearer b7f78d23-7033-4c7b-ba24-571ca97a2b42",
"accept": "application/json",
"content-type": "application/json",
"content-length": 67
}
}
}
Thank you, much appreciate any assistance.
Use .pendingMocks, either on an individual scope or on the global Nock instance, to get a list of strings describing which Interceptors
are not "done".
if (!scope.isDone()) {
console.error('pending mocks: %j', scope.pendingMocks())
}
It looks like you're using Mocha, here is a snippet I use. This is in an afterEach
on the global test runner.
afterEach(function() {
// https://github.com/mochajs/mocha/wiki/HOW-TO:-Conditionally-fail-a-test-after-completion
if (this.test && !lodash.has(this.test, "ctx.currentTest.err")) {
const pendingMocks = nock.pendingMocks();
if (pendingMocks.length) {
const msg = ["Not all nock mocks were used:"]
.concat(pendingMocks)
.join("\n\t");
this.test.error(msg);
}
}
nock.cleanAll();
});
If the test has not already failed, and there are pending mocks still, then the test will receive an error listing all the pending Interceptors. cleanAll is then called to remove any persisted or optional mocks.