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();
}