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

strict-mocha-describers

v2.1.2

Published

The implementation of describers strongly oriented to the methods that will be tested

Downloads

20

Readme

strict-mocha-describers

Build Status codecov Maintainability Packages npm version

This project implements over mocha strictly describers for creation of suite tests of classes.

How it works

When these describers are used to write your test, all other methods from the class other than the method that will be tested are overwritten by a default error with the message not mocked yet. The idea is to enforce that all these calls must be mocked and to make the test always break when some refactoring is made or a new method is called.

How to use it

The sintax is almost the same as you would use with mocha, but we have a special describe method for classes called describeClass which will be the start for any suite case created with this package. Look the following example

import { describeClass } from 'strict-mocha-descriers';

function bootStrap() {
    return new HelloWorldService();
}

describeClass(HelloWorldService, bootstrap, describeMethod => {
    describeMethod('helloWorld', it => {
        it('should print hello world', target => {
            sinon.stub(console, 'log');

            const result = target.helloWorld();

            expect(console.log).to.have.been.calledOnceWithExactly('hello world');
            expect(result).to.be.undefined;
        });
    });
});

You can also control the access for target and mocked services using variables declared in your source code:

import { describeClass } from 'strict-mocha-descriers';

let target: HelloWorldService;
let service: InjectedService;
function bootStrap() {
    service = {} as InjectedService;
    return target = new HelloWorldService(service);
}

describeClass(HelloWorldService, bootstrap, describeMethod => {
    describeMethod('helloWorld', it => {
        it('should print hello world', () => {
            service.calledMethod = sinon.stub().returns('some value');
            sinon.stub(console, 'log');

            const result = target.helloWorld();

            expect(service.calledMethod).to.have.been.calledOnceWithExactly('some input');
            expect(console.log).to.have.been.calledOnceWithExactly('hello world some value');
            expect(result).to.be.undefined;
        });
    });
});

First, rather than inform a description of the test to describeClass, we passed the class that we want to test. Also, as a second parameter, we passed a function that will be used to instantiate the target instance for each test. Finally, the callback to write each method suite case. Look that this callback receives a function as a parameter: describeMethod.

Now, look that describeMethod is called to create a suite case to wrap all case tests of one method. It receives the name of said method and it callback receives a parameter called target, which will be the instance of HelloWorldService, ready to be tested only for the helloWorld method.

If hello world method calls any other method of HelloWorldService, it must be mocked, otherwise an error will occur

You can also create tests for static method using this describers:

    describeMethod.static('staticHelloWorld', it => {
        it('should print hello world', () => {
            sinon.stub(console, 'log');

            const result = HelloWorldService.staticHelloWorld();

            expect(console.log).to.have.been.calledOnceWithExactly('hello world');
            expect(result).to.be.undefined;
        });

Just notice that there is no target parameter in this example, as there is no need for an instance in this test

Finally, you can also start a suite case for a class using the function describeStaticClass. The only difference is that the parameter passed to the callback function is already the static function tester. A test for static methods with this, therefore, will look like this:

import { describeStaticClass } from 'strict-mocha-descriers';

describeStaticClass(HelloWorldService, describeStaticMethod => {
    describeStaticMethod('helloWorld', it => {
        it('should print hello world', () => {
            sinon.stub(console, 'log');

            const result = HelloWorldService.helloWorld();

            expect(console.log).to.have.been.calledOnceWithExactly('hello world');
            expect(result).to.be.undefined;
        });
    });
});