• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

457
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;
about 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error