Browse Source

test: add tests for eslint rules

This adds tests for the custom eslint rules in this repository, using
the `RuleTester` test utility bundled with eslint.

PR-URL: https://github.com/nodejs/node/pull/16138
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
v9.x-staging
Teddy Katz 7 years ago
parent
commit
cd5ee52d70
No known key found for this signature in database GPG Key ID: B79EC7ABC0AA53A0
  1. 26
      test/parallel/test-eslint-alphabetize-errors.js
  2. 26
      test/parallel/test-eslint-buffer-constructor.js
  3. 32
      test/parallel/test-eslint-crypto-check.js
  4. 23
      test/parallel/test-eslint-inspector-check.js
  5. 38
      test/parallel/test-eslint-no-let-in-for-declaration.js
  6. 28
      test/parallel/test-eslint-no-unescaped-regexp-dot.js
  7. 25
      test/parallel/test-eslint-prefer-assert-iferror.js
  8. 37
      test/parallel/test-eslint-prefer-assert-methods.js
  9. 27
      test/parallel/test-eslint-prefer-common-mustnotcall.js
  10. 26
      test/parallel/test-eslint-require-buffer.js
  11. 31
      test/parallel/test-eslint-required-modules.js

26
test/parallel/test-eslint-alphabetize-errors.js

@ -0,0 +1,26 @@
'use strict';
require('../common');
const RuleTester = require('../../tools/eslint').RuleTester;
const rule = require('../../tools/eslint-rules/alphabetize-errors');
new RuleTester().run('alphabetize-errors', rule, {
valid: [
`
E('AAA', 'foo');
E('BBB', 'bar');
E('CCC', 'baz');
`
],
invalid: [
{
code: `
E('BBB', 'bar');
E('AAA', 'foo');
E('CCC', 'baz');
`,
errors: [{ message: 'Out of ASCIIbetical order - BBB >= AAA', line: 3 }]
}
]
});

26
test/parallel/test-eslint-buffer-constructor.js

@ -0,0 +1,26 @@
'use strict';
require('../common');
const RuleTester = require('../../tools/eslint').RuleTester;
const rule = require('../../tools/eslint-rules/buffer-constructor');
const message = 'Use of the Buffer() constructor has been deprecated. ' +
'Please use either Buffer.alloc(), Buffer.allocUnsafe(), ' +
'or Buffer.from()';
new RuleTester().run('buffer-constructor', rule, {
valid: [
'Buffer.from(foo)'
],
invalid: [
{
code: 'Buffer(foo)',
errors: [{ message }]
},
{
code: 'new Buffer(foo)',
errors: [{ message }]
}
]
});

32
test/parallel/test-eslint-crypto-check.js

@ -0,0 +1,32 @@
'use strict';
require('../common');
const RuleTester = require('../../tools/eslint').RuleTester;
const rule = require('../../tools/eslint-rules/crypto-check');
const message = 'Please add a hasCrypto check to allow this test to be ' +
'skipped when Node is built "--without-ssl".';
new RuleTester().run('crypto-check', rule, {
valid: [
'foo',
'crypto',
`
if (!common.hasCrypto) {
common.skip();
}
require('crypto');
`
],
invalid: [
{
code: 'require("crypto")',
errors: [{ message }]
},
{
code: 'if (common.foo) {} require("crypto")',
errors: [{ message }]
}
]
});

23
test/parallel/test-eslint-inspector-check.js

@ -0,0 +1,23 @@
'use strict';
require('../common');
const RuleTester = require('../../tools/eslint').RuleTester;
const rule = require('../../tools/eslint-rules/inspector-check');
const message = 'Please add a skipIfInspectorDisabled() call to allow this ' +
'test to be skippped when Node is built ' +
'\'--without-inspector\'.';
new RuleTester().run('inspector-check', rule, {
valid: [
'foo;',
'common.skipIfInspectorDisabled(); require("inspector");'
],
invalid: [
{
code: 'require("inspector")',
errors: [{ message }]
}
]
});

38
test/parallel/test-eslint-no-let-in-for-declaration.js

@ -0,0 +1,38 @@
'use strict';
require('../common');
const RuleTester = require('../../tools/eslint').RuleTester;
const rule = require('../../tools/eslint-rules/no-let-in-for-declaration');
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
const message = 'Use of `let` as the loop variable in a for-loop is ' +
'not recommended. Please use `var` instead.';
ruleTester.run('no-let-in-for-declaration', rule, {
valid: [
'let foo;',
'for (var foo = 1;;);',
'for (foo = 1;;);',
'for (;;);',
'for (const foo of bar);',
'for (var foo of bar);',
'for (const foo in bar);',
'for (var foo in bar);'
],
invalid: [
{
code: 'for (let foo = 1;;);',
errors: [{ message }]
},
{
code: 'for (let foo in bar);',
errors: [{ message }]
},
{
code: 'for (let foo of bar);',
errors: [{ message }]
}
]
});

