@putout/test
test runner for putout plugins
Last updated 5 years ago by coderaiser .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @putout/test 
SYNC missed versions from official npm registry.

@putout/test NPM version

Test runner for 🐊Putout. Basically it is supercharged tape with additional asseritions:

Install

npm i @putout/test -D

Autofix

Set environment variable UPDATE=1 to update transform and format fixtures (both source and fixed).

☝️ Remember that -fix.js fixtures will be removed when used in noReport, noTransform and noTransformWithOptions

UPDATE=1 tape test/*.js

Printer

You can override default printer (recast) with putout or babel using:

PUTOUT_PRINTER=putout tape test/*.js

Plugins API

🐊Putout can be used in all IDE's supported byESLint as eslint-plugin-putout. When async rules will be supported we can switch to ESM.

To write test for your plugins you need initialize test using createTest:

import {createTest} from '@putout/test';
import * as rmVars from '@putout/plugin-remove-unused-variables';

const test = createTest(import.meta.url, {
    'remove-unused-variables': rmVars,
});

You can also pass all 🐊Putout options:

const test = createTest(import.meta.url, {
    printer: 'putout',
    plugins: [
        ['remove-unused-variables', rmVars],
    ],
});

report(filename, message: string | string[])

Check error message (or messages) of a plugin:

test('remove usless variables: for-of', (t) => {
    t.report('dot', 'Dot files should be added to .gitignore');
    t.end();
});

When you want to check that report called exact count of times pass an array of messages:

test('remove usless variables: for-of', (t) => {
    t.report('dot', ['Dot files should be added to .gitignore']);
    t.end();
});

reportCode(input, message)

Check error message of a plugin from input code:

test('remove debugger: report', (t) => {
    t.reportCode('debugger', 'Unexpected "debugger" statement');
    t.end();
});

transform(filename [, output, plugins])

Check transform of filename.js -> filename-fix.js in test/fixtures directory:

test('remove usless variables: for-of', (t) => {
    t.transform('array-from', {
        'remove-useless-array-from': removeUselessArrayFrom,
    });
    t.end();
});

☝️When input and output the same test fails. Use noTransform() for such cases.

transformCode(input, output)

Check transform of input -> output code:

test('remove-console: property identifier: code', (t) => {
    t.transformCode('console.log()', '');
    t.end();
});

reportWithOptions(filename, options)

Check report of filename.js with options:

test('putout: test: reportWithOptions', (t) => {
    const cache = new Map();
    cache.set('x', 'y');
    
    t.reportWithOptions('remove-import', 'avoid imports', {
        cache,
    });
    t.end();
});

noReportWithOptions(filename, options)

Check no report of filename.js with options:

test('putout: test: noReportWithOptions', (t) => {
    const cache = new Map();
    
    t.noReportWithOptions('remove-import', {
        cache,
    });
    t.end();
});

transformWithOptions(filename, options)

Check transform of filename.js with options:

test('putout: plugin: declare-undefined-variables: transform: parse', (t) => {
    t.transformWithOptions('parse', {
        dismiss: [
            'assign',
            'stringify',
        ],
    });
    t.end();
});

noTransformWithOptions(filename, options)

When file should not be transformed:

test('test: declared', (t) => {
    t.noTransform('declared');
    t.end();
});

noTransformWithOptions(filename, options)

Check transform of filename.js with options:

test('putout: plugin: declare-undefined-variables: transform: assign: dismiss', (t) => {
    t.noTransformWithOptions('assign', {
        dismiss: [
            'assign',
            'stringify',
        ],
    });
    t.end();
});

noReport(filename)

Check error message of a plugin not produces

test('plugin-putout: check-replace-code: no report: typescript', (t) => {
    t.noReport('typescript');
    t.end();
});

noReportAfterTransform(filename)

Check error message of a plugin not produced

test('test: no report after transform', (t) => {
    t.noReportAfterTransform('file');
    t.end();
});

noReportAfterTransformWithOptions(filename)

Check error message of a plugin not produced with provided options:

test('test: no report', (t) => {
    t.noReportAfterTransformWithOptions('file');
    t.end();
});

noTransform(filename)

Check transform of filename.js produce nothing

test('plugin-apply-numeric-separators: no transform: hex', (t) => {
    t.noTransform('hex');
    t.end();
});

progress(filename, expected)

Check progress of filename.js;

test('remove usless variables: for-of', async ({progress}) => {
    await progress('progress', {
        i: 0,
        n: 2,
        rule: 'read-all-files',
    });
});

progressWithOptions(filename, options, expected)

Check progress of filename.js;

test('remove usless variables: for-of', async ({progressWithOptions}) => {
    const options = {
        from: '/home/coderaiser',
        to: '/',
    };
    
    await progressWithOptions('progress', options, {
        i: 0,
        n: 2,
        rule: 'read-all-files',
    });
});

Formatters API

First you need to create test with:

import {createTest} from '@putout/test';
import * as rmVars from '@putout/plugin-remove-unused-variables';

const test = createTest(import.meta.url, {
    'remove-unused-variables': rmVars,
});

format(formatter, filename)

Check file name formatting (pass process.env.UPDATE=1 to save fixture):

test('formatter: codeframe', async ({format}) => {
    await format(codeframe);
});

noFormat

Check that there is no formatting for for such file:

test('formatter: codeframe: no', async ({noFormat}) => {
    await noFormat(codeframe, 'no');
});

formatMany(formatter, [filename1, filename2])

Check file name formatting (pass process.env.UPDATE=1 to save fixture):

test('formatter: dump: many', async ({formatMany}) => {
    await formatMany(dump, ['var', 'var']);
});

Usage Example

Here is example of tests for remove-console:

const {createTest} = require('@putout/test');
const removeConsole = require('@putout/plugin-remove-console');

const test = createTest(__dirname, {
    'remove-console': removeConsole,
});

test('remove-console: report', (t) => {
    t.report('property-identifier', 'Unexpected "console" call');
    t.end();
});

test('remove-console: property identifier', (t) => {
    t.transform('property-identifier');
    t.end();
});

// when code should not be transformed
test('test: declared', (t) => {
    t.noTransformCode('alert()');
    t.end();
});

ESLint API

First you need to create test with:

import {createTest} from '@putout/test/eslint';

const test = createTest(import.meta.url);

process(filename [, config])

Works in similar to transform way:

  • βœ… reads operator-linebreak.js;
  • βœ… transforms it;
  • βœ… check that transformed is equal to operator-linebreak-fix.js;

Example:

test('test: eslint: transform', async ({process}) => {
    await process('operator-linebreak');
});

test('test: eslint: transform', async ({process}) => {
    await process('operator-linebreak', {
        rules: {
            'putout/putout': {
                rules: {
                    'convert-esm-to-commonjs': 'on',
                },
            },
        },
    });
});

☝️When input and output the same test fails. Use noProcess() for such cases.

noProcess(filename [, overrides])

Check that filename would not be processed.

Example:

test('test: eslint: noProcess', async ({noProcess}) => {
    await noProcess('operator-linebreak-fix');
});

test('test: eslint: noProcess', async ({noProcess}) => {
    await noProcess('operator-linebreak-fix', {
        rules: {
            'putout/putout': ['error', {
                printer: 'putout',
            }],
        },
    });
});

comparePlaces(filename, places[, overrides])

test('eslint-config: operator-line-break', async ({comparePlaces}) => {
    await comparePlaces('operator-linebreak', [{
        message: 'There should be no line break before or after \'=\'.',
        position: {
            column: 1,
            line: 2,
        },
        rule: 'operator-linebreak (eslint)',
    }]);
});

with overrides:

test('eslint-config: operator-line-break', async ({comparePlaces}) => {
    const overrides = {
        extends: ['plugin:putout/safe'],
    };
    
    await comparePlaces('operator-linebreak', [{
        message: 'There should be no line break before or after \'=\'.',
        position: {
            column: 1,
            line: 2,
        },
        rule: 'operator-linebreak (eslint)',
    }], overrides);
});

transform(name)

test('test: eslint: transform', (t) => {
    t.transform('remove-debugger');
    t.end();
});

report(filename, message | []messages)

test('test: eslint: report', (t) => {
    t.report('remove-debugger', `Avoid 'debugger' statement`);
    t.end();
});

Processors API

With processors api you can test processors in a simplest possible way.

First things first, init test with:

import {createTest} from '@putout/test/processor';

const test = createTest(import.meta.url, {
    extension: 'json',
    processors: ['json'],
    plugins: ['eslint'],
});

process(filename [, plugins, ])

Example:

test('putout: processor: json', async ({process}) => {
    await process('eslintrc');
});

test('putout: processor: json', async ({process}) => {
    await process('package', ['package-json']);
});

noProcess(filename [, plugins, processors])

Check that filename would not be processed.

Example:

test('putout: process: json: no process', async ({noProcess}) => {
    await noProcess('eslintrc', [], ['json']);
});

comparePlaces(filename, places)

test('putout: processor: css: places', async ({comparePlaces}) => {
    await comparePlaces('style', [{
        message: 'Expected indentation of 4 spaces (indentation)',
        position: {
            column: 1,
            line: 2,
        },
        rule: 'indentation (stylelint)',
    }]);
});

License

MIT

Current Tags

  • 10.0.0                                ...           latest (7 months ago)

112 Versions

  • 10.0.0                                ...           7 months ago
  • 9.1.0                                ...           9 months ago
  • 9.0.1                                ...           a year ago
  • 9.0.0                                ...           a year ago
  • 8.4.0                                ...           a year ago
  • 8.3.0                                ...           a year ago
  • 8.2.1                                ...           a year ago
  • 8.2.0                                ...           a year ago
  • 8.1.1                                ...           a year ago
  • 8.1.0                                ...           a year ago
  • 8.0.0                                ...           a year ago
  • 7.8.0                                ...           a year ago
  • 7.7.0                                ...           a year ago
  • 7.6.0                                ...           a year ago
  • 7.5.0                                ...           a year ago
  • 7.4.0                                ...           a year ago
  • 7.3.0                                ...           a year ago
  • 7.2.0                                ...           a year ago
  • 7.1.0                                ...           a year ago
  • 7.0.1                                ...           a year ago
  • 7.0.0                                ...           a year ago
  • 6.6.0                                ...           a year ago
  • 6.5.0                                ...           2 years ago
  • 6.4.0                                ...           2 years ago
  • 6.3.1                                ...           2 years ago
  • 6.3.0                                ...           2 years ago
  • 6.2.0                                ...           2 years ago
  • 6.1.1                                ...           2 years ago
  • 6.1.0                                ...           2 years ago
  • 6.0.1                                ...           2 years ago
  • 6.0.0                                ...           2 years ago
  • 5.14.0                                ...           2 years ago
  • 5.13.0                                ...           2 years ago
  • 5.12.0                                ...           2 years ago
  • 5.11.0                                ...           2 years ago
  • 5.10.0                                ...           2 years ago
  • 5.9.0                                ...           2 years ago
  • 5.8.0                                ...           2 years ago
  • 5.7.0                                ...           2 years ago
  • 5.6.0                                ...           2 years ago
  • 5.5.3                                ...           2 years ago
  • 5.5.2                                ...           2 years ago
  • 5.5.1                                ...           2 years ago
  • 5.5.0                                ...           2 years ago
  • 5.4.0                                ...           2 years ago
  • 5.3.0                                ...           2 years ago
  • 5.2.0                                ...           3 years ago
  • 5.1.0                                ...           3 years ago
  • 5.0.0                                ...           3 years ago
  • 4.5.0                                ...           3 years ago
  • 4.4.0                                ...           3 years ago
  • 4.3.0                                ...           3 years ago
  • 4.2.0                                ...           3 years ago
  • 4.1.0                                ...           3 years ago
  • 4.0.1                                ...           3 years ago
  • 4.0.0                                ...           3 years ago
  • 3.7.5                                ...           3 years ago
  • 3.7.4                                ...           3 years ago
  • 3.7.3                                ...           3 years ago
  • 3.7.2                                ...           3 years ago
  • 3.7.1                                ...           3 years ago
  • 3.7.0                                ...           3 years ago
  • 3.6.2                                ...           4 years ago
  • 3.6.1                                ...           4 years ago
  • 3.6.0                                ...           4 years ago
  • 3.5.0                                ...           4 years ago
  • 3.4.0                                ...           4 years ago
  • 3.3.2                                ...           4 years ago
  • 3.3.1                                ...           4 years ago
  • 3.3.0                                ...           4 years ago
  • 3.2.0                                ...           4 years ago
  • 3.1.2                                ...           4 years ago
  • 3.1.1                                ...           4 years ago
  • 3.1.0                                ...           4 years ago
  • 3.0.0                                ...           4 years ago
  • 2.3.0                                ...           4 years ago
  • 2.2.0                                ...           5 years ago
  • 2.1.1                                ...           5 years ago
  • 2.1.0                                ...           5 years ago
  • 2.0.0                                ...           5 years ago
  • 1.19.1                                ...           5 years ago
  • 1.19.0                                ...           5 years ago
  • 1.18.1                                ...           5 years ago
  • 1.18.0                                ...           5 years ago
  • 1.17.2                                ...           5 years ago
  • 1.17.1                                ...           5 years ago
  • 1.17.0                                ...           5 years ago
  • 1.16.2                                ...           5 years ago
  • 1.16.1                                ...           5 years ago
  • 1.16.0                                ...           5 years ago
  • 1.15.0                                ...           5 years ago
  • 1.14.0                                ...           5 years ago
  • 1.13.0                                ...           5 years ago
  • 1.12.1                                ...           6 years ago
  • 1.12.0                                ...           6 years ago
  • 1.11.0                                ...           6 years ago
  • 1.10.1                                ...           6 years ago
  • 1.10.0                                ...           6 years ago
  • 1.9.0                                ...           6 years ago
  • 1.8.3                                ...           6 years ago
  • 1.8.2                                ...           6 years ago
  • 1.8.1                                ...           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.1                                ...           6 years ago
  • 1.1.0                                ...           6 years ago
  • 1.0.0                                ...           6 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)

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