0

I have a TypeScript file that looks like this:

import * as sqlite from expo-sqlite
class User {
 static async getListOfUsers() {
}
}

This "User" class is the class to be tested, and the sqlite module is what I'm trying to mock.

As instructed in the Jest documentation on mocking functions, I have a mock class defined in a mocks directory adjacent to node_modules (in (project root directory)/mocks/sq

My jest.config.js file looks like this:

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  transformIgnorePatterns: ["node_modules/(?!(react-native|react-native-cookies)/)"]
};

Curiously, I encountered the same error before using ts-jest. After configuring Jest to use ts-jest, I got errors related to my code, that I could fix, so I thought this error went away.

EDIT: As someone requested, here's the mock file. The error is on the first line of this file.

const sqlite = jest.createMockFromModule("expo-sqlite"); // The error occurs here.
const userQuery = "SELECT * FROM ZUSERENTITY";
const placeQuery = "SELECT * FROM ZPLACEENTITY WHERE ZUSER=";
export class MockResultSet
{
    private length: number;
    private items: {}[];
    constructor (items: {}[])
    {
        this.items = items;
        this.length = items.length;
    }
}
export class MockTransaction {
    executeSql(sql:string): MockResultSet
    {
        if (sql === userQuery)
        {
            return new MockResultSet([
                {"ZNAME":"Montana","Z_PK":0,"ZNEXTLEVEL":0},
                {"ZNAME":"Monty","Z_PK":1,"ZNEXTLEVEL":1}
            ]);
        }
        else if (sql === placeQuery)
        {
            return new MockResultSet([
                {"ZUSER":"0","ZWORDFAMILYNUM":0,"ZACTIVITYNUM":0},
                {"ZUSER":"1","ZWORDFAMILYNUM":1,"ZACTIVITYNUM":6}
            ]);
        }
        return new MockResultSet([]);
    }
}
export type MockSQLError = {code: number, message:string,CONSTRAINT_ERR:number,DATABASE_ERR:number,QUOTA_ERR:number,SYNTAX_ERR:number,TIMEOUT_ERR:number,TOO_LARGE_ERR:number,UNKNOWN_ERR:number,VERSION_ERR:number};
export type MockSQLTransactionCallback = (transaction: MockTransaction)=>void;
export type MockSQLTransactionErrorCallback = (error: MockSQLError)=>void;
export class MockDatabase
{
    transaction(callback: MockSQLTransactionCallback)
    {
        callback(new MockTransaction());
    }
}
export function openDatabase(dbname:string)
{
    return new MockDatabase();
}
  • 2
    Showing the code of your mock class and details on where exactly (which file/line) the error happens would probably help a lot. Generally providing a [mcve] is a good idea. – SergGr Dec 14 '21 at 03:18
  • @SergGr It is done. –  Dec 15 '21 at 21:25
  • Have you tried to import the module before calling the jest.createMockFromModule function? – Max Dec 16 '21 at 07:36
  • @Max: No, was I supposed to? I guess the documentation wasn't clear on that. –  Dec 20 '21 at 15:03
  • Possibly duplicated https://stackoverflow.com/q/40465047/11853111, It's worth to visit accepted answer. – Jiho Lee Dec 20 '21 at 16:22

1 Answers1

0

Have you try to assert your dependency as jest.Mock, and then use the jest mock function like mockReturnValueOnce

import * as sqlite from "expo-sqlite"
jest.mock("expo-sqlite")

describe("Your test", function () {

   beforeEach(function () {
      (sqlite.YOUR_FUNC_TO_MOCK as jest.Mock).mockReturnValueOnce(<YOUR_MOCKED_RETURN>)
   })
})

Using mockReturnValueOnce instead of mocking the implementation is a good practice because its change will not affect any other test.

MatteoPHRE
  • 358
  • 4
  • 10