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

section-tests

v3.2.1

Published

TDD for node.js

Downloads

15,772

Readme

Section Tests - TDD for node

A beautiful, extensible and lightweight async test framework.

Attention, this module works since version 2 only with node 10+ and the --experimental-modules flag set!

Usage

1. Install the module

 npm i --save-dev section-tests

2. Add test command to your package.json file

"scripts": {
    "test": "node ./node_modules/.bin/section ./test/**/*.js"
},

You may use glob patterns for defining the files that should be loaded for executing the tests.

3. Create your test files

The section test framework generates structured messages from your test which then are processed by an output reporter. That reporter needs to be instantiated before the first test is executed.

Example:

import section, {SpecReporter} from 'section-tests';


// this must only be done in the first file
// that is executed for testing
section.use(new SpecReporter());


// lets do some preparations before we execute
// the actual tests
section.setup(async () => {

    await doSetupThings();

    // print status
    section.info('Things are set up!');
});


// now lets execute some tests
section('Outer Group', (section) => {
    section('Inner Group', (section) => {

        section.test('Test a', async() => {
            const result = await doSomething();
            asser(result);
        });

        section.test('Test a', async() => {
            const result = await doAnotherThing();
            asser(result);

            // print a neat log message
            section.success(`Got result ${result}`);
        });
    });
});

The resulting output looks like this:

API

Creating a test group

Tests must be organized in groups. In order to create a group the section can be invoked

const section = import Section from 'section-tests';

// create a new group
section('group', (section) => {

    // one can create unlimited nested groups by 
    // using the section passed to the callback
    // to the section function
    section('nested group', (section) => {

    });
});

Creating a test

Tests must be part of a test group

const section = import Section from 'section-tests';

// create a new group
section('group', (section) => {

    // the section variable passed above to this
    // function can be used to execute tests
    section.test('test name', async() => {

        // run your tests in here, make sure this
        // function is async or returns a promise 
    });
});

Defining timeouts

The deafult timeout for tests is 2 seconds

global timeout

The global timeout can be altered by calling the setTimeout method on the global section object

const section = import Section from 'section-tests';

// increases the global timeout to 3 seconds
section.setTimeout(3000);

individual test timeout

The indivdual timeout for each test can be set on the section object the test is executed on

const section = import Section from 'section-tests';

// create a new group
section('group', (section) => {

    // define test
    section.test('test name', async() => {

        // increases timeout to 10 seconds for this 
        // test only
        section.setTimeout(10000);
    });
});

Enabling a repoter

Currently only there is only one reporter, the spec reporter. It writes the output of the tests to the console.

The reporter msut be set in the first file that is executed for all tests.

import section, {SpecReporter} from 'section-tests';

// add the spec reported
section.use(new SpecReporter());

Writing logs

Tests may output additional log messages. They are displayed inline of the test ourput.

const section = import Section from 'section-tests';

// create a new group
section('group', (section) => {

    // define test
    section.test('test name', async() => {

        // log something
        section.info('important message!');
    });
});

The available log methods are:

  • notice
  • debug
  • warn
  • error
  • success