Search code examples
javascriptreactjsreact-nativejestjsexpo

TypeError: _API.default is not a constructor with Jest tests


I have an API class that I am trying to use in a React app.

// API file

class API {
...
}

export default API;

// Other file
import API from "utils/API";

const api = new API();

And I am getting the error:

TypeError: _API.default is not a constructor

But.. it seems like my default is set?

My Jest setup is like this:

  "jest": {
    "setupFiles": [
      "./jestSetupFile.js"
    ],
    "testEnvironment": "jsdom",
    "preset": "jest-expo",
    "transformIgnorePatterns": [
      "node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg|react-router-native/.*|@invertase/react-native-apple-authentication/.*)"
    ]
  },

My strong guess is that this is due to a configuration of my babel, webpack or package.json.

What could be causing this?

Note, I want to be clear, this doesn't happen whatsoever in my main application, only in Jest testing


If I change it to a named export/import, I get this:

TypeError: _API.API is not a constructor

Extremely confusing behavior.


Solution

  • This was ultimately due to additional code inside the file that I was exporting the class from.

    import { store } from "root/App";
    
    if (typeof store !== "undefined") {
      let storeState = store.getState();
      let profile = storeState.profile;
    }
    

    At the top, outside my class for some functionality I had been working on.

    This caused the class default export to fail, but only in Jest, not in my actual application.