• Stars
    star
    467
  • Rank 93,935 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 12 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Your friendly dependency injector for testing Require.js modules.

Squire.js

Squire.js is a dependency injector for Require.js users to make mocking dependencies easy!

Squire.js - The Squire

Installation

Bower

bower install squire

NPM

npm install squirejs

API

constructor

First you have to load Squire in, just like any other Require.js module. Beyond this example the rest of the documentation will assume you already loaded Squire as a module dependency.

Default Configuration

define(['Squire'], function(Squire) {
  var injector = new Squire();
});

Different Context

var injector = new Squire('other-requirejs-context');

require(Array dependencies, Function callback, Function errback)

var injector = new Squire();
injector.require(['utilities/Calculator'], function(Calculator) {
  // Calculator has been loaded.
},
function(err) {
  // Calculator threw an error loading.
});

mock(String name | Object(name: mock), Object mock)

The mock method lets you pass in mocks to be used for a given modules dependency. The first argument is the module name, the second argument is the mock itself. For multiple mocks you can pass an object, the objects key will be the path and the corresponding value will be used as the mock.

var injector = new Squire();

// Key value mocking.
injector.mock(dependencyName, mockForDependency);

// You can pass an object literal as well.
injector.mock(dependencyNameAndMock);
var injector = new Squire();
injector.mock('CrazyCalculatorDependency', {
    sin: 10
  })
  .require(['utilities/Calculator'], function(Calculator) {
    // The Calculators dependency 'CrazyCalculatorDependency' has been mocked to
    // use the object literal { sin: 10 } that we passed in.
  });

store(String name | Array names)

The store method allows you to get a pointer back to a dependency so you can stub it.

var injector = new Squire();
injector
  .store('CrazyCalculatorDependency')
  .require(['utilities/Calculator', 'mocks'], function(Calculator, mocks) {
    // mocks.store.CrazyCalculatorDependency is the Calculators dependency, you can
    // manipulate it or stub it with Sinon now.
  });

clean(Optional (String name | Array names))

The clean method allows you to remove mocks by name from your Squire instance, or remove all mocks.

var injector = new Squire();
injector.mock('something', { other: 'mock'});

// You do stuff but want to be able to get the real 'something' now.
injector.clean('something');

// Or a collection of mocks
injector.clean(['something', 'something/else']);

Or clean out all the mocks stored in a Squire instance.

injector.clean();

remove()

Remove all the dependencies loaded by this instance of Squire.

injector.remove();

run()

Run generates a function that will receive a done callback and execute it after your test function is complete. Particularly useful for frameworks where asynchrony is handled with a callback. Here is an example with Mocha.js. Jasmine can offer this callback approach using Jasmin.Async.

it('should execute this test using run', injector.run(['mocks/Shirt'], function(Shirt) {
  Shirt.color.should.equal('Red');
}));

Utilities

Squire.js offers a few helper functions to ease pains associated with mocking and testing AMD modules.

Squire.Helpers.returns(Any what)

Create a mock that returns mockViewInstance

injector.mock('Views/AwesomeView', Squire.Helpers.returns(mockViewInstance));

Squire.Helpers.constructs(Any what)

Often times AMD modules return constructor functions which means that mocking such a class would end up having to create a function that returns a function that returns your mocked instance. Squire.js eases that pain by wrapping up your instance for you.

injector.mock('Views/AwesomeView', Squire.Helpers.constructs(mockViewInstance));

Now any module that uses Views/AwesomeView as a constructor dependency will use get your mock instead:

// when invoked with in an Squire.injector.require call
var awesome = new AwesomeView(); // awesome now gets your mockViewInstance

Credits

Illustration by Adam Christiansen

More Repositories

1

grunt-parallel

Speed up your build by running commands and tasks in parallel!
JavaScript
170
star
2

react-sourcerer

A React based code pattern matching library.
JavaScript
126
star
3

pixelator.js

Pixelator.js for all your pixelation perfection pixeling needs.
JavaScript
52
star
4

the-frankenstein-framework

The polyglot future of the web.
JavaScript
32
star
5

how-to-lazy-load-react-webpack

JavaScript
28
star
6

dot-grunt

My dot grunt files.
JavaScript
23
star
7

require-sweet

An AMD loader plugin for Sweet.js
JavaScript
21
star
8

