axios-mock-adapter
axios-mock-adapter copied to clipboard
TypeError: Cannot set property 'handlers' of undefined
Hello Colin,
Encountered error: TypeError: Cannot set property 'handlers' of undefined
Project type: Back-end NodeJS with ES6 modules
Dependencies:
axiosversion^0.27.2axios-mock-adapterversion^1.21.1jasmineversion^4.2.1(the only JS test framework that works properly with ES6 modules and without transpilers)
Code:
'use strict'
import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'
let mockServer = MockAdapter(axios)
Stacktrace:
npm test
> [email protected] test
> jasmine
TypeError: Cannot set property 'handlers' of undefined
at resetHandlers (/home/gekinci/my-projects/infra/wco_services/lambdas/sync-order/node_modules/axios-mock-adapter/src/index.js:41:17)
at reset (/home/gekinci/my-projects/infra/wco_services/lambdas/sync-order/node_modules/axios-mock-adapter/src/index.js:36:17)
at MockAdapter (/home/gekinci/my-projects/infra/wco_services/lambdas/sync-order/node_modules/axios-mock-adapter/src/index.js:49:9)
at file:///home/gekinci/my-projects/infra/wco_services/lambdas/sync-order/utils/audit/audit-axios-client.test.js:6:18
at ModuleJob.run (internal/modules/esm/module_job.js:152:23)
at async Loader.import (internal/modules/esm/loader.js:177:24)
at async Jasmine._loadFiles (/home/gekinci/my-projects/infra/wco_services/lambdas/sync-order/node_modules/jasmine/lib/jasmine.js:215:5)
at async Jasmine.loadSpecs (/home/gekinci/my-projects/infra/wco_services/lambdas/sync-order/node_modules/jasmine/lib/jasmine.js:206:3)
at async Jasmine.execute (/home/gekinci/my-projects/infra/wco_services/lambdas/sync-order/node_modules/jasmine/lib/jasmine.js:502:3)
at async runJasmine (/home/gekinci/my-projects/infra/wco_services/lambdas/sync-order/node_modules/jasmine/lib/command.js:157:5)
I just ran into this myself. Try instantiating the MockAdapter with the new keyword:
let mockServer = new MockAdapter(axios)
👆
Tangentially, I had to use the result of axios.create(), which returns an AxiosInstance, instead of plain axios which is typed as AxiosStatic.