28
test/parallel/test-eslint-no-unescaped-regexp-dot.js

@ -0,0 +1,28 @@
'use strict';
require('../common');
const RuleTester = require('../../tools/eslint').RuleTester;
const rule = require('../../tools/eslint-rules/no-unescaped-regexp-dot');
new RuleTester().run('no-unescaped-regexp-dot', rule, {
valid: [
'/foo/',
String.raw`/foo\./`,
'/.+/',
'/.*/',
'/.?/',
'/.{5}/',
String.raw`/\\\./`
],
invalid: [
{
code: '/./',
errors: [{ message: 'Unescaped dot character in regular expression' }]
},
{
code: String.raw`/\\./`,
errors: [{ message: 'Unescaped dot character in regular expression' }]
}
]
});

25
test/parallel/test-eslint-prefer-assert-iferror.js

@ -0,0 +1,25 @@
'use strict';
require('../common');
const RuleTester = require('../../tools/eslint').RuleTester;
const rule = require('../../tools/eslint-rules/prefer-assert-iferror');
new RuleTester().run('prefer-assert-iferror', rule, {
valid: [
'assert.ifError(err);',
'if (err) throw somethingElse;',
'throw err;',
'if (err) { throw somethingElse; }'
],
invalid: [
{
code: 'if (err) throw err;',
errors: [{ message: 'Use assert.ifError(err) instead.' }]
},
{
code: 'if (error) { throw error; }',
errors: [{ message: 'Use assert.ifError(error) instead.' }]
}
]
});

37
test/parallel/test-eslint-prefer-assert-methods.js

@ -0,0 +1,37 @@
'use strict';
require('../common');
const RuleTester = require('../../tools/eslint').RuleTester;
const rule = require('../../tools/eslint-rules/prefer-assert-methods');
new RuleTester().run('prefer-assert-methods', rule, {
valid: [
'assert.strictEqual(foo, bar)',
'assert(foo === bar && baz)'
],
invalid: [
{
code: 'assert(foo == bar)',
errors: [{ message: "'assert.equal' should be used instead of '=='" }]
},
{
code: 'assert(foo === bar)',
errors: [{
message: "'assert.strictEqual' should be used instead of '==='"
}]
},
{
code: 'assert(foo != bar)',
errors: [{
message: "'assert.notEqual' should be used instead of '!='"
}]
},
{
code: 'assert(foo !== bar)',
errors: [{
message: "'assert.notStrictEqual' should be used instead of '!=='"
}]
},
]
});

27
test/parallel/test-eslint-prefer-common-mustnotcall.js

@ -0,0 +1,27 @@
'use strict';
require('../common');
const RuleTester = require('../../tools/eslint').RuleTester;
const rule = require('../../tools/eslint-rules/prefer-common-mustnotcall');
const message = 'Please use common.mustNotCall(msg) instead of ' +
'common.mustCall(fn, 0) or common.mustCall(0).';
new RuleTester().run('prefer-common-mustnotcall', rule, {
valid: [
'common.mustNotCall(fn)',
'common.mustCall(fn)',
'common.mustCall(fn, 1)'
],
invalid: [
{
code: 'common.mustCall(fn, 0)',
errors: [{ message }]
},
{
code: 'common.mustCall(0)',
errors: [{ message }]
}
]
});

26
test/parallel/test-eslint-require-buffer.js

@ -0,0 +1,26 @@
'use strict';
require('../common');
const RuleTester = require('../../tools/eslint').RuleTester;
const rule = require('../../tools/eslint-rules/require-buffer');
const ruleTester = new RuleTester({
parserOptions: { ecmaVersion: 6 },
env: { node: true }
});
const message = "Use const Buffer = require('buffer').Buffer; " +
'at the beginning of this file';
ruleTester.run('require-buffer', rule, {
valid: [
'foo',
'const Buffer = require("Buffer"); Buffer;'
],
invalid: [
{
code: 'Buffer;',
errors: [{ message }]
}
]
});

31
test/parallel/test-eslint-required-modules.js

@ -0,0 +1,31 @@
'use strict';
require('../common');
const RuleTester = require('../../tools/eslint').RuleTester;
const rule = require('../../tools/eslint-rules/required-modules');
new RuleTester().run('required-modules', rule, {
valid: [
{
code: 'require("common")',
options: ['common']
},
{
code: 'foo',
options: []
},
],
invalid: [
{
code: 'foo',
options: ['common'],
errors: [{ message: 'Mandatory module "common" must be loaded.' }]
},
{
code: 'require("somethingElse")',
options: ['common'],
errors: [{ message: 'Mandatory module "common" must be loaded.' }]
}
]
});
Loading…
Cancel
Save