@putout/plugin-tape
🐊Putout plugin helps with tests
Last updated 9 months ago by coderaiser .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @putout/plugin-tape 
SYNC missed versions from official npm registry.

@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

Config

{
    "rules": {
        "tape/add-args": "on",
        "tape/add-t-end": "on",
        "tape/apply-assertions-order": "on",
        "tape/apply-destructuring": "on",
        "tape/apply-stringify": "on",
        "tape/apply-stub": "on",
        
        "tape/apply-with-name": "on",
        
        "tape/convert-called-with-args": "on",
        
        "tape/convert-called-with-no-args-to-called-with": "on",
        "tape/convert-called-with-to-called-with-no-args": "on",
        
        "tape/convert-deep-equal-to-equal": "on",
        "tape/convert-does-not-throw-to-try-catch": "on",
        
        "tape/convert-emitter-to-promise": "on",
        "tape/convert-equal-length-to-match-string": "on",
        "tape/convert-equal-to-called-once": "on",
        "tape/convert-equal-to-deep-equal": "on",
        "tape/convert-equals-to-equal": "on",
        
        "tape/convert-match-regexp-to-string": "on",
        "tape/convert-ok-to-called-with": "on",
        "tape/convert-ok-to-match": "on",
        
        "tape/convert-ok-to-pass": "on",
        "tape/convert-tape-to-supertape": "on",
        "tape/convert-throws-to-try-catch": "on",
        
        "tape/declare": "on",
        "tape/extract-args-from-called-with": "on",
        
        "tape/extract-result-from-assertion": "on",
        "tape/jest": "on",
        
        "tape/remove-default-messages": "on",
        
        "tape/remove-only": ["on", {
            "allowed": ["test"]
        }],
        "tape/remove-skip": ["on", {
            "allowed": ["test"]
        }],
        "tape/remove-t-from-async": "on",
        "tape/remove-useless-not-called-args": "on",
        "tape/remove-useless-t-end": "on",
        
        "tape/remove-useless-undefined": "on",
        "tape/switch-expected-with-result": "on",
        "tape/sync-with-name": "on"
    }
}

add-args

❌ Example of incorrect code

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

test('hello: world', async () => {
    const result = await get();
    
    deepEqual(result, []);
});

βœ… Example of correct code

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

test('hello: world', async ({deepEqual}) => {
    const result = await get();
    
    deepEqual(result, []);
});

add-t-end

❌ Example of incorrect code

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

βœ… Example of correct code

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

apply-assertions-order

Check out in 🐊Putout Editor.

❌ Example of incorrect code

t.equal(result, expected);
cleanup();
t.end();

βœ… Example of correct code

cleanup();

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

apply-destructuring

Check out in 🐊Putout Editor.

❌ Example of incorrect code

const test = require('supertape');

βœ… Example of correct code

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

apply-stringify

Checkout in 🐊Putout Editor

❌ Example of incorrect code

const read = stub().returns('{"checkCoverage": true}');

βœ… Example of correct code

const read = stub().returns(stringify({
    checkCoverage: true,
}));

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();
});

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-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-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-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-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-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('node:events');

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

convert-equal-length-to-match-string

Checkout in 🐊Putout Editor.

❌ Example of incorrect code

test('hello: world', (t) => {
    const count = (written.match(/## my-rule/) || []).length;
    
    t.equal(count, 1);
    t.end();
});

βœ… Example of correct code

test('hello: world', (t) => {
    t.match(written, /## my-rule/);
    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-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-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-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');

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-ok-to-match

❌ Example of incorrect code

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

βœ… Example of correct code

t.match(result, /hello/);

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 should be a string!');
    t.end();
});

declare

❌ 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();
});

extract-args-from-called-with

Checkout in 🐊Putout Editor.

❌ Example of incorrect code

test('abc: def', (t) => {
    t.calledWith(executeFn, [
        ['tape', 'test.js'], {
            exit,
        },
    ]);
});

βœ… Example of correct code

test('abc: def', (t) => {
    const args = [
        ['tape', 'test.js'], {
            exit,
        },
    ];
    
    t.calledWith(executeFn, args);
});

extract-result-from-assertion

Checkout in 🐊Putout Editor.

❌ Example of incorrect code

t.notOk(screen.getByRole('button').disabled);

t.deepEqual(readState({
    getItem,
}), {
    a: 1,
});

βœ… Example of correct code

const {disabled} = screen.getByRole('button');
t.notOk(disabled);

const result = readState({
    getItem,
});

const expected = {
    a: 1,
};

t.deepEqual(result, expected);

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();
});

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-only

