Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nock now returns 500 when cassette missing #1404

Merged
merged 2 commits into from Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/modules/request-recorder.js
Expand Up @@ -91,8 +91,21 @@ export default (opts) => {
nockBack.setMode(hasCassette ? 'lockdown' : 'record');
nockBack.fixtures = opts.cassetteFolder;
nockMock.patch();
nockListener.subscribe('no match', () => {
nockListener.subscribe('no match', (req) => {
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') {
// 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);
};

const { protocol, options, body } = requestInjector.getLast();
if (anyFlagPresent(['record'])) {
expectedCassette.push(async () => {
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