Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/// dont_print option
nock.recorder.rec({
dont_print: true,
})
// ... some HTTP calls
const nockCalls = nock.recorder.play()
/// output_objects option
nock.recorder.rec({
output_objects: true,
})
// ... some HTTP calls
const nockCallObjects = nock.recorder.play()
let nocks = nock.load(str)
nocks.forEach(nock => {
nock = nock.filteringRequestBody((body: string) => {
return body
})
})
// Pre-process the nock definitions as scope filtering has to be defined before the nocks are defined (due to its very hacky nature).
const nockDefs = nock.loadDefs(str)
nockDefs.forEach(def => {
// Do something with the definition object e.g. scope filtering.
def.options = def.options || {}
def.options.filteringScope = (scope: string) => {
return /^https:\/\/api[0-9]*.example.test/.test(scope)
}
})
// Load the nocks from pre-processed definitions.
function initNock(fixture, opts) {
var fixtureFile = './test/fixtures/' + fixture;
var nocked = {};
var requiredNocks = [];
opts = opts || {};
opts.not_required = opts.not_required || [];
var nockMode = opts.mode || defaultNockMode;
var nocks;
if (nockMode === 'PLAY') {
nocks = nock.load(fixtureFile);
if (opts.processor) {
var ret = opts.processor(nocks);
if (typeof ret != 'undefined') {
nocks = ret;
}
}
nocks.forEach(function(n, i) {
nocked[n.name || 'loaded_' + i] = n;
});
// allow some mocks to be not required
Object.keys(nocked).filter(function(name) {
return opts.not_required.indexOf(name) < 0;
function beforeTest(cassettePath, options) {
nockReset();
// I feel like this could be written better. Some duplication here
if (options.mode === 'all') {
nockStartRecording();
} else if (fs.existsSync(cassettePath)) {
if (!nock.isActive()) {
nock.activate();
}
nock.load(cassettePath);
} else {
nockStartRecording();
}
}
test('streams trades', done => {
nock.load('./tests/mocks/pubclient_stream_trades.json');
let last = from;
let current;
publicClient
.getProductTradeStream('BTC-USD', from, to)
.on('data', data => {
current = data.trade_id;
assert.equal(typeof current, 'number');
assert.equal(
current,
last + 1,
current + ' is next in series, last: ' + last
);
last = current;
})
test('.getProductTradeStream() with current date function', done => {
nock.load('./tests/mocks/pubclient_stream_trades_function.json');
let last = from;
let current;
publicClient
.getProductTradeStream(
'BTC-USD',
from,
trade => Date.parse(trade.time) >= Date.now()
)
.on('data', data => {
current = data.trade_id;
assert.equal(typeof current, 'number');
assert.equal(
current,
last + 1,
current + ' is next in series, last: ' + last
before(function() {
github = new Github({
username: testUser.USERNAME,
password: testUser.PASSWORD,
auth: 'basic',
});
nock.load('test/fixtures/search.json');
});
before(function () {
if (mocksExist) {
nock.load(mocksPath);
} else {
TEST_DELAY = 1000;
nock.recorder.rec({
dont_print: true,
output_objects: true
});
}
});
before(() => {
if (process.env.RECORD_MOCK_DATA) {
nock.recorder.rec({
output_objects: true,
dont_print: true,
})
} else {
nock.load(mockDataFilename)
}
})