chai-duration

1.1.0 • Public • Published

Description

Assertions on durations for testing performances



Installation

npm install chai-duration -D


Usage

JavaScript / Typescript

const chai = require("chai");

const chaiDuration = require("chai-duration");
chai.use(chaiDuration);


Overview

Check test cases

By default, all duration are in milliseconds
it('should assume this test takes longer than 1 second', async function ()
{
    await sleep(2000);
    expect(this).to.take.longer.than.duration("1sec");
});

it('should assume this test takes no longer than 1 second', async function ()
{
    await sleep(500);
    expect("test").to.take.no.longer.than.duration("1sec");
});

Compare two durations

By default, all duration are in milliseconds
expect(100).to.be.shorter.than.duration(500);
expect(100).to.have.shorter.duration(500);

expect(100).to.have.same.duration(100);

expect(100).to.be.longer.than.duration(500);
expect(1000).to.have.longer.duration(500);

Compare two durations with or without units

expect("10s").to.be.shorter.than.duration("1mn");
expect("100s").to.be.longer.than.duration("1mn");
expect("1h20min10s").to.be.longer.than.duration("1mn");
expect(60 * 60 * 1000).to.have.same.duration("1 hour");

Compare time differences

By default, all duration are in milliseconds
const {sleep} = require("@thimpat/libutils");

it('should assume time differences longer than 1 second', async function ()
{
    // Start time
    const startDate = new Date();
    
    // ... Replace with the method/function you want to measure
    await sleep(2000);

    // End time
    const endDate = new Date();
    
    // Use time difference using an array
    expect([endDate, startDate]).to.have.time.difference.longer.than.duration(10);
});
it('should assume the time difference between 1s and 5s to equal 4s', async function ()
{
    expect(["1s", "5s"]).to.have.time.difference.same.as.duration("4s");
});

Readme

Keywords

none

Package Sidebar

Install

npm i chai-duration

Weekly Downloads

1

Version

1.1.0

License

MIT

Unpacked Size

11.6 kB

Total Files

7

Last publish

Collaborators

  • thimpat