How to use mock-knex - 10 common examples

To help you get started, we’ve selected a few mock-knex examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sequiturs / bookshelf-advanced-serialization / test / index.js View on Github external
regularComment.load('author').then(function() {
          var regularResult = regularComment.toJSON();
          expect(regularResult).to.eql({
            id: '8dc1464d-8c32-448d-a81d-ff161077d781',
            author_id: '3fe94198-7b32-44ee-abdd-04104b902c51',
            author: {},
            content: 'Hello, World!'
          });

          tracker.uninstall();

          tracker = mockKnex.getTracker();
          tracker.install();
          configureTracker(tracker);

          Comment.forge({ id: '8dc1464d-8c32-448d-a81d-ff161077d781' }, {
            accessor: { user: { id: stubs.users.elephant1.id } }
          })
          .fetch()
          .then(function(comment) {
            comment.load('author').then(function() {
              var author = comment.related('author'); // Need to call this because
              // .related() is what transfers model._accessor to the relation

              comment.toJSON().then(function(result) {
                expect(result).to.eql({
                  id: '8dc1464d-8c32-448d-a81d-ff161077d781',
                  author: null,
github openupm / openupm / test / app-models-base.js View on Github external
/* eslint-disable no-unused-vars */
/* eslint-disable no-undef */
const assert = require("assert");
const should = require("should");
const rewire = require("rewire");
const mockKnex = require("mock-knex");
const tracker = mockKnex.getTracker();

const { ModelBase, registerModel } = require("../app/models/base");

class TempModel extends ModelBase {}

registerModel(TempModel, {
  hasTimestamps: true
});

describe("app/models/base.js", function() {
  beforeEach(function(done) {
    tracker.install();
    done();
  });

  afterEach(function(done) {
github sequiturs / bookshelf-advanced-serialization / test / index.js View on Github external
};
          var expectedJsonWithoutPivot = {
            id: 1,
            name: 'bert',
            classesTeacherOf: [{
              id: 2,
              name: 'history'
            }]
          };

          // Establish regular, unplugged-in Bookshelf behavior

          regularBookshelf.model('Teacher', regularBookshelf.Model.extend(teachersModelDefinition, {}));
          regularBookshelf.model('Class', regularBookshelf.Model.extend(classesModelDefinition, {}));

          var tracker = mockKnex.getTracker();
          tracker.install();
          tracker.on('query', mock);

          regularBookshelf.model('Teacher').forge({ name: 'bert' })
          .fetch()
          .then(function(model) {
            return model.load('classesTeacherOf').then(function() {

              var regularJson = model.toJSON();
              expect(regularJson).to.eql(expectedJsonWithPivot);

              var regularJsonOmitPivot = model.toJSON({ omitPivot: true });
              expect(regularJsonOmitPivot).to.eql(expectedJsonWithoutPivot);

              tracker.uninstall();
            });
github openupm / openupm / app / db / postgres.js View on Github external
const config = require("config");
const mockKnex = require("mock-knex");

let Knex = require("knex");
let knexStringcase = require("knex-stringcase");

let knex = Knex(knexStringcase(config.knex));

if (process.env.NODE_ENV === "test") {
  mockKnex.mock(knex);
}

// Update updatedAt field to datetime now.
knex.touchupdatedAt = function(record) {
  if (typeof record === "undefined" || record === null) record = {};
  record.updatedAt = knex.fn.now();
  return record;
};

module.exports = knex;
github sequiturs / bookshelf-advanced-serialization / test / index.js View on Github external
describe('fetchAll', function() {
    var tracker = mockKnex.getTracker();
    before(function() {
      tracker.install();
      tracker.on('query', function sendResult(query) {
        query.response([
          stubs.users.elephant1,
          stubs.users.antelope99
        ]);
      });
    });
    after(function() {
      tracker.uninstall();
    });
    it('should set _accessor on all models in a collection fetched via fetchAll', function(done) {
      var user = User.forge({}, { accessor: { user: 'foo' }});
      user.fetchAll().then(function(collection) {
        expect(collection.length).to.equal(2);
github sequiturs / bookshelf-advanced-serialization / test / index.js View on Github external
'use strict';

var expect = require('expect.js');
var BluebirdPromise = require('bluebird');
var _ = require('lodash');

var mockKnex = require('mock-knex');
var knex = require('knex')({});
mockKnex.mock(knex);

var plugin = require('../lib/index.js');
var SanityError = require('../lib/errors.js').BookshelfAdvancedSerializationPluginSanityError;

var stubs = require('./_stubs.js');

describe('Plugin', function() {
  describe('options', function() {
    it('should accept not passing an options argument', function() {
      expect(function() {
        plugin();
      }).to.not.throwException();
    });
    it('should accept an options argument that is an object', function() {
      expect(function() {
        plugin({});
github sequiturs / bookshelf-advanced-serialization / test / index.js View on Github external
var configureTracker = function(tracker) {
        tracker.on('query', function sendResult(query, step) {
          [
            function() {
              query.response([
                stubs.comments[0]
              ]);
            },
            function() {
              query.response([]);
            }
          ][step - 1]();
        });
      };

      var tracker = mockKnex.getTracker();
      tracker.install();
      configureTracker(tracker);

      RegularComment.forge({ id: '8dc1464d-8c32-448d-a81d-ff161077d781' }, {
        accessor: { user: { id: stubs.users.elephant1.id } }
      })
      .fetch()
      .then(function(regularComment) {
        regularComment.load('author').then(function() {
          var regularResult = regularComment.toJSON();
          expect(regularResult).to.eql({
            id: '8dc1464d-8c32-448d-a81d-ff161077d781',
            author_id: '3fe94198-7b32-44ee-abdd-04104b902c51',
            author: {},
            content: 'Hello, World!'
          });
github weyoss / graphql-bookshelfjs / test / run / hooks.js View on Github external
'use strict';

const config = {
    "client": "mysql",
    debug: false
};

const knex = require('knex');
const mockKnex = require('mock-knex');
const tracker = mockKnex.getTracker();

const connection = knex(config);
mockKnex.mock(connection);

const bookshelf = require('bookshelf')(connection);
const models = require('./../common/models')(bookshelf);
const graphQLSchema = require('./../common/schema')(models);

const sinon = require('sinon');

before(function(done) {
    this.sandbox = sinon.sandbox.create();
    this.sandbox.graphQLSchema = graphQLSchema;
    this.sandbox.tracker = tracker;
    done();
});
github DefinitelyTyped / DefinitelyTyped / types / mock-knex / mock-knex-tests.ts View on Github external
import knex = require("knex");
import * as mockDb from "mock-knex";

const db = knex({
	client: 'sqlite'
});

mockDb.mock(db);

const tracker = mockDb.getTracker();
tracker.install();
tracker.on('query', (query, step) => {
	if (query.method === "first" || step === 1) {
		query.response([{
			a: 1
		}, {
			a: 2
		}, {
			a: 3
		}], {
			stream: false
		});
	} else {
		query.reject(new Error("bad query"));
	}
});
github weyoss / graphql-bookshelfjs / test / run / hooks.js View on Github external
'use strict';

const config = {
    "client": "mysql",
    debug: false
};

const knex = require('knex');
const mockKnex = require('mock-knex');
const tracker = mockKnex.getTracker();

const connection = knex(config);
mockKnex.mock(connection);

const bookshelf = require('bookshelf')(connection);
const models = require('./../common/models')(bookshelf);
const graphQLSchema = require('./../common/schema')(models);

const sinon = require('sinon');

before(function(done) {
    this.sandbox = sinon.sandbox.create();
    this.sandbox.graphQLSchema = graphQLSchema;
    this.sandbox.tracker = tracker;
    done();
});

beforeEach(function (done) {
    this.sandbox.restore();

mock-knex

a knex mock adapter for simulating a db during testing

MIT
Latest version published 4 months ago

Package Health Score

70 / 100
Full package analysis