ctimmerm / axios-mock-adapter

Axios adapter that allows to easily mock requests
MIT License
3.47k stars 245 forks source link

TypeError: Cannot set property 'handlers' of undefined #344

Open eau-de-la-seine opened 2 years ago

eau-de-la-seine commented 2 years ago

Hello Colin,

Encountered error: TypeError: Cannot set property 'handlers' of undefined

Project type: Back-end NodeJS with ES6 modules

Dependencies:

Code:

'use strict'

import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'

let mockServer = MockAdapter(axios)

Stacktrace:

npm test

> lambda_sync_order@0.0.0 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)
jndietz commented 2 years ago

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.