node/test/parallel/test-eslint-async-iife-no-unused-result.js
Anna Henningsen 77b68f9a29
tools: add linting rule for async IIFEs
The result of an async IIFE should always be handled in our tests,
typically by adding `.then(common.mustCall())` to verify that the
async function actually finishes executing at some point.

PR-URL: https://github.com/nodejs/node/pull/34363
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2020-07-20 18:13:32 +02:00

50 lines
1.5 KiB
JavaScript

'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
common.skipIfEslintMissing();
const RuleTester = require('../../tools/node_modules/eslint').RuleTester;
const rule = require('../../tools/eslint-rules/async-iife-no-unused-result');
const message = 'The result of an immediately-invoked async function needs ' +
'to be used (e.g. with `.then(common.mustCall())`)';
const tester = new RuleTester({ parserOptions: { ecmaVersion: 8 } });
tester.run('async-iife-no-unused-result', rule, {
valid: [
'(() => {})()',
'(async () => {})',
'(async () => {})().then()',
'(async () => {})().catch()',
'(function () {})()',
'(async function () {})',
'(async function () {})().then()',
'(async function () {})().catch()',
],
invalid: [
{
code: '(async () => {})()',
errors: [{ message }],
output: '(async () => {})()',
},
{
code: '(async function() {})()',
errors: [{ message }],
output: '(async function() {})()',
},
{
code: "const common = require('../common');(async () => {})()",
errors: [{ message }],
output: "const common = require('../common');(async () => {})()" +
'.then(common.mustCall())',
},
{
code: "const common = require('../common');(async function() {})()",
errors: [{ message }],
output: "const common = require('../common');(async function() {})()" +
'.then(common.mustCall())',
},
]
});