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

keystonejs-stub

v0.2.3

Published

Keystonejs-stub is a stubbing system for keystonejs to be used with unit testing frameworks like Jasmine.

Downloads

22

Readme

keystonejs-stub

Keystonejs-stub is a stubbing system for keystonejs to be used with unit testing frameworks like Jasmine. Using this stubbing system you can test any module that requires keystone. Although Jasmine is used to demonstrate how the stubbing system works it should be testing-framework agnostic. The setup of the testing framework itself is outside the scope of this project.

Usage

Install it:

    npm install keystonejs-stub --save-dev

Require it in your test spec:

    stubKeystone = require('keystonejs-stub'),

Use it--the following example uses proxyquire:

    SampleModule = proxyquire('./SampleModule', {
        'keystone': stubKeystone
    });

For more usage examples please checkout the samples in the "tests" folder. You may need to run "npm install" in the keystonejs-stub directory to install dev dependencies. You can execute the sample tests by running "npm test" under the keystonejs-stub directory.

Notes

  • The intent of this stub system is not for doing integration or end-to-end testing, which can be accomplished in a slightly different manner. The stub system is specifically for unit testing. That assumes that you have adhered as much as possible to the Single Responsibility Principle when designing/implementing your application modules. The stub system heavily relies on mocking stubbed methods that return results, which affect subsequent logic in the module.

  • If you are testing List Models you need to export the list model:

      var keystone = require('keystone');
    
      var SampleListModel = new keystone.List('SampleListModel', {});
    
      SampleListModel.add({name: { type: String } });
    
      SampleListModel.register();
    
      module.exports = SampleListModel;
  • Regarding mocking and assuming you have the following chained mongoose call:

      SomeList.model.findOne({'slug': 'xxx'}).sort('-yyy').populate('zzz').exec(function (err, item) {
          ...
      });

    Then you can mock that chain in various ways:

    • You can assume keystonejs-stub's default mocking for .sort() and .populate() and just mock the .exec() as follows:

        spyOn(SomeList.model,'exec').and.callFake(function(callback){
            callback && callback(null, <result listing data>);
        });
    • You can mock either .sort() or .populate() along with .exec() as follows:

        spyOn(stubKeystone.lists['SampleListModel'].model,'find').and.returnValue({
            sort: function (arg) {
                <do-whatever-sort>
                return {
                    exec: function (callback) {
                        <do-whatever-before-callback>
                        callback && callback(null, <result listing data>);
                    }
                }
            }
        });
    • The previous mock can also be done as independent mocks as follows:

        spyOn(stubKeystone.lists['SampleListModel'].model,'sort').and.callFake(function(arg){
            <do-whatever-sort>
            return this;
        });
      
        spyOn(stubKeystone.lists['SampleListModel'].model,'exec').and.callFake(function(callback){
            <do-whatever-before-callback>
            callback && callback(null, <result listing data>);
        });
    • You can mock all .sort(), .populate(), and .exec() as follows:

        spyOn(stubKeystone.lists['SampleListModel'].model,'find').and.returnValue({
            sort: function (arg) {
                <do-whatever-sort>
                return {
                    populate: function (arg) {
                        <do-whatever-populate>
                        return {
                            exec: function (callback) {
                                <do-whatever-before-callback>
                                callback && callback(null, <result listing data>);
                            }
                        }
                    }
                }
            }
        });
    • or you can break the above as independent mocks as previously described

  • When mocking models you can just assign them directly to the lists object in the keystone stub:

      stubKeystone.lists['SampleListModel'] = proxyquire('./SampleListModel', {
          'keystone': stubKeystone
      });

    NOTE: However, if the model is the SUT then you don't need to assign it to lists!

  • To test list virtuals, methods, and statics you must first set a document item in the list:

      SampleListModel.setDoc(doc);

If you need to enable logging on keystonejs-stub then set (windows) or export (linux) any of the following debug tags:

keystone
list
model
schema

For example:

export DEBUG=keystone,list,schema,model && npm test

 

This is work-in-progress and based on current needs. Feel free to send improvements!

License

MIT. Copyright (c) 2015 Carlos Colon (webteckie)