@putout/plugin-tape
Version:
πPutout plugin helps with tests
928 lines (670 loc) β’ 18.3 kB
Markdown
[]: https://img.shields.io/npm/v/@putout/plugin-tape.svg?style=flat&longCache=true
[]: https://npmjs.org/package/@putout/plugin-tape"npm"
> Tape-inspired TAP-compatible simplest high speed test runner with superpowers.
>
> (c) πΌ[**Supertape**](https://github.com/coderaiser/supertape)
π[**Putout**](https://github.com/coderaiser/putout) plugin helps to apply best parctises for tests written with πΌ[**Supertape**](https://github.com/coderaiser/supertape).
```
npm i @putout/plugin-tape -D
```
- β
[add-args](
- β
[add-await-to-re-import](
- β
[add-node-prefix-to-mock-require](
- β
[add-stop-all](
- β
[add-t-end](
- β
[apply-destructuring](
- β
[apply-stub](
- β
[apply-with-name](
- β
[convert-called-with-args](
- β
[convert-called-with-no-args-to-called-with](
- β
[convert-called-with-to-called-with-no-args](
- β
[convert-deep-equal-to-equal](
- β
[convert-does-not-throw-to-try-catch](
- β
[convert-emitter-to-promise](
- β
[convert-equal-to-called-once](
- β
[convert-equal-to-deep-equal](
- β
[convert-equal-to-not-ok](
- β
[convert-equal-to-ok](
- β
[convert-equals-to-equal](
- β
[convert-match-regexp-to-string](
- β
[convert-mock-require-to-mock-import](
- β
[convert-ok-to-called-with](
- β
[convert-ok-to-match](
- β
[convert-tape-to-supertape](
- β
[convert-throws-to-try-catch](
- β
[declare](
- β
[jest](
- β
[remove-default-messages](
- β
[remove-only](
- β
[remove-skip](
- β
[remove-stop-all](
- β
[remove-useless-not-called-args](
- β
[remove-useless-t-end](
- β
[switch-expected-with-result](
- β
[sync-with-name](
```json
{
"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/add-node-prefix-to-mock-require": "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"
}
}
```
π**Putout** gives ability to switch easily from [**Jest**](https://github.com/coderaiser/putout/blob/master/packages/plugin-jest/README.md#putoutplugin-jest-) to πΌ**Supertape**. Checkout in π[**Putout Editor**](https://putout.cloudcmd.io/#/gist/e610ded68d5808c2dbc09a1f84be8a96/c8f515668b5bdf16b58ef85c3ca13d93682f3ccb).
```js
it('should equal', () => {
expect(a).toEqual(b);
});
```
```js
import {test} from 'supertape';
test('should equal', () => {
t.equal(a, b);
t.end();
});
```
πΌ**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.
```js
test('plugin-apply-destructuring: transform: array: destructuring', (t) => {
t.equal(expected, result);
t.end();
});
```
```js
test('plugin-apply-destructuring: transform: array: destructuring', (t) => {
t.equal(result, expected);
t.end();
});
```
```js
const test = require('tape');
```
```js
const test = require('supertape');
```
```js
const test = require('supertape');
test('some message', (t) => {
t.throws(copymitter, /from should be a string!/, 'should throw when no args');
t.end();
});
```
```js
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();
});
```
```js
const test = require('supertape');
test('some message', (t) => {
t.doesNotThrow(copymitter, 'should throw when no args');
t.end();
});
```
```js
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();
});
```
```js
const test = require('supertape');
const {stub} = test;
test('some message', (t) => {
const fn = stub();
fn();
t.calledWith(fn, 'hello');
t.end();
});
```
```js
const test = require('supertape');
const {stub} = test;
test('some message', (t) => {
const fn = stub();
fn();
t.calledWith(fn, ['hello']);
t.end();
});
```
No need to use [`equal`](https://github.com/coderaiser/supertape#tequalactual-expected-msg), [`supertape`](https://github.com/coderaiser/supertape) supports [`calledOnce`](https://github.com/coderaiser/supertape/tree/master/packages/operator-stub#tcalledoncefn--message).
```js
const test = require('supertape');
const {stub} = test;
test('some message', (t) => {
const fn = stub();
fn();
t.equal(fn.callCount, 1);
t.end();
});
```
```js
const test = require('supertape');
const {stub} = test;
test('some message', (t) => {
const fn = stub();
fn();
t.calledOnce(fn);
t.end();
});
```
Use [`equal`](https://github.com/coderaiser/supertape#tequalactual-expected-msg) when comparing with primitives, [`deepEqual`](https://github.com/coderaiser/supertape#tdeepequalactual-expected-msg) for [`Objects`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) and [`Arrays`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array);
```js
const test = require('supertape');
const {stub} = test;
test('some message', (t) => {
t.deepEqual(x, 5);
t.end();
});
```
```js
const test = require('supertape');
const {stub} = test;
test('some message', (t) => {
t.equal(x, 5);
t.end();
});
```
```js
const test = require('supertape');
const {stub} = test;
test('some message', (t) => {
const fn = stub();
fn();
t.calledWith(fn);
t.end();
});
```
```js
const test = require('supertape');
const {stub} = test;
test('some message', (t) => {
const fn = stub();
fn();
t.calledWithNoArgs(fn);
t.end();
});
```
```js
const test = require('supertape');
const {stub} = test;
test('some message', (t) => {
const fn = stub();
fn();
t.calledWithNoArgs(fn, [1, 2]);
t.end();
});
```
```js
const test = require('supertape');
const {stub} = test;
test('some message', (t) => {
const fn = stub();
fn();
t.calledWith(fn, [1, 2]);
t.end();
});
```
```js
test('copymitter', (t) => {
const cp = copymitter(from, to, ['1']);
cp.on('end', (t) => {
t.end();
});
});
```
```js
const {once} = require('node:events');
test('copymitter', async (t) => {
const cp = copymitter(from, to, ['1']);
await once(cp, 'end');
t.end();
});
```
Check out in π[**Putout Editor**](https://putout.cloudcmd.io/#/gist/adf238850271c0a4ad917e71bab8757c/4ff416c3d9f3de0930339c7a4929eb4b62461d1a).
```js
const test = require('supertape');
const {stub} = test;
```
```js
const {test, stub} = require('supertape');
```
Apply [stub](https://github.com/cloudcmd/stub) functions created. Look how it works in π[**Putout Editor**](https://putout.cloudcmd.io/#/gist/d098a8c8d03a072a7f3981e3cfb1d7af/f1542bfa39513ac625b2a9cd9243cc06dee286c0).
```js
const a = async () => true;
const b = async () => {};
const c = async () => throwError('hello');
const d = async () => {
throw Error('hello');
};
```
```js
const a = stub().resolves(true);
const b = stub().resolves();
const c = stub().rejects(Error('hello'));
const d = stub().rejects(Error('hello'));
```
```js
test('should call init before show', (t) => {
const init = stub();
const show = stub();
t.calledInOrder([init, show]);
t.end();
});
```
```js
test('should call init before show', (t) => {
const init = stub().withName('init');
const show = stub().withName('show');
t.calledInOrder([init, show]);
t.end();
});
```
```js
test('should call init before show', (t) => {
const init = stub().withName('show');
const show = stub().withName('show');
t.calledInOrder([init, show]);
t.end();
});
```
```js
test('should call init before show', (t) => {
const init = stub().withName('init');
const show = stub().withName('show');
t.calledInOrder([init, show]);
t.end();
});
```
```js
import {stub} from 'supertape';
mockImport('fs/promises', {
readFile: stub().resolves(''),
});
```
```js
import {stub} from 'supertape';
import {createMockImport} from 'mock-import';
const {
mockImport,
stopAll,
reImport,
} = createMockImport(import.meta.url);
mockImport('fs/promises', {
readFile: stub().resolves(''),
});
```
```js
test('xxx', (t) => {
const a = stub();
t.end();
});
```
```js
import {test, stub} from 'supertape';
test('xxx', (t) => {
const a = stub();
t.end();
});
```
Checkout in π[**Putout Editor**](https://putout.cloudcmd.io/#/gist/38f43d1972b3ebe37597022b42024cd5/ccd8cf8a3ff4a30c6dccc528358f9439f1fd366e).
```js
mockRequire('fs/promises', {
readdir,
});
```
```js
mockRequire('node:fs/promises', {
readdir,
});
```
```js
test('xxx', () => {
t.end();
});
```
```js
test('xxx', (t) => {
t.end();
});
```
```js
test('xxx', () => {});
```
```js
test('xxx', (t) => {
t.end();
});
```
```js
test('stop-all: should be called', (t) => {
const read = reImport('./read');
t.end();
});
```
```js
test('stop-all: should be called', async (t) => {
const read = await reImport('./read');
t.end();
});
```
When you write test mocking `ESM` with [`mockImport()`](https://github.com/coderaiser/mock-import#mockimportname-mock) never forget to call [`stopAll()`](https://github.com/coderaiser/mock-import#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.
```js
test('stop-all: should be called', (t) => {
mockImport('fs/promises', {
readFile: stub(),
});
t.end();
});
```
```js
test('stop-all: should be called', (t) => {
mockImport('fs/promises', {
readFile: stub(),
});
stopAll();
t.end();
});
```
```js
test('test: remove me', () => {
t.end();
t.end();
});
```
```js
test('test: remove me', () => {
t.end();
});
```
```js
t.ok(result.includes('hello'));
```
```js
t.match(result, /hello/);
```
```js
t.ok(set.calledWith(1, 2));
```
```js
t.calledWith(set, [1, 2]);
```
```js
t.equal(error, null);
```
```js
t.notOk(error);
```
```js
t.equal(result, true);
```
```js
t.ok(result);
```
```js
const expected = {
hello: 'world',
};
t.equal(error, expected);
t.end();
```
```js
const expected = {
hello: 'world',
};
t.deepEqual(error, expected);
t.end();
```
Checkout in π[**Putout Editor**](https://putout.cloudcmd.io/#/gist/9a428ce99c5ec8966363a7a6da5bbba8/7441cbf7c8429b265259e7d67125fa138d7db19b).
```js
t.equals(e.message, 'token should be a string!', 'should throw');
```
```js
t.equal(e.message, 'token should be a string!', 'should throw');
```
```js
t.match(result, RegExp('hello'));
```
```js
t.match(result, 'hello');
```
πΌ**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.
```js
t.equal(result, expected, 'should equal');
```
```js
t.equal(result, expected);
```
```js
t.notCalled(fn, []);
```
```js
t.notCalled(fn);
```
```js
test.only('some test', (t) => {
t.end();
});
testDeclareBeforeReference.only('some test', (t) => {
t.end();
});
```
```js
test('some test', (t) => {
t.end();
});
testDeclareBeforeReference('some test', (t) => {
t.end();
});
```
```js
test.skip('some test', (t) => {
t.end();
});
```
```js
test('some test', (t) => {
t.end();
});
```
When `reImport()` or `reRequire` not called, `stopAll()` is redundant and should be removed.
```js
test('some test', (t) => {
stopAll();
t.end();
});
```
```js
test('some test', (t) => {
t.end();
});
```
Convert [mockRequire](https://github.com/boblauer/mock-require) to [mockImport](https://github.com/coderaiser/mock-import).
```js
const mockRequire = require('mock-require');
const {reRequire, stopAll} = mockRequire;
test('', (t) => {
mockRequire('fs/promises', {
unlink: stub(),
});
const fn = reRequire('..');
fn();
stopAll();
t.end();
});
```
```js
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();
});
```
MIT