Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
this.nockScope = nock(endpoint, { encodedQueryParams: true })
this.recording = record
if (record) {
nock.recorder.rec({
output_objects: true,
})
}
this.anyRequestSetUp = false
this.debug = debug
// ethers hard-codes this value, see https://github.com/ethers-io/ethers.js/issues/489
this._rpcRequestId = 42
this._noMatches = []
nock.emitter.on('no match', (clientRequestObject, options, body) => {
this._noMatches.push(body)
if (debug) {
if (!this.anyRequestSetUp) {
console.log(
new Error('No mocks have been set up, but a request was made!')
)
}
if (!body) {
console.log(
new Error(
'no body? (is there a jest.fakeTimers call? you must reset mocks' +
', see https://github.com/sapegin/jest-cheat-sheet#clearing-and-restoring-mocks'
)
)
}
console.log(`NO HTTP MOCK EXISTS FOR THAT REQUEST\n${body}`)
hostname: 'localhost',
path: '/mockedResource',
})
nock.removeInterceptor({
hostname: 'localhost',
path: '/login',
method: 'POST',
proto: 'https',
})
const interceptor = nock('http://example.test').get('somePath')
nock.removeInterceptor(interceptor)
// Events
/// Global no match event
nock.emitter.on('no match', (req: any) => {})
// Nock Back
/// Setup
nock.back.fixtures = '/path/to/fixtures/'
nock.back.setMode('record')
/// Usage
const before = (def: nock.Definition) => {
def.options = def.options || {}
def.options.filteringScope = (scope: string) => {
return /^https:\/\/api[0-9]*.example.test/.test(scope)
}
}
const after = (scope: nock.Scope) => {
scope = scope.filteringRequestBody((body: string): string => {
return body
public done() {
// scope.done() will throw an error if there are expected api calls that have not happened.
// So ensures that all expected calls have been made.
this.scope.done();
// Remove 'no match' handler, for tests using nock without this module
nock.emitter.removeListener('no match', this.handleUnexpectedRequest);
// Restore unmocked behaviour
nock.cleanAll();
nock.restore();
}
// Set up an FTP server that will respond in line with test data.
ftp = setupFtp();
// Mess with node DNS so that a call to any host that starts with FTP actually gets resolved to our local FTP server.
sinon.stub(dns, "lookup").callsFake((hostname, options, callback) => {
if (hostname.startsWith("ftp")) {
callback(null, "127.0.0.1", 4);
} else {
originalDns(hostname, options, callback);
}
});
sinon.stub(console, "info");
nock.emitter.on("no match", onMatchFail);
});
after(() => {
nock.emitter.removeListener('no match', unmockedListener);
return server.stop();
});
.then(() => nock.emitter.on('no match', unmockedListener));
});
constructor() {
nock.cleanAll();
if (!nock.isActive()) {
nock.activate();
}
this.scope = nock(BalenaAPIMock.basePathPattern);
nock.emitter.on('no match', this.handleUnexpectedRequest);
}
after(() => {
dns.lookup.restore();
(console.info as any).restore();
ftp.close();
nock.emitter.removeListener("no match", onMatchFail);
});