Test runner for 🐊Putout. Basically it is supercharged tape with additional asseritions:
npm i @putout/test -D
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
🐊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],
],
});
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();
});
Check error message of a plugin from input
code:
test('remove debugger: report', (t) => {
t.reportCode('debugger', 'Unexpected "debugger" statement');
t.end();
});
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.
Check transform of input
-> output
code:
test('remove-console: property identifier: code', (t) => {
t.transformCode('console.log()', '');
t.end();
});
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();
});
Check no report of filename.js
with options
:
test('putout: test: noReportWithOptions', (t) => {
const cache = new Map();
t.noReportWithOptions('remove-import', {
cache,
});
t.end();
});
Check transform of filename.js
with options
:
test('putout: plugin: declare-undefined-variables: transform: parse', (t) => {
t.transformWithOptions('parse', {
dismiss: [
'assign',
'stringify',
],
});
t.end();
});
When file should not be transformed:
test('test: declared', (t) => {
t.noTransform('declared');
t.end();
});
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();
});
Check error message of a plugin not produces
test('plugin-putout: check-replace-code: no report: typescript', (t) => {
t.noReport('typescript');
t.end();
});
Check error message of a plugin not produced
test('test: no report after transform', (t) => {
t.noReportAfterTransform('file');
t.end();
});
Check error message of a plugin not produced with provided options:
test('test: no report', (t) => {
t.noReportAfterTransformWithOptions('file');
t.end();
});
Check transform of filename.js
produce nothing
test('plugin-apply-numeric-separators: no transform: hex', (t) => {
t.noTransform('hex');
t.end();
});
Check progress of filename.js
;
test('remove usless variables: for-of', async ({progress}) => {
await progress('progress', {
i: 0,
n: 2,
rule: 'read-all-files',
});
});
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',
});
});
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,
});
Check file name formatting (pass process.env.UPDATE=1
to save fixture
):
test('formatter: codeframe', async ({format}) => {
await format(codeframe);
});
Check that there is no formatting for for such file:
test('formatter: codeframe: no', async ({noFormat}) => {
await noFormat(codeframe, 'no');
});
Check file name formatting (pass process.env.UPDATE=1
to save fixture
):
test('formatter: dump: many', async ({formatMany}) => {
await formatMany(dump, ['var', 'var']);
});
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();
});
First you need to create test with:
import {createTest} from '@putout/test/eslint';
const test = createTest(import.meta.url);
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.
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',
}],
},
});
});
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);
});
test('test: eslint: transform', (t) => {
t.transform('remove-debugger');
t.end();
});
test('test: eslint: report', (t) => {
t.report('remove-debugger', `Avoid 'debugger' statement`);
t.end();
});
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'],
});
Example:
test('putout: processor: json', async ({process}) => {
await process('eslintrc');
});
test('putout: processor: json', async ({process}) => {
await process('package', ['package-json']);
});
Check that filename would not be processed.
Example:
test('putout: process: json: no process', async ({noProcess}) => {
await noProcess('eslintrc', [], ['json']);
});
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)',
}]);
});
MIT