Tests failed because of type error: TypeError: Cannot read properties of undefined (reading 'startsWith')

Hi. I am trying to connect to Mongo Atlas using Nextjs (^13.5.3), mongodb (^6.1.0) and nodejs (20.5.1). My web app is running perfectly, retrieves and updates data as it should. However when I run my tests with Jest and React Tesing Library, the following error is logged:

TypeError: Cannot read properties of undefined (reading 'startsWith')

       7 |
       8 | // Create a MongoClient with a MongoClientOptions object to set the Stable API version
    >  9 | const client = new MongoClient(uri as string, {
         |                ^
      10 |   serverApi: {
      11 |     version: ServerApiVersion.v1,
      12 |     strict: false,

      at connectionStringHasValidScheme (node_modules/mongodb-connection-string-url/src/index.ts:13:22)
      at new ConnectionString (node_modules/mongodb-connection-string-url/src/index.ts:132:30)
      at parseOptions (node_modules/mongodb/src/connection_string.ts:244:15)
      at new MongoClient (node_modules/mongodb/src/mongo_client.ts:331:34)
      at Object.<anonymous> (src/data/mongodb.ts:9:16)
      at Object.<anonymous> (src/components/Sidebar/Sidebar.tsx:12:18)
      at Object.<anonymous> (src/components/Sidebar/__test__/Sidebar.test.tsx:8:57)

I read a same issue related to this: TypeError: Cannot read property 'startsWith' of undefined
The problem was solved by moving the .env files inside the source directory, but that also didn’t work for me.
At this point, I have no idea what could be the problem. My only thought is that the node server isn’t running when I run the tests so the enviroment variables can’t be accessed. But I need to include the Sidebar component so I could test it in the unit test.

Here is the code that throws the error:

const uri = process.env.MONGODB_CONNECTION_STRING;

// Create a MongoClient with a MongoClientOptions object to set the Stable API version
const client = new MongoClient(uri as string, {
  serverApi: {
    version: ServerApiVersion.v1,
    strict: false,
    deprecationErrors: true,
  },
});

Appreciate every help :blush:

I was having the same issues with my code: server runs fine but my tests break.

I isolated the problem and fixed it.
I assume your ‘uri’ is getting its value from an environment variable, am I correct? So, mine code was like this:

export class MongoDB {
  private static URI = process.env.MONGODB_URI;
  private static client = new MongoClient(this.URI!);

Problem is that URI is getting undefined when I run the tests, but not when I run my app. Why?
Well, for some reason, Jest (Which I’m using) does not get your environment variables correctly, and you should inform jest which file to look.

To resolve the issue, I installed dotenv-cli, which ables you to run applications on the cli using a dotenv file as base in case you have environment variables in your SUT.

These are the steps:

  1. npm i --save dotenv-cli.
  2. Open your VSCode user settings (Ctrl+Shift P / Command+Shift P) and type User Settings and hit enter, or File → Preferences → Settings.
  3. Go to the Workspace tab (this ensures this settings will be applied only for this project), search for “Jest Command”. You will find the following option, which, in your case, will be empty:
    image
  4. Fill it with npx dotenv-cli -e .env.local -- jest (replace .env.local with your actual dotenv file).
  5. Close settings and try to run your test suite again, it should work fine now.

Thank you, this works for me as well

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.