Skip to content

Test failed on my machine #1125

@crud-engr

Description

@crud-engr

I'm unable to run integration test on my machine, which runs well on other machines.

Here is the error displayed.
ReferenceError: File is not defined

Tools used with their versions:

  • @testcontainers/mongodb: 11.5.1
  • Mongoose: 7.4.1
  • Docker: 28.1.1
  • Jest: 30.0.4
  • Node: 20.18.1

Here is the integrationSetup.ts for the test config

import { afterAll, afterEach, beforeAll, jest } from "@jest/globals";
import {
  MongoDBContainer,
  type StartedMongoDBContainer,
} from "@testcontainers/mongodb";
import mongoose from "mongoose";
import logger from "../../src/utils/logger";

// Mock external services BEFORE any imports that might use them
jest.mock("twilio", () => {
  return jest.fn().mockImplementation(() => ({
    messages: {
      create: jest.fn(),
    },
  }));
});

// Mock Redis - for now.
jest.mock("ioredis", () => {
  return jest.fn().mockImplementation(() => ({
    get: jest.fn(),
    set: jest.fn(),
    del: jest.fn(),
    exists: jest.fn(),
    expire: jest.fn(),
    flushall: jest.fn(),
    quit: jest.fn(),
    on: jest.fn(),
    connect: jest.fn(),
    disconnect: jest.fn(),
  }));
});

// Mock external services globally
jest.mock("src/utils/email");
jest.mock("src/utils/sms");
jest.mock("src/utils/stripe");
jest.mock("web-push");

// Mock Agenda to prevent job execution during tests
jest.mock("src/plugins/agenda.plugin", () => ({
  default: {
    define: jest.fn(),
    start: jest.fn(),
    stop: jest.fn(),
    cancel: jest.fn(),
    schedule: jest.fn(),
    now: jest.fn(),
  },
}));

let mongoContainer: StartedMongoDBContainer | undefined;

beforeAll(async () => {
  mongoContainer = await new MongoDBContainer("mongo:8.0.12").start();

  const mongoUri = mongoContainer.getConnectionString();
  process.env.MONGODB_URI = mongoUri;

  try {
    await mongoose.connect(mongoUri, {
      directConnection: true,
    });
  } catch (error) {
    logger.error("Failed to setup test environment:", error);
    if (mongoContainer) {
      await mongoContainer.stop();
    }
    throw error;
  }
}, 60000);

afterAll(async () => {
  try {
    if (mongoose.connection.readyState === 1) {
      await mongoose.disconnect();
    }
  } catch (error) {
    logger.error("Error disconnecting mongoose:", error);
  }

  try {
    if (mongoContainer) {
      await mongoContainer.stop();
    }
  } catch (error) {
    logger.error("Error stopping mongo container:", error);
  }
});

afterEach(async () => {
  // Only clean DB if connection exists and is ready
  if (mongoose.connection.readyState === 1 && mongoose.connection.db) {
    try {
      const collections = await mongoose.connection.db.collections();
      for (const collection of collections) {
        await collection.deleteMany({});
      }
    } catch (error) {
      logger.error("Error cleaning database:", error);
    }
  }
});

What exactly am I missing?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions