Jest tests fail with error stating 'bind' property is undefined when importing Airtable

I’m facing an issue while executing my Jest tests. Whenever I attempt to import the Airtable library in my test scripts, I encounter an error that halts the process.

The problem arises immediately upon importing Airtable:

import AirtableService from 'airtable'

This is the error message I receive:

TypeError: Cannot read property 'bind' of undefined

    > 1 | import AirtableService from 'airtable'
        | ^
     

      at Object.<anonymous> (node_modules/airtable/src/fetch.ts:5:80)
      at Object.<anonymous> (node_modules/airtable/src/base.ts:5:1)
      at Object.<anonymous> (node_modules/airtable/src/airtable.ts:1:1)

It appears that the issue is originating from the Airtable library itself, particularly within the fetch.ts file. Has anyone else experienced this? I’m uncertain whether this is a configuration problem with Jest or something else.

I encountered the same problem recently with Jest and Airtable. The issue primarily stems from Jest’s default environment, which lacks certain browser APIs required by Airtable, notably the fetch API. To resolve this, you can modify your Jest configuration to use jsdom by adding testEnvironment: 'jsdom' to your jest.config.js file. Alternatively, you could install whatwg-fetch as a polyfill in your testing setup, which is what I opted for since other aspects of my testing needed the node environment. This adjustment resolved the bind property error, allowing Airtable imports to function correctly in my tests.