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

@batterii/fake-query

v2.0.1

Published

Fake Objection query builder for unit tests

Downloads

1,051

Readme

@batterii/fake-query

This module exposes a fake Objection query builder for unit tests. It is built using Sinon and is intended to be used in conjunction with it.

The FakeQuery Class

A named export of this module, the FakeQuery exposes a fake QueryBuilder instance on its builder property. You can inject the fake into your code under test by stubbing the static ::query method on the desired model.

The fake builder automatically creates sinon stubs for any property accessed on the builder, except for the #then and #catch methods used to execute the query and obtain its result, as well as the #inspect method which prints out a string representation of the builder.

Created stubs always return this, as all QueryBuilder methods are chainable. Test code can examine the stubs property to write assertions about the query. Typically, you will want to do a keys assertion on the stubs object, followed by sinon assertions on the stubs themselves.

By default, the fake builder will neither resolve or reject when executed, as is normal for sinon stubs. If you want it to resolve or reject, simply involve the #resolves or #rejects methods with the desired result value.

Once the fake builder has been executed, it can no longer be changed. If any of its instance methods are invoked, or if you attempt to change its result with #resolves or #rejects, the invoked method will throw. This ensures that your assertions are always referring to the state of the builder when it was executed, and not after.

Example (Using TypeScript, Mocha, and Chai)

import {FakeQuery} from "@batterii/fake-query";
import {MyModel} from "../path/to/my-model";
import chai from "chai";
import sinon from "sinon";
import sinonChai from "sinon-chai";

chai.use(sinonChai);
const {expect} = chai;

describe('functionUnderTest', function() {
	let qry: FakeQuery;

	beforeEach(function() {
		qry = new FakeQuery();

		// Make sure this stub is cleaned up! See the `afterEach` below.
		sinon.stub(MyModel, "query").returns(qry.builder);
	});

	afterEach(function() {
		sinon.restore();
	});

	it("deletes the things", async function() {
		const deletedThings = [];
		qry.resolves(deletedThings);

		const result = await functionUnderTest();

		expect(MyModel.query).to.be.calledOnce;
		expect(MyModel.query).to.be.calledOn(MyModel);
		expect(MyModel.query).to.be.calledWithExactly();
		expect(qry.stubs).to.have.keys(["delete", "where", "returning"]);
		expect(qry.stubs.delete).to.be.calledOnce;
		expect(qry.stubs.delete).to.be.calledWithExactly();
		expect(qry.stubs.where).to.be.calledOnce;
		expect(qry.stubs.where).to.be.calledWith("id", ">", 42);
		expect(qry.stubs.returning).to.be.calledOnce;
		expect(qry.stubs.returning).to.be.calledWith("*");
		expect(result).to.equal(deletedThings);
	});
});

// Any non-cosmetic changes to this function will cause the above test to fail.
async function functionUnderTest(): Promise<MyModel[]> {
	return MyModel.query()
		.delete()
		.where('id', '>', 42)
		.returning('*');
}