unvalidated-redirects-example

Repository showcasing unvalidated redirects exploit when using target="_blank".
HTML
21
star
9

CSSComponents

What if presentational components were authored in a CSS based DSL?
JavaScript
21
star
10

json-lisp

https://merrickchristensen.com/articles/json-lisp/
TypeScript
19
star
11

StoreHouse

A memory defensive low level library that provides a key-value cache.
JavaScript
15
star
12

require-ts

An AMD loader plugin for TypeScript
JavaScript
15
star
13

StyleManager

Load your StyleSheets as AMD dependencies and use JavaScript to manage a style element.
JavaScript
15
star
14

grunt-angular-seed

The angular seed with Grunt.js for tooling.
JavaScript
14
star
15

CSS.js

CSS in pure JavaScript
JavaScript
11
star
16

grunt-configure

A powerful configuration utility for Grunt.js
JavaScript
11
star
17

Rich-Client-Music-Player

A quick demonstration of Backbone, Handlebars, and RequireJS for a presentation.
JavaScript
11
star
18

rjs-build-analysis

Analyse your r.js build output for potential mistakes.
JavaScript
8
star
19

mc-ascii-art

An ASCII art HTML tag.
Perl
8
star
20

Kohana-Log-Email

A log writer that will email you important logs.
PHP
7
star
21

virality

A CLI to check the virality of a URL.
JavaScript
7
star
22

Lunch

An example of how to build big client applications with CoffeeScript & Backbone.js.
CoffeeScript
6
star
23

ng-react-router

Angular 2's Dependency Injector + React + React Router
JavaScript
6
star
24

grunt-compliment

Get a compliment whenever you need! Treat yo' self!
JavaScript
5
star
25

Elapsed.js

How much time has past since...?
JavaScript
5
star
26

grunt-connect

A Grunt plugin to run a server indefinitely.
JavaScript
5
star
27

JavaScript-Library

A library of awesome projects.
4
star
28

KoGen

Basic code generator for Kohana.
PHP
4
star
29

Github

JavaScript
4
star
30

Testing-AMD

This is not a library that demonstrates testing AMD. Well, it does but its designed to show the current pain points of testing AMD so we can create better solutions moving forward.
JavaScript
4
star
31

utahjs.com

Utah JavaScript User Group Website
JavaScript
3
star
32

di

Dependency Injection Playground
JavaScript
3
star
33

Sleepy

A Restful Module For Kohana
PHP
3
star
34

node-introduction

An introduction to HTTP & Node.js
JavaScript
3
star
35

require-js-slides

Based on the talk given at Utah JS. AMD, Require.js and Other Wins.
2
star
36

react-environment

JavaScript
2
star
37

PHP-MySQL-Wrapper

One for raw PHP one for CodeIgniter.
2
star
38

DevMountain-AJAX

HTML
2
star
39

requests

Requests for blog articles, podcasts or dare I say... conferences?
2
star
40

Learning-From-Underscore

Implementing underscore one method at a time for academic purposes.
JavaScript
1
star
41

observable.js

A simple on, off & trigger implementation.
JavaScript
1
star
42

Konbon

A konbon board written in JavaScript.
JavaScript
1
star
43

Beautiful-Words

1
star
44

Kohana-Braintree

A Kohana Wrapper Braintree
PHP
1
star
45

WWKD

A repository of all the sh** Kenneth says
PHP
1
star
46

hellojs

Hello World!
PHP
1
star
47

panelist

JavaScript
1
star
48

settings

Vim Script
1
star
49

requirejs-plugin-integration-testing

An example repository for an article.
JavaScript
1
star
50

hacks-for-snacks

CSS
1
star
51

compwnent

Compile your assets to AMD modules.
JavaScript
1
star
52

decide

CoffeeScript
1
star
53

Assets

An asset compiler for less HTTP requests and easier asset management.
JavaScript
1
star
54

nodepp

This is a library which attempts to bring the asynchronous event-loop lambda-based execution style of node to C++.
C++
1
star
55

Commune.js

Simplify communication between a cross-domain iFrame with promises.
JavaScript
1
star
56

require-js-example

An example demonstration showcasing require.js features.
JavaScript
1
star
57

Git-Kohana-BoilerPlate

A basic repository with a few submodules for Kohana and a shared application architecture.
PHP
1
star