chore: lint promises (#14808)

This commit is contained in:
Simen Bekkhus 2023-12-30 00:22:50 +01:00 committed by GitHub
parent fb647d5a17
commit 69fd995fea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 53 additions and 52 deletions

View File

@ -704,9 +704,7 @@ module.exports = {
'unicorn/no-await-expression-member': 'off',
'unicorn/no-console-spaces': 'off',
'unicorn/no-object-as-default-parameter': 'off',
'unicorn/no-thenable': 'off',
'unicorn/no-typeof-undefined': 'off',
'unicorn/no-useless-promise-resolve-reject': 'off',
'unicorn/no-useless-undefined': 'off',
'unicorn/prefer-logical-operator-over-ternary': 'off',
'unicorn/prefer-math-trunc': 'off',

View File

@ -512,9 +512,9 @@ describe('toThrow', () => {
err = new Err('async apple');
}
if (resolve) {
return Promise.resolve(err || 'apple');
return err || 'apple';
} else {
return Promise.reject(err || 'apple');
throw err || 'apple';
}
};

View File

@ -114,7 +114,7 @@ class TestScheduler {
const onResult = async (test: Test, testResult: TestResult) => {
if (watcher.isInterrupted()) {
return Promise.resolve();
return;
}
if (testResult.testResults.length === 0) {

View File

@ -107,7 +107,7 @@ export default async function watch(
passWithNoTests: true,
});
const updateConfigAndRun = ({
const updateConfigAndRun = async ({
bail,
changedSince,
collectCoverage,
@ -205,7 +205,8 @@ export default async function watch(
})}`,
);
delete errorWithContext.stack;
return Promise.reject(errorWithContext);
throw errorWithContext;
}
checkForConflicts(watchPluginKeys, plugin, globalConfig);
@ -277,11 +278,9 @@ export default async function watch(
});
}
const startRun = (
globalConfig: Config.GlobalConfig,
): Promise<void | null> => {
const startRun = async (globalConfig: Config.GlobalConfig): Promise<void> => {
if (isRunning) {
return Promise.resolve(null);
return;
}
testWatcher = new TestWatcher({isWatchMode: true});
@ -291,53 +290,55 @@ export default async function watch(
const configs = contexts.map(context => context.config);
const changedFilesPromise = getChangedFilesPromise(globalConfig, configs);
return runJest({
changedFilesPromise,
contexts,
failedTestsCache,
filter,
globalConfig,
jestHooks: hooks.getEmitter(),
onComplete: results => {
isRunning = false;
hooks.getEmitter().onTestRunComplete(results);
try {
await runJest({
changedFilesPromise,
contexts,
failedTestsCache,
filter,
globalConfig,
jestHooks: hooks.getEmitter(),
onComplete: results => {
isRunning = false;
hooks.getEmitter().onTestRunComplete(results);
// Create a new testWatcher instance so that re-runs won't be blocked.
// The old instance that was passed to Jest will still be interrupted
// and prevent test runs from the previous run.
testWatcher = new TestWatcher({isWatchMode: true});
// Create a new testWatcher instance so that re-runs won't be blocked.
// The old instance that was passed to Jest will still be interrupted
// and prevent test runs from the previous run.
testWatcher = new TestWatcher({isWatchMode: true});
// Do not show any Watch Usage related stuff when running in a
// non-interactive environment
if (isInteractive) {
if (shouldDisplayWatchUsage) {
outputStream.write(usage(globalConfig, watchPlugins));
shouldDisplayWatchUsage = false; // hide Watch Usage after first run
isWatchUsageDisplayed = true;
// Do not show any Watch Usage related stuff when running in a
// non-interactive environment
if (isInteractive) {
if (shouldDisplayWatchUsage) {
outputStream.write(usage(globalConfig, watchPlugins));
shouldDisplayWatchUsage = false; // hide Watch Usage after first run
isWatchUsageDisplayed = true;
} else {
outputStream.write(showToggleUsagePrompt());
shouldDisplayWatchUsage = false;
isWatchUsageDisplayed = false;
}
} else {
outputStream.write(showToggleUsagePrompt());
shouldDisplayWatchUsage = false;
isWatchUsageDisplayed = false;
outputStream.write('\n');
}
} else {
outputStream.write('\n');
}
failedTestsCache.setTestResults(results.testResults);
},
outputStream,
startRun,
testWatcher,
}).catch(error =>
failedTestsCache.setTestResults(results.testResults);
},
outputStream,
startRun,
testWatcher,
});
} catch (error) {
// Errors thrown inside `runJest`, e.g. by resolvers, are caught here for
// continuous watch mode execution. We need to reprint them to the
// terminal and give just a little bit of extra space so they fit below
// `preRunMessagePrint` message nicely.
console.error(
`\n\n${formatExecError(error, contexts[0].config, {
`\n\n${formatExecError(error as any, contexts[0].config, {
noStackTrace: false,
})}`,
),
);
);
}
};
const onKeypress = (key: string) => {
@ -385,10 +386,10 @@ export default async function watch(
activePlugin = matchingWatchPlugin;
if (activePlugin.run) {
activePlugin.run(globalConfig, updateConfigAndRun).then(
shouldRerun => {
async shouldRerun => {
activePlugin = null;
if (shouldRerun) {
updateConfigAndRun();
await updateConfigAndRun();
}
},
() => {
@ -463,7 +464,6 @@ export default async function watch(
}
startRun(globalConfig);
return Promise.resolve();
}
const checkForConflicts = (

View File

@ -806,7 +806,7 @@ class HasteMap extends EventEmitter implements IHasteMap {
*/
private async _watch(hasteMap: InternalHasteMap): Promise<void> {
if (!this._options.watch) {
return Promise.resolve();
return;
}
// In watch mode, we'll only warn about module collisions and we'll retain

View File

@ -46,6 +46,7 @@ export default class PCancelable<T> implements PromiseLike<T> {
});
}
// eslint-disable-next-line unicorn/no-thenable
then<TResult1 = T, TResult2 = never>(
onFulfilled?:
| ((value: T) => TResult1 | PromiseLike<TResult1>)

View File

@ -92,6 +92,7 @@ export default function queueRunner(options: Options): PromiseLike<void> & {
return {
cancel: token.cancel.bind(token),
catch: result.catch.bind(result),
// eslint-disable-next-line unicorn/no-thenable
then: result.then.bind(result),
};
}

View File

@ -137,7 +137,7 @@ export default class TestRunner extends EmittingTestRunner {
const runTestInWorker = (test: Test) =>
mutex(async () => {
if (watcher.isInterrupted()) {
return Promise.reject();
throw new Error();
}
await this.#eventEmitter.emit('test-file-start', [test]);

View File

@ -25,6 +25,7 @@ test('a rejected Promise', () => {
});
test('a thenable', () => {
// eslint-disable-next-line unicorn/no-thenable
expect(isPromise({then: () => 'hello'})).toBe(true);
});