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

@sportheroes/bk-mocha-suite

v1.16.3

Published

Suite for your mocha tests

Downloads

82

Readme

Mocha Suit - the way to improve your tests.

It's a simple wrapper for your Mocha tests to make them a bit more OOP like.


Motivation

The main rule of any test you create is TEST should be simple as much as posible. Unfortunately JavaScript which is my the most loved programing language is so elegant and simple that sometimes it's become a problem when you create something more or less complicated.

To be honest all we know JS is object oriented language just partially. This is the reason why it's not so easy to create test framework on JavaScript so cool as for example jUnit in Java. In most cases it's ok to follow algorithmic approach with all these describes/before/it/after when you create some simple stuff. But basically it does not encourage you to reuse your code. And at some point you start to share your variables and methods between your describes in common closure or (ohh no!) in global object. Then you create your own wrappers upon test modules, move helpers from neighboring modules and so on. And finally you reinvent the wheel and create your own test generation script.

So I did. Almost did

Installation

# from NPM
$ npm install --save-dev mocha-suit

# last version from GIT
$ git clone https://github.com/muonjs/mocha-suit.git
$ cd ../mocha-suit
$ sudo npm link .
$ cd ~/PathToYourSuperProject
$ npm link mocha-suit

Description

Mocha Suit is the wrapper upon the Mocha test framework. It helps you to incapsulate your tests within the class object (named Suit) with it's own setup (before and beforeEach), teardown (after and afterEach) methods and testcases (it) themselves. Literally Suit is a describe block of Mocha with it's own testcases.

var MochaSuit = require("mocha-suit");
var Suit = MochaSuit("Your first test suit");
Suit.before(function() { ... });
Suit.it("test it!",function() { ... });
Suit.after(function() { ... });
new Suit();
/* ----------- will generate ----------- */
describe("Your first test suit",function() {
    before(function() { ... });
    it("test it!",function() { ... });
    after(function() { ... });
});

Suit could be extended to new sub suit, that means you place another describe block inside of top one.

var MochaSuit = require("mocha-suit");
var TopSuit = MochaSuit("Top test suit");
TopSuit.before(function() { ... });

var Suit = TopSuit.extend("Some specific suit");
Suit.before(function() { ... });
Suit.it("test it!",function() { ... });
Suit.after(function() { ... });
new Suit();
/* ----------- will generate ----------- */
describe("Top test suit",function() {
    before(function() { ... });
    describe("Some specific suit",function() {
        before(function() { ... });
        it("test it!",function() { ... });
        after(function() { ... });
    })
});

Mocha Suit is complient to Mocha. So this is always Mocha's this object which is accessible inside of all Mocha's setup and test methods when you run them in usual manner. Also you can use done argument to call methods asyncronously (or return Promise object).

Suit.before(function(done) {
  this.timeout(3000);
  setTimeout(done,2000);
});

Suit.before(function() {
  return Promise.delay(2000);
});

/* ----------- will generate ----------- */
before(function(done) {
  this.timeout(3000);
  setTimeout(done,2000);
});

before(function() {
  return Promise.delay(2000);
});

Is that all? Nope. Welcome to the wiki for the rest cool stuff.

Usage

Mocha Suit depends on Mocha. So you need to initialize Mocha before run your tests. Simplest way is to put your tests in /test/ directory and then run mocha from console. Since Suit is just a wrapper you can utilize any mocha options and arguments you want. For more info visit Mocha's documentation.

Jasmine

Are you Jasmine lover and Mocha hater? Superficial difference between Mocha and Jasmine is setup/teardown methods naming. Specifically before is known as beforeAll and after is known as afterAll. Mocha Suit suports both versions. Since Mocha Suit is barely wrapper it has no matter what you do inside of your test code or witch helper library (for ex. spies, assertions or matchers) you use. So next one code should be ok:

var MochaSuit = require("mocha-suit");
var Suit = MochaSuit("Test suit");
Suit.before(function() { ... }); // won't have effect in jasmine
Suit.beforeAll(function() { ... }); // won't have effect in mocha
Suit.it("test it!",function() { ... });
Suit.after(function() { ... }); // won't have effect in jasmine
Suit.afterAll(function() { ... }); // won't have effect in mocha
new Suit();

Every time test is generated Suit checks whether method exists in global object or not. If beforeAll is absent it won't run. And vice versa.

You can ask me: "Why is library name Mocha Suit? Why not Jasmine Suit or merely Test Suit?". The answer is I don't know Jasmine so well as Mocha. So I suppose some pitfalls may exist. I'll appreciate any help to make Suit to be more universal.

Test && Contribution

To run tests:

$ npm test

Since Mocha is dev dependency of Mocha Suit this call envokes mocha under ./test/tests directory with options passed to ./test/mocha.opts. Alternative way to do the same thing is:

$ mocha -R spec # or whatever reporter you like

For Jasmine's fans there is ./spec/support/jasmine.conf tuning Jasmine to run the same tests inside of alternative environment.

$ jasmine

Of course all pull requests are welcomed.

Documentation && Translation

Since English isn't my native language I'll appreciate any pull requests with REAME corrections as well as request with translations to any other languages.

License

This project is distributed under MIT license. 2016.