diff --git a/test/parallel/test-eslint-alphabetize-errors.js b/test/parallel/test-eslint-alphabetize-errors.js new file mode 100644 index 0000000000..83c73fc78c --- /dev/null +++ b/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 }] + } + ] +}); diff --git a/test/parallel/test-eslint-buffer-constructor.js b/test/parallel/test-eslint-buffer-constructor.js new file mode 100644 index 0000000000..dfff1374c1 --- /dev/null +++ b/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 }] + } + ] +}); diff --git a/test/parallel/test-eslint-crypto-check.js b/test/parallel/test-eslint-crypto-check.js new file mode 100644 index 0000000000..102560e78e --- /dev/null +++ b/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 }] + } + ] +}); diff --git a/test/parallel/test-eslint-inspector-check.js b/test/parallel/test-eslint-inspector-check.js new file mode 100644 index 0000000000..20774284ab --- /dev/null +++ b/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 }] + } + ] +}); diff --git a/test/parallel/test-eslint-no-let-in-for-declaration.js b/test/parallel/test-eslint-no-let-in-for-declaration.js new file mode 100644 index 0000000000..b6b7c35745 --- /dev/null +++ b/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 }] + } + ] +}); diff --git a/test/parallel/test-eslint-no-unescaped-regexp-dot.js b/test/parallel/test-eslint-no-unescaped-regexp-dot.js new file mode 100644 index 0000000000..5dbd355e71 --- /dev/null +++ b/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' }] + } + ] +}); diff --git a/test/parallel/test-eslint-prefer-assert-iferror.js b/test/parallel/test-eslint-prefer-assert-iferror.js new file mode 100644 index 0000000000..00df47975b --- /dev/null +++ b/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.' }] + } + ] +}); diff --git a/test/parallel/test-eslint-prefer-assert-methods.js b/test/parallel/test-eslint-prefer-assert-methods.js new file mode 100644 index 0000000000..2129c08322 --- /dev/null +++ b/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 '!=='" + }] + }, + ] +}); diff --git a/test/parallel/test-eslint-prefer-common-mustnotcall.js b/test/parallel/test-eslint-prefer-common-mustnotcall.js new file mode 100644 index 0000000000..848e762ce3 --- /dev/null +++ b/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 }] + } + ] +}); diff --git a/test/parallel/test-eslint-require-buffer.js b/test/parallel/test-eslint-require-buffer.js new file mode 100644 index 0000000000..21be41f995 --- /dev/null +++ b/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 }] + } + ] +}); diff --git a/test/parallel/test-eslint-required-modules.js b/test/parallel/test-eslint-required-modules.js new file mode 100644 index 0000000000..8203cd0d0a --- /dev/null +++ b/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.' }] + } + ] +});