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

jasmine-test-helpers

v1.2.3

Published

Public and free software for helping test NodeJS projects using Jasmine or Jasmine-Node.

Downloads

76

Readme

Jasmine Test Helpers

Helping you test your Node.js applications, one helper at a time.

npm version Build Status Dependencies Dev Dependencies codecov.io

About

This contains a series of Jasmine helpers made to facilitate testing certain functionality more easily. These help to detect when a test fails but should have passed, turn middleware into a promise and makes it easier to use promises in tests. More explanation is below for each helper.

How to Use With Jasmine Versions 2 And Up

Include the package in your package.json file.

npm install --save-dev jasmine-test-helpers

Then you need to be able to include the helper in your testing directory or where you'd like to run tests.

You can do this by creating a helpers object in your spec/support/jasmine.json file, or adding to an existing helpers object.

If you don't already have a helpers object in spec/support/jasmine.json, add this:

"helpers": [
    "../node_modules/jasmine-test-helpers/lib/**/*.js"
]

If you already have a helpers object, just add "../node_modules/jasmine-test-helpers/lib/" to it.

How to Use With Jasmine Versions Below 2

Include the package in your package.json file.

npm install --save-dev jasmine-test-helpers

Then you need to be able to include the helper in your testing directory or where you'd like to run tests.

You can do this by adding a path to the module in the run scripts in your package.json. For example, if your package.json looks something like this:

{
    "scripts": {
        "start": "node bin/myCoolApp",
        "test": "jasmine-node spec/"
    }
}

You would change that to:

{
    "scripts": {
        "start": "node bin/myCoolApp",
        "test": "jasmine-node node_modules/jasmine-test-helpers/lib/ spec/"
    }
}

jasmine.fail([actual], [expected])

This is most useful when testing a promise is getting the reject as expected, and when you'd like the test to fail if the promise is resolved rather than rejected. Parameters can be passed as well if they make sense for the test, but don't regularly need to be used as this will cause a failure of the test if called.

describe("a suite", () => {
    it("rejects the returned promise", () => {
        return someClass.someMethodAsync().then(jasmine.fail, () => {
            expect(someVar).toBe("this");
        });
    });
});

jasmine.middlewareToPromise(middlewareFunction)

Used when wanting to make a piece of middleware used in setting up a node server and you want to make it to be run synchronously with your tests to get a response back straight away without waiting for a real promise to resolve.

The middleware can be written in a traditional style using a callback like done or next.

var middleware, middlewareAsync, req, res;

middleware = require("../lib/your-favorite-middleware");
middlewareAsync = jasmine.middlewareToPromise(middleware);

beforeEach(() => {
    req = makeFakeRequestObject();  // You write this
    res = makeFakeResponseObject();  // You write this
});
describe("a suite", () => {
    it("is easier with promises", () => {
        // If an argument is passed, the promise is rejected.
        // If the middleware throws, the promise is rejected.
        // The promise helper automatically handles both.
        // Just remember to `return` the promise.
        return middlewareAsync(req, res).then(() => {
            expect(res.send).toHaveBeenCalled();
        });
    });
    it("works in a traditional way", (done) => {
        // You must manually ensure the middleware does not throw.
        // Also you must manually ensure no argument was passed to
        // the `done` / `next` callback.
        // Don't forget to call `done()` when everything's complete.
        function doneWithMiddleware(arg) {
            expect(arg).not.toBeDefined();
            expect(res.send).toHaveBeenCalled();
            done();
        }
        jasmine.expect(() => {
            middleware(req, res, doneWithMiddleware);
        }).not.toThrow();
    });
});

Promises Helper

This makes it much easier to use promises in node tests. Instead of having to remember to pass done in the it part of the testing in Jasmine, you simple need to return the promise and the rest is handled like normal.

// Before
describe("a suite", () => {
    it("does what the test needs to do", (done) => {
        someClass.someMethodAsync().then((result) => {
            expect(result).toBe("this");
        }).then(done, done);
    });
});

// After
describe("a suite", () => {
    it("does what the test needs to do", () => {
        return someClass.someMethodAsync().then((result) => {
            expect(result).toBe("this");
        });
    });
});

See how much simpler that is? You won't need to add done to the it or accidentally put done in the describe and wonder why your tests are failing when you've been writing tests all day.