Skip to content

Commit

Permalink
chore: enforce errors are given messages (jestjs#14824)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Dec 31, 2023
1 parent 8f296b6 commit 5736ae3
Show file tree
Hide file tree
Showing 11 changed files with 16 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ module.exports = {
'no-unused-vars': 'off',
'sort-keys': 'off',
'unicorn/consistent-function-scoping': 'off',
'unicorn/error-message': 'off',
'unicorn/no-await-expression-member': 'off',
'unicorn/no-static-only-class': 'off',
'unicorn/prefer-number-properties': 'off',
Expand Down Expand Up @@ -373,6 +374,7 @@ module.exports = {
rules: {
'import/no-extraneous-dependencies': 'off',
'unicorn/consistent-function-scoping': 'off',
'unicorn/error-message': 'off',
},
},
{
Expand Down Expand Up @@ -696,7 +698,6 @@ module.exports = {
'unicorn/prefer-reflect-apply': 'off',

// TODO: turn on at some point
'unicorn/error-message': 'off',
'unicorn/prefer-string-replace-all': 'off',

// enabling this is blocked by https://github.com/microsoft/rushstack/issues/2780
Expand Down
4 changes: 2 additions & 2 deletions e2e/__tests__/__snapshots__/testFailingJasmine.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ FAIL __tests__/worksWithConcurrentMode.test.js
15 | });
16 |
at Function.failing (../../packages/jest-jasmine2/build/index.js:307:17)
at Function.failing (../../packages/jest-jasmine2/build/index.js:311:17)
at Suite.failing (__tests__/worksWithConcurrentMode.test.js:13:17)
at Object.describe (__tests__/worksWithConcurrentMode.test.js:8:1)
Expand Down Expand Up @@ -80,7 +80,7 @@ FAIL __tests__/worksWithConcurrentOnlyMode.test.js
15 | });
16 |
at Function.failing (../../packages/jest-jasmine2/build/index.js:307:17)
at Function.failing (../../packages/jest-jasmine2/build/index.js:311:17)
at Suite.failing (__tests__/worksWithConcurrentOnlyMode.test.js:13:22)
at Object.describe (__tests__/worksWithConcurrentOnlyMode.test.js:8:1)
Expand Down
12 changes: 3 additions & 9 deletions packages/expect/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import {equals, iterableEquality, subsetEquality} from '@jest/expect-utils';
import * as matcherUtils from 'jest-matcher-utils';
import {isPromise} from 'jest-util';
import {ErrorWithStack, isPromise} from 'jest-util';
import {
any,
anything,
Expand Down Expand Up @@ -428,21 +428,15 @@ const _validateResult = (result: any) => {
};

function assertions(expected: number): void {
const error = new Error();
if (Error.captureStackTrace) {
Error.captureStackTrace(error, assertions);
}
const error = new ErrorWithStack(undefined, assertions);

setState({
expectedAssertionsNumber: expected,
expectedAssertionsNumberError: error,
});
}
function hasAssertions(...args: Array<unknown>): void {
const error = new Error();
if (Error.captureStackTrace) {
Error.captureStackTrace(error, hasAssertions);
}
const error = new ErrorWithStack(undefined, hasAssertions);

matcherUtils.ensureNoExpected(args[0], '.hasAssertions');
setState({
Expand Down
1 change: 1 addition & 0 deletions packages/jest-circus/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ const _getError = (
asyncError = errors[1];
} else {
error = errors;
// eslint-disable-next-line unicorn/error-message
asyncError = new Error();
}

Expand Down
1 change: 1 addition & 0 deletions packages/jest-fake-timers/src/legacyFakeTimers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ export default class FakeTimers<TimerRef = unknown> {
'in this test file or enable fake timers for all tests by setting ' +
"{'enableGlobally': true, 'legacyFakeTimers': true} in " +
`Jest configuration file.\nStack Trace:\n${formatStackTrace(
// eslint-disable-next-line unicorn/error-message
new Error().stack!,
this._config,
{noStackTrace: false},
Expand Down
1 change: 1 addition & 0 deletions packages/jest-fake-timers/src/modernFakeTimers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export default class FakeTimers {
'with fake timers. Call `jest.useFakeTimers()` in this test file or enable ' +
"fake timers for all tests by setting 'fakeTimers': {'enableGlobally': true} " +
`in Jest configuration file.\nStack Trace:\n${formatStackTrace(
// eslint-disable-next-line unicorn/error-message
new Error().stack!,
this._config,
{noStackTrace: false},
Expand Down
1 change: 1 addition & 0 deletions packages/jest-jasmine2/src/jasmine/Spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export default class Spec {
this.queueRunnerFactory = attrs.queueRunnerFactory || function () {};
this.throwOnExpectationFailure = !!attrs.throwOnExpectationFailure;

// eslint-disable-next-line unicorn/error-message
this.initError = new Error();
this.initError.name = '';

Expand Down
2 changes: 2 additions & 0 deletions packages/jest-jasmine2/src/jasmineAsyncInstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function promisifyLifeCycleFunction(
return originalFn.call(env, asyncJestLifecycleWithCallback, timeout);
}

// eslint-disable-next-line unicorn/error-message
const extraError = new Error();

// Without this line v8 stores references to all closures
Expand Down Expand Up @@ -142,6 +143,7 @@ function promisifyIt(
return originalFn.call(env, specName, asyncJestTestWithCallback, timeout);
}

// eslint-disable-next-line unicorn/error-message
const extraError = new Error();

// Without this line v8 stores references to all closures
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 @@ -39,6 +39,7 @@ export default function queueRunner(options: Options): PromiseLike<void> & {
onCancel(resolve);
});

// eslint-disable-next-line unicorn/error-message
const mapper = ({fn, timeout, initError = new Error()}: QueueableFn) => {
let promise = new Promise<void>(resolve => {
const next = function (...args: [Error]) {
Expand Down
1 change: 1 addition & 0 deletions packages/jest-runner/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export default class TestRunner extends EmittingTestRunner {
const runTestInWorker = (test: Test) =>
mutex(async () => {
if (watcher.isInterrupted()) {
// eslint-disable-next-line unicorn/error-message
throw new Error();
}

Expand Down
1 change: 1 addition & 0 deletions packages/jest-snapshot/src/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export default class SnapshotState {
): void {
this._dirty = true;
if (options.isInline) {
// eslint-disable-next-line unicorn/error-message
const error = options.error || new Error();
const lines = getStackTraceLines(
removeLinesBeforeExternalMatcherTrap(error.stack || ''),
Expand Down

0 comments on commit 5736ae3

Please sign in to comment.