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

mock-mocha

v1.0.0

Published

To mock mocha in your test when you test your mocha tests

Downloads

3

Readme

mock-mocha

Build Status dependency Status devDependency Status Coverage Status

To mock mocha in your test when you test your mocha tests with Mocha or Jasmine*.

* in next minor version.

Usage

'use strict';

const mocks = require('mock-mocha');
const expect = require('chai').expect;

describe("testing my mocha tests", () => {
    beforeEach(() => {
        // 1. save the original test framework functions
        mocks.saveMochaGlobals();

        // 2. set mock functions to global
        mocks.setGlobals();

        // 3. clear existing data
        mocks.clear();

        // 4. require your module/file, you want to test
        // Important to pass root (__dirname) too!
        mocks.require(__dirname, '../my-test-script.js');
    });

    // 5. test your tests
    it("should make root suite", () => {
        expect(mocks.describes.length).to.equal(1);
    });

    // ... more tests

    afterEach(() => {
        // 6. restore your original test framework functions
        mocks.restoreMochaGlobals();
    });
});

For tips about How to test you tests you could check how-to.spec.js.

API

Properties

  • describes

    The registered test suites, in order of the execution of test suites.

    Type: Array.<Describe>

  • its

    The registered tests, in order of the execution of tests.

    Type: Array.<It>

  • hooks

    The registered hooks, in order of the registration of hooks.

    Type: Array.<Before|BeforeEach|AfterEach|After>

  • delayed

    Indicates whether Mocha's run has been used.

    Type: Boolean

  • storedMochaGlobals

    All previously stored original Mocha global methods, like describe, it, etc.

    Type: Object.<String, Function>

  • STATUS

    Possible statuses of Describe, It and Hook types.

    Values: OK, SKIP, ONLY

    Type: Object.<String, Number>

Methods

  • clear()

    Sets all status properties (describes, its, hooks and delayed) to its default value.

  • require(root, pathOfFile)

    Requires the given file (based on the given root, usually __dirname) with require, but without its default caching.

    Params:

    • root {String} - the root of the current file, usually __dirname
    • pathOfFile {String} - the relative path to the file needs to be tested, relative to the curent test spec file

    Retuns: {*}

  • saveMochaGlobals([globalScope])

    Saves the global methods of Mocha to storedMochaGlobals from GLOBAL or the given object and returns those.

    Params:

    • globalScope {Object} - the global scope, from the methods needs to be saved. Optional, by default it's NodeJS's GLOBAL`

    Returns: {Object.<String, Function>}

  • restoreMochaGlobals([globalScope], [stored])

    Restores the previously saved (or given) Mocha global methods to GLOBAL or to the given object and returns the new scope.

    Params:

    • globalScope {Object} - the global scope, to the methods needs to be restored. Optional, by default it's NodeJS's GLOBAL
    • stored {Object} - the object, where the original methods are stored. Optional, by default it's storedMochaGlobals

    Returns: {Object}

  • setGlobals([globalScope])

    Decorates GLOBAL or the given object with the mock Mocha global methods.

    Params:

    • globalScope {Object} - the global scope, which needs to be decorated with mocks. Optional, by default it's NodeJS's GLOBAL

    Returns: {Object}

Types

  • Describe - test suite

    Properties:

    • description {String}
    • test {Function}
    • status {STATUS|Number}

    Methods:

    • execute() : void - executes test, which is a sync function
  • It - test

    Properties:

    • description {String}
    • test {Function}
    • status {STATUS|Number}

    Methods:

    • execute() : Promise - executes test, which could be both sync and async
  • Before, BeforeEach, AfterEach, After - hooks

    Properties:

    • description {String}
    • test {Function}
    • status {STATUS|Number}

    Methods:

    • execute() : Promise - executes test, which could be both sync and async

Note: In case of It and all hooks, during execution the executed test/hook can call this.skip() as it could be done in Mocha. In this case type of the given It/hooks will change to SKIP.

Mock Mocha methods

The mocked mocha methods are:

| Mock method | Action when called | |:------------------------------------|:-------------------------------------------------------| | describe(description, suite) | Adds new Describe to describes with OK status. | | describe.skip(description, suite) | Adds new Describe to describes with SKIP status. | | describe.only(description, suite) | Adds new Describe to describes with ONLY status. | | it(description, test) | Adds new It to its with OK status. | | it.skip(description, test) | Adds new It to its with SKIP status. | | it.only(description, test) | Adds new It to its with ONLY status. | | before([description,] hook) | Adds new Before to hooks wth OK status. | | beforeEach([description,] hook) | Adds new BeforeEach to hooks with OK status. | | afterEach([description,] hook) | Adds new AfterEach to hooks with OK status. | | after([description,] hook) | Adds new After to hooks with OK status. | | run() | Sets delayed to true. |