Moralis Error while Testing in Jest

I am trying to initialize the Moralis SDK using the function Moralis.start({apiKey: 'YOUR API KEY'}). But receiving the following error:

 TypeError: Moralis.start is not a function

      28 | });
      29 |
    > 30 | Moralis.start({
         |         ^
      31 |   apiKey: process.env.NEXT_PUBLIC_MORALIS_API_KEY,
      32 | });
      33 |

This works in production mode. But fails while doing integration testing in Jest.

Follwing are my package versions:
Moralis: 2.6.0
Framework: NextJS(12.3.1)
Jest: 29.2.0

Any kind of help is appreciated :slight_smile:

In case if you are using require method to import you have to do in in this way

const Moralis = require("moralis").default;

Can you also show your full code that uses Moralis, as well as your Jest test code.

Hi John,

I am using the ES module, so I am using the import statement:

import Moralis from 'moralis'

Hi Alex,

Below is the Jest Code:

import Moralis from 'moralis';

Moralis.start({
  apiKey: process.env.NEXT_PUBLIC_MORALIS_API_KEY,
});

describe('BlockChain Info' , () =>{
    test('BlockchainInfo Snapshot', () => {
         const{conatiner} = render(
               <WagmiConfig client={wagmiClient}>
                     <BlockchainInfo />
               </WagmiConfig>
         );
         expect(container).toMatchSnapshot();
    });
});

The Moralis API is being futher used in the BlockChainInfo component to fetch the balance. But before that, we have to initialize the SDK which is done through Moralis.start(). And I face the mentioned error, before the test is executed.

Is there a particular Jest configuration you’re using? I am not getting that particular Moralis.start is not a function error with a similar one.

Jest configuration below:

// jest.config.js
const nextJest = require('next/jest');

const createJestConfig = nextJest({
  // Provide the path to your Next.js app to load next.config.js and .env files in your test environment
  dir: './',
});

// Add any custom config to be passed to Jest
/** @type {import('jest').Config} */
const customJestConfig = {
  extensionsToTreatAsEsm: ['.ts', '.tsx'],
  // Add more setup options before each test is run
  setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
  // if using TypeScript with a baseUrl set to the root directory then you need the below for alias' to work
  moduleDirectories: ['node_modules', '<rootDir>/'],
  // preset: 'ts-jest/presets/js-with-ts',

  moduleFileExtensions: ['js', 'jsx', 'tsx', 'ts'],
  testEnvironment: 'jest-environment-jsdom',
  moduleNameMapper: {
    '^@/components/(.*)$': '<rootDir>/components/$1',
    '^uuid$': require.resolve('uuid'),
  },
  transform: {
    '^.+\\.tsx?$': [
      'ts-jest',
      {
        useESM: true,
      },
    ],
  },
};

// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
module.exports = createJestConfig(customJestConfig);

I have a feeling something might be weird with my configuration… :thinking:

I had to comment out the setupFilesAfterEnv line but that config seems to work for me (although I get a different error related to the use of NextAuth) with a render of MyApp component coming from this tutorial.

Try updating moralis - I got an error related to the moralis import when I used [email protected] but not a higher one of 2.6.5.