❌ Example of incorrect code

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

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

βœ… Example of correct code

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

testDeclareBeforeReference('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-t-from-async

❌ Example of incorrect code

test('sqlite: all returns empty array', async (t) => {
    const db = await createDb();
    await db.exec('CREATE TABLE t (id INTEGER PRIMARY KEY, x TEXT)');
    const rows = await db.all('SELECT x FROM t WHERE x = :x', {
        x: 'missing',
    });
    
    await t.dbAll(rows, []);
    
    t.end();
});

βœ… Example of correct code

test('sqlite: all returns empty array', async ({dbAll}) => {
    const db = await createDb();
    await db.exec('CREATE TABLE t (id INTEGER PRIMARY KEY, x TEXT)');
    const rows = await db.all('SELECT x FROM t WHERE x = :x', {
        x: 'missing',
    });
    
    await dbAll(rows, []);
});

remove-useless-not-called-args

❌ Example of incorrect code

t.notCalled(fn, []);

βœ… Example of correct code

t.notCalled(fn);

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();
});

remove-useless-undefined

❌ Example of incorrect code

stub().returns(undefined);
stub().resolves(undefined);

βœ… Example of correct code

stub();
stub().resolves();

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();
});

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();
});

convert-ok-to-pass

Checkout in 🐊Putout Editor.

❌ Example of incorrect code

t.ok(true);

βœ… Example of correct code

t.pass();

License

MIT

Current Tags

  • 21.20.0                                ...           latest (2 months ago)

