//insertRecord.ts
import {knexInstance} from './logDatabaseConfig';
const insertLog = async(quan: Number) => {
try {
await knexInstance('LOG')
.insert({sync_quantity: quan, sync_at: new Date()});
} catch (error) {
throw new Error('Error insert' + error);
}
}
export default insertLog;
import { expect } from '@jest/globals';
import knex, { Knex } from 'knex';
import { Tracker, getTracker, MockClient } from 'knex-mock-client';
import { faker } from '@faker-js/faker';
import insertLog from './insertLog';
jest.mock('./insertLog', () => {
return {
db: knex({ client: MockClient })
};
});
describe('log insert tests', () => {
let tracker: Tracker;
beforeAll(() => {
tracker = getTracker();
});
afterEach(() => {
tracker.reset();
});
it('should add new log record', async () => {
tracker.on.insert('LOG');
const syncQuantity = 1;
await insertLog(1);
const insertHistory = tracker.history.insert;
expect(insertHistory).toHaveLength(1);
expect(insertHistory[0].method).toEqual('insert');
});
});
Question about knex-mock-client
My dependencies "@faker-js/faker": "^7.2.0", "@types/jest": "^28.1.1", "concurrently": "^7.2.1", "jest": "^28.1.1", "knex-mock-client": "^1.8.4", "nodemon": "^2.0.16", "ts-jest": "^28.0.4"
TypeError: (0 , insertLog_1.default) is not a function and highlighting - await insertLog(1);
Anyone got the same issue?