From 99cf5e97c40def1b1d682c67614e15551baca2ed Mon Sep 17 00:00:00 2001 From: Marc Bachmann Date: Mon, 5 Aug 2024 11:51:29 +0200 Subject: [PATCH] Improve 'Could not find mock for' message by providing headers and params context --- src/index.js | 10 +++++----- src/utils.js | 7 ++++++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index d68e686..cde8c28 100644 --- a/src/index.js +++ b/src/index.js @@ -84,17 +84,17 @@ function convertDataAndConfigToConfig (method, data, config) { if (methodsWithConfigsAsSecondArg.includes(method)) { return validateconfig(method, data || {}); } else { - return validateconfig(method, Object.assign({}, config || {}, { data: data })); + return validateconfig(method, Object.assign({}, config, { data: data })); } } -var allowedConfigs = ['headers', 'params', 'data']; +var allowedConfigProperties = ['headers', 'params', 'data']; function validateconfig (method, config) { for (var key in config) { - if (!allowedConfigs.includes(key)) { + if (!allowedConfigProperties.includes(key)) { throw new Error( - 'Invalid config attribute ' + - key + + 'Invalid config property ' + + JSON.stringify(key) + ' provided to ' + toMethodName(method) + '. Config: ' + diff --git a/src/utils.js b/src/utils.js index 59916ef..ba06240 100644 --- a/src/utils.js +++ b/src/utils.js @@ -166,7 +166,12 @@ function createAxiosError(message, config, response, code) { function createCouldNotFindMockError(config) { var message = "Could not find mock for: \n" + - JSON.stringify(config, ["method", "url"], 2); + JSON.stringify({ + method: config.method, + url: config.url, + params: config.params, + headers: config.headers + }, null, 2); var error = new Error(message); error.isCouldNotFindMockError = true; error.url = config.url;