141 Versions

  • 21.20.0                                ...           2 months ago
  • 21.19.2                                ...           2 months ago
  • 21.19.1                                ...           2 months ago
  • 21.19.0                                ...           3 months ago
  • 21.18.0                                ...           3 months ago
  • 21.17.1                                ...           3 months ago
  • 21.17.0                                ...           3 months ago
  • 21.16.0                                ...           3 months ago
  • 21.15.1                                ...           4 months ago
  • 21.15.0                                ...           4 months ago
  • 21.14.0                                ...           4 months ago
  • 21.13.0                                ...           4 months ago
  • 21.12.0                                ...           4 months ago
  • 21.11.0                                ...           4 months ago
  • 21.10.0                                ...           4 months ago
  • 21.9.1                                ...           4 months ago
  • 21.9.0                                ...           4 months ago
  • 21.8.2                                ...           4 months ago
  • 21.8.1                                ...           4 months ago
  • 21.8.0                                ...           4 months ago
  • 21.7.0                                ...           4 months ago
  • 21.6.0                                ...           4 months ago
  • 21.5.0                                ...           5 months ago
  • 21.4.0                                ...           6 months ago
  • 21.3.0                                ...           7 months ago
  • 21.2.0                                ...           7 months ago
  • 21.1.0                                ...           9 months ago
  • 21.0.0                                ...           9 months ago
  • 20.3.1                                ...           10 months ago
  • 20.3.0                                ...           10 months ago
  • 20.2.0                                ...           10 months ago
  • 20.1.0                                ...           10 months ago
  • 20.0.0                                ...           10 months ago
  • 19.3.0                                ...           a year ago
  • 19.2.0                                ...           a year ago
  • 19.1.0                                ...           a year ago
  • 19.0.0                                ...           2 years ago
  • 18.0.0                                ...           2 years ago
  • 17.0.0                                ...           2 years ago
  • 16.1.0                                ...           2 years ago
  • 16.0.0                                ...           2 years ago
  • 15.1.0                                ...           2 years ago
  • 15.0.0                                ...           2 years ago
  • 14.2.0                                ...           3 years ago
  • 14.1.0                                ...           3 years ago
  • 14.0.1                                ...           3 years ago
  • 14.0.0                                ...           3 years ago
  • 13.1.0                                ...           3 years ago
  • 13.0.0                                ...           3 years ago
  • 12.1.0                                ...           3 years ago
  • 12.0.1                                ...           3 years ago
  • 12.0.0                                ...           3 years ago
  • 11.1.0                                ...           4 years ago
  • 11.0.0                                ...           4 years ago
  • 10.0.1                                ...           4 years ago
  • 10.0.0                                ...           4 years ago
  • 9.14.0                                ...           4 years ago
  • 9.13.0                                ...           4 years ago
  • 9.12.0                                ...           4 years ago
  • 9.11.0                                ...           4 years ago
  • 9.10.0                                ...           4 years ago
  • 9.9.1                                ...           4 years ago
  • 9.9.0                                ...           4 years ago
  • 9.8.0                                ...           5 years ago
  • 9.7.0                                ...           5 years ago
  • 9.6.0                                ...           5 years ago
  • 9.5.0                                ...           5 years ago
  • 9.4.0                                ...           5 years ago
  • 9.3.0                                ...           5 years ago
  • 9.2.1                                ...           5 years ago
  • 9.2.0                                ...           5 years ago
  • 9.1.0                                ...           5 years ago
  • 9.0.1                                ...           5 years ago
  • 9.0.0                                ...           5 years ago
  • 8.0.0                                ...           5 years ago
  • 7.7.0                                ...           5 years ago
  • 7.6.0                                ...           5 years ago
  • 7.5.0                                ...           5 years ago
  • 7.4.0                                ...           5 years ago
  • 7.3.0                                ...           5 years ago
  • 7.2.0                                ...           5 years ago
  • 7.1.0                                ...           5 years ago
  • 7.0.0                                ...           5 years ago
  • 6.7.0                                ...           5 years ago
  • 6.6.0                                ...           5 years ago
  • 6.5.0                                ...           5 years ago
  • 6.4.0                                ...           5 years ago
  • 6.3.0                                ...           5 years ago
  • 6.2.0                                ...           5 years ago
  • 6.1.0                                ...           5 years ago
  • 6.0.1                                ...           5 years ago
  • 6.0.0                                ...           5 years ago
  • 5.0.0                                ...           5 years ago
  • 4.5.0                                ...           5 years ago
  • 4.4.0                                ...           5 years ago
  • 4.3.0                                ...           5 years ago
  • 4.2.0                                ...           5 years ago
  • 4.1.0                                ...           5 years ago
  • 4.0.0                                ...           5 years ago
  • 3.16.0                                ...           5 years ago
  • 3.15.0                                ...           5 years ago
  • 3.14.2                                ...           5 years ago
  • 3.14.1                                ...           5 years ago
  • 3.14.0                                ...           5 years ago
  • 3.13.1                                ...           5 years ago
  • 3.13.0                                ...           5 years ago
  • 3.12.0                                ...           5 years ago
  • 3.11.0                                ...           5 years ago
  • 3.10.0                                ...           5 years ago
  • 3.9.0                                ...           5 years ago
  • 3.8.0                                ...           5 years ago
  • 3.7.1                                ...           5 years ago
  • 3.7.0                                ...           5 years ago
  • 3.6.0                                ...           5 years ago
  • 3.5.0                                ...           5 years ago
  • 3.4.0                                ...           5 years ago
  • 3.3.1                                ...           5 years ago
  • 3.3.0                                ...           5 years ago
  • 3.2.0                                ...           5 years ago
  • 3.1.0                                ...           5 years ago
  • 3.0.0                                ...           5 years ago
  • 2.7.0                                ...           5 years ago
  • 2.5.0                                ...           6 years ago
  • 2.4.0                                ...           6 years ago
  • 2.3.0                                ...           6 years ago
  • 2.2.0                                ...           6 years ago
  • 2.1.0                                ...           6 years ago
  • 2.0.2                                ...           6 years ago
  • 2.0.1                                ...           6 years ago
  • 2.0.0                                ...           6 years ago
  • 1.10.0                                ...           6 years ago
  • 1.9.0                                ...           6 years ago
  • 1.8.0                                ...           6 years ago
  • 1.7.0                                ...           6 years ago
  • 1.6.0                                ...           6 years ago
  • 1.5.0                                ...           6 years ago
  • 1.4.0                                ...           6 years ago
  • 1.3.0                                ...           6 years ago
  • 1.2.0                                ...           6 years ago
  • 1.1.0                                ...           6 years ago
  • 1.0.0                                ...           6 years ago
Maintainers (1)
Downloads
Total 299
Today 0
This Week 1
This Month 235
Last Day 0
Last Week 172
Last Month 64
Dependencies (0)
None
Dev Dependencies (9)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE