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

@putout/plugin-tape

v14.2.0

Published

๐ŸŠPutout plugin helps with tests

Downloads

70,254

Readme

@putout/plugin-tape NPM version

Tape-inspired TAP-compatible simplest high speed test runner with superpowers.

(c) ๐Ÿ“ผSupertape

๐ŸŠPutout plugin helps to apply best parctises for tests written with ๐Ÿ“ผSupertape.

Install

npm i @putout/plugin-tape -D

Rules

{
    "rules": {
        "tape/convert-mock-require-to-mock-import": "off",
        "tape/jest": "on",
        "tape/apply-stub": "on",
        "tape/apply-destructuring": "on",
        "tape/apply-with-name": "on",
        "tape/add-t-end": "on",
        "tape/add-stop-all": "on",
        "tape/add-await-to-re-import": "on",
        "tape/remove-useless-t-end": "on",
        "tape/sync-with-name": "on",
        "tape/switch-expected-with-result": "on",
        "tape/convert-tape-to-supertape": "on",
        "tape/convert-throws-to-try-catch": "on",
        "tape/convert-does-not-throw-to-try-catch": "on",
        "tape/convert-called-with-args": "on",
        "tape/convert-called-with-to-called-with-no-args": "on",
        "tape/convert-called-with-no-args-to-called-with": "on",
        "tape/convert-equal-to-called-once": "on",
        "tape/convert-equal-to-deep-equal": "on",
        "tape/convert-equals-to-equal": "on",
        "tape/convert-deep-equal-to-equal": "on",
        "tape/convert-emitter-to-promise": "on",
        "tape/convert-ok-to-match": "on",
        "tape/convert-ok-to-called-with": "on",
        "tape/convert-match-regexp-to-string": "on",
        "tape/add-args": "on",
        "tape/declare": "on",
        "tape/remove-default-messages": "on",
        "tape/remove-useless-not-called-args": "on",
        "tape/remove-only": "on",
        "tape/remove-skip": "on",
        "tape/remove-stop-all": "on"
    }
}

jest

๐ŸŠPutout gives ability to switch easily from Jest to ๐Ÿ“ผSupertape. Checkout in ๐ŸŠPutout Editor.

โŒ Example of incorrect code

it('should equal', () => {
    expect(a).toEqual(b);
});

โœ… Example of correct code

import {test} from 'supertape';

test('should equal', () => {
    t.equal(a, b);
    t.end();
});

switch-expected-with-result

๐Ÿ“ผSupertape uses more natural way of comparing: first you pass result and then expected.

It gives you ability to use value instead of expected and understand code faster: no need to search for a second argument. While result is always a variable, so it most likely much shorter.

โŒ Example of incorrect code

test('plugin-apply-destructuring: transform: array: destructuring', (t) => {
    t.equal(expected, result);
    t.end();
});

โœ… Example of correct code

test('plugin-apply-destructuring: transform: array: destructuring', (t) => {
    t.equal(result, expected);
    t.end();
});

convert-tape-to-supertape

โŒ Example of incorrect code

const test = require('tape');

โœ… Example of correct code

const test = require('supertape');

convert-throws-to-try-catch

โŒ Example of incorrect code

const test = require('supertape');

test('some message', (t) => {
    t.throws(copymitter, /from should be a string!/, 'should throw when no args');
    t.end();
});

โœ… Example of correct code

const tryCatch = require('try-catch');
const test = require('supertape');

test('some message', (t) => {
    const [error] = tryCatch(copymitter);
    
    t.equal(error.message, 'from shoulde be a string!', 'should throw when no args');
    t.end();
});

convert-does-not-throw-to-try-catch

โŒ Example of incorrect code

const test = require('supertape');

test('some message', (t) => {
    t.doesNotThrow(copymitter, 'should throw when no args');
    t.end();
});

โœ… Example of correct code

const test = require('supertape');
const tryCatch = require('try-catch');

test('some test', (t) => {
    const [error] = tryCatch(copymitter);
    
    t.notOk(error, 'should not throw when no args');
    t.end();
});

convert-called-with-args

โŒ Example of incorrect code

const test = require('supertape');
const {stub} = test;

test('some message', (t) => {
    const fn = stub();
    fn();
    
    t.calledWith(fn, 'hello');
    t.end();
});

โœ… Example of correct code

const test = require('supertape');
const {stub} = test;

test('some message', (t) => {
    const fn = stub();
    fn();
    
    t.calledWith(fn, ['hello']);
    t.end();
});

convert-equal-to-called-once

No need to use equal, supertape supports calledOnce.

โŒ Example of incorrect code

const test = require('supertape');
const {stub} = test;

test('some message', (t) => {
    const fn = stub();
    fn();
    
    t.equal(fn.callCount, 1);
    t.end();
});

โœ… Example of correct code

const test = require('supertape');
const {stub} = test;

test('some message', (t) => {
    const fn = stub();
    fn();
    
    t.calledOnce(fn);
    t.end();
});

convert-deep-equal-to-equal

Use equal when comparing with primitives, deepEqual for Objects and Arrays;

โŒ Example of incorrect code

const test = require('supertape');
const {stub} = test;

test('some message', (t) => {
    t.deepEqual(x, 5);
    t.end();
});

โœ… Example of correct code

const test = require('supertape');
const {stub} = test;

test('some message', (t) => {
    t.equal(x, 5);
    t.end();
});

convert-called-with-to-called-with-no-args

โŒ Example of incorrect code

const test = require('supertape');
const {stub} = test;

test('some message', (t) => {
    const fn = stub();
    fn();
    
    t.calledWith(fn);
    t.end();
});

โœ… Example of correct code

const test = require('supertape');
const {stub} = test;

test('some message', (t) => {
    const fn = stub();
    fn();
    
    t.calledWithNoArgs(fn);
    t.end();
});

convert-called-with-no-args-to-called-with

โŒ Example of incorrect code

const test = require('supertape');
const {stub} = test;

test('some message', (t) => {
    const fn = stub();
    fn();
    
    t.calledWithNoArgs(fn, [1, 2]);
    t.end();
});

โœ… Example of correct code

const test = require('supertape');
const {stub} = test;

test('some message', (t) => {
    const fn = stub();
    fn();
    
    t.calledWith(fn, [1, 2]);
    t.end();
});

convert-emitter-to-promise

โŒ Example of incorrect code

test('copymitter', (t) => {
    const cp = copymitter(from, to, ['1']);
    
    cp.on('end', (t) => {
        t.end();
    });
});

โœ… Example of correct code

const {once} = require('events');

test('copymitter', async (t) => {
    const cp = copymitter(from, to, ['1']);
    
    await once(cp, 'end');
    
    t.end();
});

apply-destructuring

Check out in ๐ŸŠPutout Editor.

โŒ Example of incorrect code

const test = require('supertape');
const {stub} = test;

โœ… Example of correct code

const {test, stub} = require('supertape');

apply-stub

Apply stub functions created. Look how it works in ๐ŸŠPutout Editor.

โŒ Example of incorrect code

const a = async () => true;
const b = async () => {};
const c = async () => throwError('hello');

const d = async () => {
    throw Error('hello');
};

โœ… Example of correct code

const a = stub().resolves(true);
const b = stub().resolves();
const c = stub().rejects(Error('hello'));
const d = stub().rejects(Error('hello'));

apply-with-name

โŒ Example of incorrect code

test('should call init before show', (t) => {
    const init = stub();
    const show = stub();
    
    t.calledInOrder([init, show]);
    t.end();
});

โœ… Example of correct code

test('should call init before show', (t) => {
    const init = stub().withName('init');
    const show = stub().withName('show');
    
    t.calledInOrder([init, show]);
    t.end();
});

sync-with-name

โŒ Example of incorrect code

test('should call init before show', (t) => {
    const init = stub().withName('show');
    const show = stub().withName('show');
    
    t.calledInOrder([init, show]);
    t.end();
});

โœ… Example of correct code

test('should call init before show', (t) => {
    const init = stub().withName('init');
    const show = stub().withName('show');
    
    t.calledInOrder([init, show]);
    t.end();
});

declare

mockImport

โŒ Example of incorrect code

import {stub} from 'supertape';

mockImport('fs/promises', {
    readFile: stub().resolves(''),
});
โœ… Example of correct code
import {stub} from 'supertape';
import {createMockImport} from 'mock-import';

const {
    mockImport,
    stopAll,
    reImport,
} = createMockImport(import.meta.url);

mockImport('fs/promises', {
    readFile: stub().resolves(''),
});

test

โŒ Example of incorrect code

test('xxx', (t) => {
    const a = stub();
    t.end();
});

โœ… Example of correct code

import {test, stub} from 'supertape';

test('xxx', (t) => {
    const a = stub();
    t.end();
});

add-args

