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

sails-test-helper

v0.5.0

Published

Test helper suite for Sails.js.

Downloads

2,721

Readme

sails-test-helper

Test helper suite for Sails.js using Mocha test framework.

Dependencies

Installation

# Local install
$ npm install sails-test-helper

# Global install
$ sudo npm install -g sails-test-helper

Initialization

Copy test suite template to current directory.

# Local install
$ node_modules/.bin/sails-test-helper init

# Global install
$ sails-test-helper init

The following will be copied to the current working directory:

Makefile
test/
  factories/
  fixtures/
  helpers/
  unit/

Writing Tests

Controllers

//-- test/unit/controllers/SampleController.test.js
require("sails-test-helper");

describe(TEST_NAME, function() {
  describe("GET index", function() {
    it("should be successful", function(done) {
      request.get("/sample")
        .expect(200)
        .end(done);
    });
  });
});

Execute SampleController test

$ make test

  controllers/SampleController
    GET index
      ✓ should be successful

  1 passing

Models

//-- test/unit/models/Sample.test.js
require("sails-test-helper");

describe(TEST_NAME, function() {
  describe(".create()", function() {
    it("should be successful", function(done) {
      Sample.create().exec(function(err, record) {
        expect(err).to.not.exist;
        expect(record).to.exist;
        done();
      });
    });
  });
});

Execute Sample test

$ make test

  models/Sample
    .create()
      ✓ should be successful 

  1 passing

Test Execution

Tests are executed using make command. Basically the script will look for tests to be executed in test/unit/ directory.

# Run all tests
$ make test

# Run tests under a specific directory
# This will run all tests under test/unit/controllers directory
$ make test controllers

# This will run tests under test/unit/controllers and test/unit/models directories
$ make test controllers models

# Run a specific test file
# This will run tests in test/unit/controllers/SampleController.test.js file
$ make test controllers/SampleController.test

Mocha Options

Mocha options can be passed as parameter to make. By default, mocha is being executed using the ff. options:

# recursive with 30 second timeout using spec reporter
$ mocha --recursive -t 30000 -R spec

Use MOCHA_OPTS commandline variable to pass specific mocha options to make.

# Dot format without colors. Useful for test execution on CI servers such as Jenkins. 
$ make MOCHA_OPTS='-C -R dot' test

Helpers

Custom Helpers

You can write your own test helpers or node modules and save it under test/helpers/ directory. Use the built-in requireHelper() function to load your custom helper.

//-- test/unit/services/SampleService.test.js
require("sails-test-helper");

describe(TEST_NAME, function() {
  it("should load my custom helper", function() {
    var my_helper = requireHelper("my_helper");
    expect(my_helper).to.exist;
  });
});

If you need to do some initialization prior to all your tests execution, you can put them inside test/helpers/bootstrap.js file. This file will be loaded automatically upon test execution.

//-- test/helpers/bootstrap.js
//-- global variables can also be initialized here...

before(function(done) {
  //-- anything to run or initialize before running all tests...
  
  done();
});

Factories

You can use the built-in factory module to define, build or create factories.

//-- test/unit/controllers/SampleController.test.js
require("sails-test-helper");

describe(TEST_NAME, function() {
  before(function(done) {
    //-- define a factory
    factory.define("sample")
      .attr("id", 0, {auto_increment: true});

    //-- create a record
    factory.create("sample", function(sample) {
      done();
    });
  });
  
  describe("GET index", function() {
    before(function(done) {
      Sample.count(function(err, count) {
        expect(err).to.not.exist;
        expect(count).to.be.greaterThan(0);
        done();
      });
    });
    it("should be successful", function(done) {
      request.get("/sample")
        .expect(200)
        .end(done);
    });
  });
});

You can also load your factory definitions from test/factories/ directory through your local bootstrap file.

//-- test/helpers/bootstrap.js
before(function(done) {
  factory.load();
  done();
});

Please see sails-factory for more details.