npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

knex-mock-client

v3.0.2

Published

A mock client for knex which allows you to write tests with DB interactions

Downloads

77,728

Readme

knex-mock-client

npm CI

A mock client for Knex which allows you to write unit tests with DB interactions with a breeze.

Installation

To use this lib, first you will have to install it:

npm i --save-dev knex-mock-client

or

yarn add --dev knex-mock-client

Example

Mocking an insert statement

// my-cool-controller.ts
import { db } from '../common/db-setup';

export async function addUser(user: User): Promise<{ id }> {
  const [insertId] = await db.insert(user).into('users');

  return { id: insertId };
}
// my-cool-controller.spec.ts
import { expect } from '@jest/globals';
import { createTracker, MockClient } from 'knex-mock-client';
import { faker } from '@faker-js/faker';
import { db } from '../common/db-setup';

jest.mock('../common/db-setup', () => {
   const knex = require('knex');
   return {
      db: knex({ client: MockClient }),
   };
});

describe('my-cool-controller tests', () => {
   let tracker: Tracker;

   beforeAll(() => {
      tracker = createTracker(db);
   });

   afterEach(() => {
      tracker.reset();
   });

   it('should add new user', async () => {
      const insertId = faker.number.int();
      tracker.on.insert('users').response([insertId]);

      const newUser = { name: 'foo bar', email: '[email protected]' };
      const data = await addUser(newUser);

      expect(data.id).toEqual(insertId);

      const insertHistory = tracker.history.insert;

      expect(insertHistory).toHaveLength(1);
      expect(insertHistory[0].method).toEqual('insert');
      expect(insertHistory[0].bindings).toEqual([newUser.name, newUser.email]);
   });
});

Each one of on methods (select, insert,update, delete, any) are accepting a query matcher. There are 3 kind of matchers:

  1. String - will match part of the given sql using String.includes

    tracker.on.select('select * from users where `id`=?').response([]);
  2. RegEx - will match the given sql with the given regex

    tracker.on.update(/update users where .*/).response([]);
  3. Function - you can specify a custom matcher by providing a function. This function will accept RawQuery as an argument and should return a boolean value.

    tracker.on
      .insert(
        ({ method, sql, bindings }: RawQuery) =>
          method === 'insert' && /^insert into `users`/.test(sql) && bindings.includes('secret-token')
      )
      .response([]);

Query handlers

You can specify the db response by calling:

  1. response<T = any>(data: T | ((rawQuery: RawQuery) => (T | Promise<T>))) - This will register a permanent query handler.

    If a value is provided, it will be returned directly. If a callback is passed, it will be called with the RawQuery and should return a value for the tracker to return.

    tracker.on.select('select * from users where `id`=?').response([{ id: 1, name: 'foo' }]);
    tracker.on
      .select('select * from users where `id`=?')
      .response((rawQuery) => [{ id: 1, name: 'foo' }]);
  2. responseOnce<T = any>(data: T | ((rawQuery: RawQuery) => (T | Promise<T>)))- This will register a one-time query handler, which will be removed from handlers list after the first usage.

    If a value is provided, it will be returned directly. If a callback is passed, it will be called with the RawQuery and should return a value for the tracker to return.

    tracker.on.select('select * from users where `id`=?').responseOnce([{ id: 1, name: 'foo' }]);
    tracker.on
      .select('select * from users where `id`=?')
      .responseOnce((rawQuery) => Promise.resolve([{ id: 1, name: 'foo' }]));
  3. simulateError(errorMessage: string) - will register a permanent failure handler for the matched query

    tracker.on.select('select * from users where `id`=?').simulateError('Connection lost');
  4. simulateErrorOnce(errorMessage: string) - will register a one-time failure handler, after the first usage it will be removed from handlers list.

    tracker.on.select('select * from users where `id`=?').simulateErrorOnce('Connection lost');

You can reset handlers by calling tracker.resetHandlers() which will remove all handlers for all query methods.

History Calls

Each db request that your app makes throughout knex will be registered in a scoped (by query method) history call list. It will also be registered in tracker.history.all.

Each call is an object with the interface of RawQuery.

interface RawQuery {
  method: 'select' | 'insert' | 'update' | 'delete';
  sql: string;
  bindings: any[];
  options: Record<string, any>;
  timeout: boolean;
  cancelOnTimeout: boolean;
  __knexQueryUid: string;
  queryContext: any;
}

Specific dialects

Some DB's (like postgress) has specific dialects, for the mockClient build the proper query you must pass the dialect property.


db = knex({
  client: MockClient,
  dialect: 'pg', // can be any Knex valid dialect name.
});

const givenData = [{ id: faker.number.int() }];
tracker.on.select('table_name').response(givenData);

const data = await db('table_name').distinctOn('age');

You can reset all history calls by calling tracker.resetHistory().

You can reset queryHandlers & history by calling tracker.reset().

This lib got inspiration from axios-mock-adapter api️.