Skip to content

Commit

Permalink
fix: nock now returns 500 when cassette missing
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Siemon committed Nov 24, 2023
1 parent 74a39e7 commit 4953dfc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/modules/request-recorder.js
Expand Up @@ -91,7 +91,19 @@ export default (opts) => {
nockBack.setMode(hasCassette ? 'lockdown' : 'record');
nockBack.fixtures = opts.cassetteFolder;
nockMock.patch();
nockListener.subscribe('no match', () => {
nockListener.subscribe('no match', (req) => {
// todo: remove workaround when https://github.com/nock/nock/issues/2558 is done
const destroyOriginal = req.destroy;
req.destroy = (err) => {
if (err.status === 404 && err.statusCode === 404 && err.code === 'ERR_NOCK_NO_MATCH') {
// eslint-disable-next-line no-param-reassign
err.statusCode = 500;
// eslint-disable-next-line no-param-reassign
err.status = 500;
}
return destroyOriginal.call(req, err);
};

assert(hasCassette === true);
const { protocol, options, body } = requestInjector.getLast();
if (anyFlagPresent(['record'])) {
Expand Down
14 changes: 14 additions & 0 deletions test/modules/request-recorder.spec.js
Expand Up @@ -485,6 +485,20 @@ describe('Testing RequestRecorder', { useTmpDir: true, timestamp: 0 }, () => {
}]);
});

it('Testing nock not found', async ({ capture }) => {
const cassettePath = path.join(tmpDir, cassetteFile);
fs.smartWrite(cassettePath, [
makeCassetteEntry(1),
makeCassetteEntry(3)
]);
const e = await capture(() => runTest({
heal: false,
qs: [1, 2, 3]
}));
expect(e.code).to.equal('ERR_NOCK_NO_MATCH');
expect(e.status).to.equal(500);
});

it('Testing record (https)', async ({ capture }) => {
const cassettePath = path.join(tmpDir, cassetteFile);
fs.smartWrite(cassettePath, []);
Expand Down

0 comments on commit 4953dfc

Please sign in to comment.