Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

async-test-util

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-test-util

Util-functions that are be useful in async tests

  • 1.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6K
increased by184.82%
Maintainers
1
Weekly downloads
 
Created
Source

async-test-util

Utility-functions that can be usefull when you have asynchronous tests in javascript.

Installation

$ npm install async-test-util --save-dev
// es6
import AsyncTestUtil from 'async-test-util';

// es5
var AsyncTestUtil = require('async-test-util');

wait()

Waits until the given time has expired and then resolves.

it('should wait', async() => {
    await AsyncTestUtil.wait(200);
    console.log('200 ms is over');
});

waitResolveable()

Waits until the given timeout has expired or the resolve was triggered manually

it('should wait until observable fired or time is over', async() => {
    const resolveable = AsyncTestUtil.waitResolveable(200);
    myObservable.subscribe(sth => {
        console.log('got first event');
        resolveable.resolve();
    });
    await resolveable.promise;
});

waitUntil()

Waits until the given predicate-function returns true. Throws if the optional timeout has passed before.

it('should wait until server is online', async() => {
    const checkServer = async() => {
        try{
            await fetch('http://example.com/api/');
            return true;
        }catch(){
            return false;
        }
    };
    await AsyncTestUtil.waitUntil(checkServer);
});

With timeout:

it('should wait until server is online (maxtime: 1000ms)', async() => {
    const checkServer = async() => {
        try{
            await fetch('http://example.com/api/');
            return true;
        }catch(){
            return false;
        }
    };
    await AsyncTestUtil.waitUntil(checkServer, 1000);
});

waitForever()

Waits forever, never resolves.

it('should never resolve', async() => {
    let resolved = false;
    AsyncTestUtil
        .waitForever()
        .then(() => resolved = true);
    await AsyncTestUtil.wait(100);
    assert.equal(false, resolved);
});

runForever()

Runs the given predicate-function forever. Between each run, the interval-time is awaited.

it('should run forever', async() => {
    let t = 0;
    const pred = () => t++;
    AsyncTestUtil.runForever(
        pred, // predicate-function
        10    // interval
     );

    await AsyncTestUtil.wait(100);
    assert.ok(t > 4);
    const lastT = t;
    await AsyncTestUtil.wait(100);
    assert.ok(t > lastT);
});

assertThrows()

Async-Form of assert.throws. Asserts that the given function throws with the defined error, throws if not.

// with error-type
it('should throw because route does not exist', async() => {
    const getServerVersion = async() => {
        const response = await fetch('http://example.com/foobar/');
        return response;
    };
    await AsyncTestUtil.assertThrows(
        () => getServerVersion(),       // function that throws (required)
        Error                   // Error-type           (optional)
    );
});

// with error-text-flag
it('should throw because route does not exist', async() => {
    const pingServer = async() => {
        try{
            await fetch('http://example.com/foobar/');            
        }catch(err){
            throw new Error('route not reachable');
        }
    };
    await AsyncTestUtil.assertThrows(
        () => pingServer(),       // function that throws                                    (required)
        Error,                    // Error-type                                              (optional)
        'reachable'               // text-flag, throw if error-message does not include this (optional)  
    );
});

randomString()

Creates a random string. Takes length as first parameter an custom charset as second.


console.log(AsyncTestUtil.randomString());
// > 'rhfkx'

console.log(AsyncTestUtil.randomString(10));
// > 'dhcvkledzu'

console.log(AsyncTestUtil.randomString(
    6,    // (optional) length
    'abc' // (optional) charset, only this will be used to create the string
));
// > 'acbcba'

randomNumber()

Creates a random number. Optional range can be given.


console.log(AsyncTestUtil.randomNumber());
// > 56

console.log(AsyncTestUtil.randomNumber(
    1000, // min-value (default=0)
    2000 // max-value (default=1000)
));
// > 1768

clone()

Reference to clone. Does exactly the same thing.

it('should not modify the original object', () => {
    const original = {
        foo: 'bar',
        level: 1
    };
    const cloned = AsyncTestUtil.clone(original);
    cloned.level = 2;
    assert.equal(original.level, 1);
});

deepEqual()

Reference to deep-equal. Does exactly the same thing.

it('the 2 objects should be equal', () => {
    const obj1 = {
        foo: 'bar',
        level: 1
    };
    const obj2 = {
        foo: 'bar',
        level: 1
    };
    assert.ok(AsyncTestUtil.deepEqual(obj1, obj2));
});

FAQs

Package last updated on 31 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc