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

atlas-cleanup-tests

v1.0.2

Published

Seamlessly wrap sync mocha tests with your cleanup script. Supports 'done' callback.

Downloads

4

Readme

atlas-cleanup-tests

Seamlessly wrap sync mocha tests with your cleanup script. Supports 'done' callback.

Travis


install

npm install --save-dev atlas-cleanup-tests

why

If you are mocking an API in a mocha test, you may need to revert changes after the test is done. The afterEach hook in mocha runs after mocha tries to output the results for the test. This means that if your test mocks an API that mocha depends on (e.g. process.stdout), mocha itself will fail.

If we try and mock console.log, mocha will break:

// Animal.test.js
const mocha = require("mocha");
const { expect } = require("chai");
const rewire = require("rewire")
const Animal = rewire("./Animal")

// a handle for reverting mocks
let revert;

describe("cat", function(){
  afterEach(function(){
    // this code doesn't get called soon enough
    revert()
  })
  it("should meow", function(){
    let sound;
    // this mocks 'console.log' for our Animal module
    revert = Animal.__set__("console.log", msg => {sound = msg})
    const cat = new Animal();
    cat.roar();
    expect(sound).to.equal("meow!")
  })
})

Fixing this isn't too difficult to do inside your tests, but I like to keep a clean house. Additionally, mocking standard output is pretty avoidable. By using a wrapper (e.g. a Logger) around console.log or process.stdout, you can easily mock your application's logging functionality. For simple stuff though, using a special logger feels uneccesary.

This package gives you a cleanup function which runs after your test runs, but before afterEach runs. This way, your home stays clean before mocha gets invited over.

examples

without done callback

// Animal.test.js
...
const Cleaner = require("atlas-cleanup-tests");
const cleanup = Cleaner(() => {
  revert()
  console.log("cleanup script done!")
})

// seamlessly wrap your tests
describe("cat", function(){
  it("should meow", cleanup(function(){
    let sound;
    revert = Animal.__set__("console.log", msg => {sound = msg})
    const cat = new Animal();
    cat.roar();
    expect(sound).to.equal("meow!")
  }))
})

with done callback

Even if your test is sync, mocha's done callback can be used to test (partial) code-paths. The cleanup function will also be able to wrap these tests without any work on your part:

// Animal.test.js
...
describe("cat", function(){
  it("should lose health when it roars", cleanup(function(done){
    let sound;
    revert = Animal.__set__("console.log", msg => {
      expect(msg).to.equal("meow!");
      // don't care about anything verifying meow
      done();
    })
    const cat = new Animal();
    cat.roar();
  }))
})

caveats

This wrapper assumes that you have mocked your async dependencies. It will not catch unhandled async errors (e.g. assertions made in an async callback). Unless you are unit testing your async functions or doing integration tests, it's probably better to mock them anyway.