Skip to content

Commit

Permalink
chore: lint promises (#14808)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Dec 29, 2023
1 parent fb647d5 commit 69fd995
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 54 deletions.
2 changes: 0 additions & 2 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions packages/expect/src/__tests__/toThrowMatchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
};

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-core/src/TestScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
94 changes: 47 additions & 47 deletions packages/jest-core/src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export default async function watch(
passWithNoTests: true,
});

const updateConfigAndRun = ({
const updateConfigAndRun = async ({
bail,
changedSince,
collectCoverage,
Expand Down Expand Up @@ -205,7 +205,8 @@ export default async function watch(
})}`,
);
delete errorWithContext.stack;
return Promise.reject(errorWithContext);

throw errorWithContext;
}
checkForConflicts(watchPluginKeys, plugin, globalConfig);

Expand Down Expand Up @@ -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});
Expand All @@ -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);

// 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;
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});

// 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) => {
Expand Down Expand Up @@ -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();
}
},
() => {
Expand Down Expand Up @@ -463,7 +464,6 @@ export default async function watch(
}

startRun(globalConfig);
return Promise.resolve();
}

const checkForConflicts = (
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-haste-map/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/jest-jasmine2/src/PCancelable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>)
Expand Down
1 change: 1 addition & 0 deletions packages/jest-jasmine2/src/queueRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
}
2 changes: 1 addition & 1 deletion packages/jest-runner/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
1 change: 1 addition & 0 deletions packages/jest-util/src/__tests__/isPromise.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ test('a rejected Promise', () => {
});

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

Expand Down

0 comments on commit 69fd995

Please sign in to comment.