From 4953dfc78d6cd51f6cced87ac6d11c236959160a Mon Sep 17 00:00:00 2001 From: Lukas Siemon Date: Thu, 23 Nov 2023 16:36:28 -0800 Subject: [PATCH 1/2] fix: nock now returns 500 when cassette missing --- src/modules/request-recorder.js | 14 +++++++++++++- test/modules/request-recorder.spec.js | 14 ++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/modules/request-recorder.js b/src/modules/request-recorder.js index 1abfc3b9..5f3614b5 100644 --- a/src/modules/request-recorder.js +++ b/src/modules/request-recorder.js @@ -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'])) { diff --git a/test/modules/request-recorder.spec.js b/test/modules/request-recorder.spec.js index 3687a699..e475654a 100644 --- a/test/modules/request-recorder.spec.js +++ b/test/modules/request-recorder.spec.js @@ -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, []); From 9b8c29fdbdbe4ababb53c751469dd23263b23d18 Mon Sep 17 00:00:00 2001 From: Lukas Siemon Date: Thu, 23 Nov 2023 16:39:10 -0800 Subject: [PATCH 2/2] amend: minor --- src/modules/request-recorder.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/modules/request-recorder.js b/src/modules/request-recorder.js index 5f3614b5..71d8515c 100644 --- a/src/modules/request-recorder.js +++ b/src/modules/request-recorder.js @@ -92,7 +92,9 @@ export default (opts) => { nockBack.fixtures = opts.cassetteFolder; nockMock.patch(); nockListener.subscribe('no match', (req) => { - // todo: remove workaround when https://github.com/nock/nock/issues/2558 is done + assert(hasCassette === true); + + // convert 404 response code to 500 const destroyOriginal = req.destroy; req.destroy = (err) => { if (err.status === 404 && err.statusCode === 404 && err.code === 'ERR_NOCK_NO_MATCH') { @@ -104,7 +106,6 @@ export default (opts) => { return destroyOriginal.call(req, err); }; - assert(hasCassette === true); const { protocol, options, body } = requestInjector.getLast(); if (anyFlagPresent(['record'])) { expectedCassette.push(async () => {