โŒ Example of incorrect code

test('xxx', () => {
    t.end();
});

โœ… Example of correct code

test('xxx', (t) => {
    t.end();
});

add-t-end

โŒ Example of incorrect code

test('xxx', () => {});

โœ… Example of correct code

test('xxx', (t) => {
    t.end();
});

add-await-to-re-import

โŒ Example of incorrect code

test('stop-all: should be called', (t) => {
    const read = reImport('./read');
    t.end();
});

โœ… Example of correct code

test('stop-all: should be called', async (t) => {
    const read = await reImport('./read');
    t.end();
});

add-stop-all

When you write test mocking ESM with mockImport() never forget to call stopAll() when you no longer need it. This leads to bugs in tests which are hard to find, each test should be checked with the one which pass when called alone but fail when called with others.

โŒ Example of incorrect code

test('stop-all: should be called', (t) => {
    mockImport('fs/promises', {
        readFile: stub(),
    });
    
    t.end();
});

โœ… Example of correct code

test('stop-all: should be called', (t) => {
    mockImport('fs/promises', {
        readFile: stub(),
    });
    
    stopAll();
    t.end();
});

remove-useless-t-end

โŒ Example of incorrect code

test('test: remove me', () => {
    t.end();
    t.end();
});

โœ… Example of correct code

test('test: remove me', () => {
    t.end();
});

convert-ok-to-match

โŒ Example of incorrect code

t.ok(result.includes('hello'));

โœ… Example of correct code

t.match(result, /hello/);

convert-ok-to-called-with

โŒ Example of incorrect code

t.ok(set.calledWith(1, 2));

โœ… Example of correct code

t.calledWith(set, [1, 2]);

convert-equal-to-not-ok

โŒ Example of incorrect code

t.equal(error, null);

โœ… Example of correct code

t.notOk(error);

convert-equal-to-ok

โŒ Example of incorrect code

t.equal(result, true);

โœ… Example of correct code

t.ok(result);

convert-equal-to-deep-equal

โŒ Example of incorrect code

const expected = {
    hello: 'world',
};

t.equal(error, expected);
t.end();

โœ… Example of correct code

const expected = {
    hello: 'world',
};

t.deepEqual(error, expected);
t.end();

convert-equals-to-equal

Checkout in ๐ŸŠPutout Editor.

โŒ Example of incorrect code

t.equals(e.message, 'token should be a string!', 'should throw');

โœ… Example of correct code

t.equal(e.message, 'token should be a string!', 'should throw');

convert-match-regexp-to-string

โŒ Example of incorrect code

t.match(result, RegExp('hello'));

โœ… Example of correct code

t.match(result, 'hello');

remove-default-messages

๐Ÿ“ผSupertape will put this information for you, and it is always the same. No need to repeat the same information twice on one line, better to avoid it.

โŒ Example of incorrect code

t.equal(result, expected, 'should equal');

โœ… Example of correct code

t.equal(result, expected);

remove-useless-not-called-args

โŒ Example of incorrect code

t.notCalled(fn, []);

โœ… Example of correct code

t.notCalled(fn);

remove-only

โŒ Example of incorrect code

test.only('some test', (t) => {
    t.end();
});

โœ… Example of correct code

test('some test', (t) => {
    t.end();
});

remove-skip

โŒ Example of incorrect code

test.skip('some test', (t) => {
    t.end();
});

โœ… Example of correct code

test('some test', (t) => {
    t.end();
});

remove-stop-all

When reImport() or reRequire not called, stopAll() is redundant and should be removed.

โŒ Example of incorrect code

test('some test', (t) => {
    stopAll();
    t.end();
});

โœ… Example of correct code

test('some test', (t) => {
    t.end();
});

convert-mock-import-to-require

Convert mockRequire to mockImport.

โŒ Example of incorrect code

const mockRequire = require('mock-require');

const {reRequire, stopAll} = mockRequire;

test('', (t) => {
    mockRequire('fs/promises', {
        unlink: stub(),
    });
    
    const fn = reRequire('..');
    fn();
    
    stopAll();
    t.end();
});

โœ… Example of correct code

import {createMockImport} from 'mock-import';

const {
    mockImport,
    reImport,
    stopAll,
} = createMockImport(import.meta.url);

test('', async (t) => {
    mockImport('fs/promises', {
        unlink: stub(),
    });
    
    const fn = await reImport('..');
    fn();
    
    stopAll();
    t.end();
});

License

MIT