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

seriate.mock

v0.0.1

Published

A mocking add-on for seriate

Downloads

3

Readme

#Seriate.mock

An addon library for Seriate that provides request mocking.

##API

The follow methods are added to the seriate instance:

|method name | description | |--------------------|--------------------------| |addMock(key, config) | Stores the mock in the cache to be retrieved later |addFileMock(path, config) | Stores a mock for use when sql.fromFile() is called |clearMock([key]) | Removes specific mock from cache if key is provided or clears all mocks if no key is provided. |getMock(key) | Retrieves mock from cache |resolveMock(stepName, stepConfig) | Retrieves the correct mock to be used for a particular SQL Context step.

###Examples

Seriate.mock provides a factory function that modifies a seriate object.

var sql = require( "seriate" );
var mock = require( "seriate.mock" );

mock( sql );

sql.addMock( "findUsers", [
	{ name: "Bob Ross", occupation: "painter" },
	{ name: "Tony Stark", occupation: "super hero" }
]);

sql.getPlainContext()
	.step( "findUsers" )
	.end( function( sets ) {
		sets.findUsers; // [{ name: "Bob Ross", occupation: "painter" },{ name: "Tony Stark", occupation: "super hero" }]
	});

The value passed to addMock can take several forms:

// ...

sql.addMock( "findRecords", [ 'val1', 'val2', 'val3' ] ); // Will return the array

sql.addMock( "getTotal", 123 ); // Will return the numeric value

// ...

sql.addMock( "findSomething", function( stepName, stepConfig ) {

	// stepName is the name of the current step being executed

	// stepConfig is the configuration object that was defined with the step

	return { title: "Happy Little Clouds", alias: stepConfig.params.alias };
});

sql.getPlainContext()
	.step( "findSomething", {
		params: {
			alias: "happy-little-clouds"
		}
	})
	.end( function( sets ) {
		sets.findSomething; // { title: "Happy Little Clouds", alias: "happy-little-clouds" }
	});

Warning: Giving addMock a plain object will not work the way you think it does.

When passing a plain object to addMock it must be in the form of a raw mock object including at least the mockResults key:

// ...

sql.addMock( "ninjaTurtles", {
	mockResults: [ 'Leonardo', 'Donatello', 'Raphael', 'Michelangelo' ], // This can be any data type including a function
	isError: false, // [Default false] If true, results will be passed to query error handler
	waitTime: 1000, // [Default 0] Time in milliseconds to wait for query to resolve
	once: false // [Default false] If true, mock will be discarded after one use
});

The API works the same way with Seriate's TransactionContext object:

sql.addMock( "dogs", [ "German Shepherds", "Labradors", "Dobermans" ] );

sql.getTransactionContext()
	.step( "dogs" )
	.end( function( sets ) {
		sets.dogs; // [ "German Shepherds", "Labradors", "Dobermans" ]
	});

Warning: Using this library will mock all transaction contexts. You will not be able to perform actual transactions at any point.

You can mock calls to execute or first by using the query (or preparedSql) as the key:

sql.addMock( "SELECT * FROM sys.books", [
	"A Tale of Two Cities",
	"The Jungle",
	"Heart of Darkness"
] );

sql.execute({
		query: "SELECT * FROM sys.books"
	})
	.end(function( results ) {
		results; // [ "A Tale of Two Cities", "The Jungle", "Heart of Darkness" ]
	});

sql.first({
		query: "SELECT * FROM sys.books"
	})
	.end(function( result ) {
		result; // "A Tale of Two Cities"
	});

You might consider using the once parameter single use mocks to avoid collisions without having to remember to clear the mock manually.

sql.addMock( "SELECT * FROM sys.books", [
	"A Tale of Two Cities",
	"The Jungle",
	"Heart of Darkness"
], { once: true } );

sql.execute({
		query: "SELECT * FROM sys.books"
	})
	.end(function( results ) {
		results; // [ "A Tale of Two Cities", "The Jungle", "Heart of Darkness" ]
	});

sql.first({
		query: "SELECT * FROM sys.books"
	})
	.end(function( result ) {
		result; // Mock no longer exists and will not be found
	});

####File Mocking

You can mock by file access by using the addFileMock method.

var expectedResults = [{ name: "Neo" }, { name: "Trinity" }, { name: "Morpheus" }];

sql.addFileMock( "./matrixCharacters", function( stepName, stepArg ) {
	return expectedResults;
} );

sql.getPlainContext()
	.step( "characters", {
		preparedSql: sql.fromFile( "./matrixCharacters" ),
	})
	.end( function( sets ) {
		sets.characters; // [{ name: "Neo" }, { name: "Trinity" }, { name: "Morpheus" }]
	} );

The file mocks will try to resolve to the same absolute path as the value given to sql.fromFile. Set the configuration option sqlFileBasePath to form the basis for your absolute paths built by addFileMock. For instance;

// Filename: /Users/me/myProject/src/lib/somefile.js

sql.getPlainContext()
	.step( "characters", {
		preparedSql: sql.fromFile( "./matrixCharacters" ),
	})
	.end( function( sets ) {
		sets.characters; // [{ name: "Neo" }, { name: "Trinity" }, { name: "Morpheus" }]
	} );

// File will resolve to /Users/me/myProject/src/lib/matrixCharacters

To mock this file from an external test, you would do something like this:

// Filename: /Users/me/myProject/spec/lib/somefile.spec.js

var sql = require( "seriate" );
var mock = require( "seriate.mock" );

mock( sql, { sqlFileBasePath: "/Users/me/myProject/src"} );

var expectedResults = [{ name: "Neo" }, { name: "Trinity" }, { name: "Morpheus" }];

sql.addFileMock( "lib/matrixCharacters", function( stepName, stepArg ) {
	return expectedResults;
} );

// Mock file will resolve to /Users/me/myProject/src/lib/matrixCharacters