Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

512
Views
jest.mock Cannot find module

I have a function which uses mysql connection to make a query and i am attempting to unit test this function, to do this i need to mock mysql. However when i use the jest.mock function to mock a mysql connection it returns the following error.

Cannot find module 'MySQLReplica' from 'src/api/leaderboard/ranks/popularity.test.ts'

popularity.test.ts

import { MySQLReplica } from "@services/mysql.service";
import { IEntityLeaderboardQuery } from "./../leaderboard.interface";
import { PopularityRank } from "./popularity";
import { mocked } from "ts-jest/utils";
jest.mock("MySQLReplica");

describe("Leaderboard - Rank - Popularity", () => {
    afterAll(() => {
        MySQLReplica.end();
    });

    const mockedMySQLReplica = mocked(MySQLReplica, true);
    it("should get popularity scores", async () => {
        const query: IEntityLeaderboardQuery = {
            top: "10",
            entity_types: ["person"],
            region: "Europe",
            country: "Germany",
            end_date: "2021-06-15",
            start_date: "2021-06-01",
            rank: "popularity",
        };

        const result = await PopularityRank(query);
        console.log(result);
    });
});

services/mysql.service.ts

import mysql from "mysql2/promise";
import { Config } from "@config";

export const MySQLReplica = mysql.createPool({
    host: Config.db.replica.host,
    database: Config.db.replica.database,
    user: Config.db.replica.username,
    password: Config.db.replica.password,
    waitForConnections: true,
    connectionLimit: 500,
    queueLimit: 0,
});

export const MySQLWrite = mysql.createPool({
    host: Config.db.write.host,
    database: Config.db.write.database,
    user: Config.db.write.username,
    password: Config.db.write.password,
    waitForConnections: true,
    connectionLimit: 100,
    queueLimit: 0,
});

export const MySQLPreparedStatement = mysql.format;
over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!