From a326853900967b2f32fc0a873cc5a23d221d8a36 Mon Sep 17 00:00:00 2001 From: Justin Weisberg Date: Sun, 18 Apr 2021 21:02:15 -0700 Subject: [PATCH] feat(hardening): ensure that an instance is provided to mock properly test expected constructor error --- src/index.js | 3 ++- test/basics.spec.js | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 5d6eb97..4e99bc9 100644 --- a/src/index.js +++ b/src/index.js @@ -52,7 +52,6 @@ function resetHistory() { function MockAdapter(axiosInstance, options) { reset.call(this); - // TODO throw error instead when no axios instance is provided if (axiosInstance) { this.axiosInstance = axiosInstance; this.originalAdapter = axiosInstance.defaults.adapter; @@ -60,6 +59,8 @@ function MockAdapter(axiosInstance, options) { options && options.delayResponse > 0 ? options.delayResponse : null; this.onNoMatch = (options && options.onNoMatch) || null; axiosInstance.defaults.adapter = this.adapter.call(this); + } else { + throw new Error("Please provide an instance of axios to mock"); } } diff --git a/test/basics.spec.js b/test/basics.spec.js index 9976cc6..67b899a 100644 --- a/test/basics.spec.js +++ b/test/basics.spec.js @@ -17,6 +17,16 @@ describe("MockAdapter basics", function () { expect(instance.defaults.adapter).to.exist; }); + it("correctly throws an error when attempting to instantiate an undefined axios instance", function () { + var emptyInstance = undefined; + var constructorFunc = function () { + new MockAdapter(emptyInstance); + }; + expect(constructorFunc).to.throw( + "Please provide an instance of axios to mock" + ); + }); + it("calls interceptors", function () { instance.interceptors.response.use( function (config) {