axios-mock-adapter icon indicating copy to clipboard operation
axios-mock-adapter copied to clipboard

TypeError: Cannot set property 'handlers' of undefined

Open eau-de-la-seine opened this issue 3 years ago • 1 comments

Hello Colin,

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

Project type: Back-end NodeJS with ES6 modules

Dependencies:

  • axios version ^0.27.2
  • axios-mock-adapter version ^1.21.1
  • jasmine version ^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)

eau-de-la-seine avatar Jul 11 '22 12:07 eau-de-la-seine

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.

jndietz avatar Oct 01 '22 00:10 jndietz