Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(lint): use jest/require-to-throw-message rule #13377

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintrc.cjs
Expand Up @@ -168,6 +168,7 @@ module.exports = {
'jest/no-alias-methods': 'error',
'jest/no-focused-tests': 'error',
'jest/no-identical-title': 'error',
'jest/require-to-throw-message': 'error',
'jest/valid-expect': 'error',
},
},
Expand All @@ -186,6 +187,7 @@ module.exports = {
'import/no-extraneous-dependencies': 'off',
'import/no-unresolved': 'off',
'jest/no-focused-tests': 'off',
'jest/require-to-throw-message': 'off',
'no-console': 'off',
'no-undef': 'off',
'no-unused-vars': 'off',
Expand Down
2 changes: 1 addition & 1 deletion e2e/custom-matcher-stack-trace/__tests__/sync.test.js
Expand Up @@ -35,7 +35,7 @@ describe('Custom matcher', () => {
// This expectation should fail,
// Which is why it's wrapped in a .toThrow() block.
expect(() => 'foo').toCustomMatch('bar');
}).toThrow();
}).toThrow('Expected "bar" but got "foo"');
});

it('preserves error stack', () => {
Expand Down
1 change: 1 addition & 0 deletions e2e/failureDetails-property/__tests__/tests.test.js
Expand Up @@ -33,5 +33,6 @@ it('throws!', () => {
});

test('promise rejection', async () => {
// eslint-disable-next-line jest/require-to-throw-message
await expect(Promise.resolve(1)).rejects.toThrow();
});
2 changes: 1 addition & 1 deletion e2e/nested-event-loop/__tests__/nestedEventLoop.test.js
Expand Up @@ -27,5 +27,5 @@ it('can assert on errors across nested event loops', () => {
if (caught) {
throw caught;
}
}).toThrow();
}).toThrow('This should be caught.');
});
20 changes: 14 additions & 6 deletions packages/jest-config/src/__tests__/stringToBytes.test.ts
Expand Up @@ -21,11 +21,13 @@ describe('numeric input', () => {
});

test('should throw when no reference supplied', () => {
expect(() => stringToBytes(0.3)).toThrow();
expect(() => stringToBytes(0.3)).toThrow(
'For a percentage based memory limit a percentageReference must be supplied',
);
});

test('should throw on a bad input', () => {
expect(() => stringToBytes(-0.3, 51)).toThrow();
expect(() => stringToBytes(-0.3, 51)).toThrow('Unexpected numerical input');
});
});

Expand All @@ -40,17 +42,23 @@ describe('string input', () => {
});

test('should throw when no reference supplied', () => {
expect(() => stringToBytes('0.3')).toThrow();
expect(() => stringToBytes('0.3')).toThrow(
'For a percentage based memory limit a percentageReference must be supplied',
);
});

test('should throw on a bad input', () => {
expect(() => stringToBytes('-0.3', 51)).toThrow();
expect(() => stringToBytes('-0.3', 51)).toThrow(
'Unexpected numerical input',
);
});
});

describe('parsing', () => {
test('0% should throw an error', () => {
expect(() => stringToBytes('0%', 51)).toThrow();
expect(() => stringToBytes('0%', 51)).toThrow(
'Unexpected numerical input',
);
});

test('30%', () => {
Expand Down Expand Up @@ -109,7 +117,7 @@ describe('string input', () => {
});

test('unknown unit', () => {
expect(() => stringToBytes('50XX')).toThrow();
expect(() => stringToBytes('50XX')).toThrow('Unexpected input');
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-haste-map/src/__tests__/worker.test.js
Expand Up @@ -180,7 +180,7 @@ describe('worker', () => {

await expect(
getSha1({computeSha1: true, filePath: '/i/dont/exist.js', rootDir}),
).rejects.toThrow();
).rejects.toThrow("Cannot read path '/i/dont/exist.js'.");
});

it('avoids computing dependencies if not requested and Haste does not need it', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-matcher-utils/src/__tests__/index.test.ts
Expand Up @@ -310,7 +310,7 @@ describe('getLabelPrinter', () => {
expect(printLabel(stringConsistent)).toBe('Expected: ');
expect(() => {
printLabel(stringInconsistentLonger);
}).toThrow();
}).toThrow('Invalid count value');
});
});

Expand Down
20 changes: 14 additions & 6 deletions packages/jest-mock/src/__tests__/index.test.ts
Expand Up @@ -1261,13 +1261,17 @@ describe('moduleMocker', () => {
it('should throw on invalid input', () => {
expect(() => {
moduleMocker.spyOn(null, 'method');
}).toThrow();
}).toThrow('Cannot read prop');
expect(() => {
moduleMocker.spyOn({}, 'method');
}).toThrow();
}).toThrow(
'Cannot spy the method property because it is not a function; undefined given instead',
);
expect(() => {
moduleMocker.spyOn({method: 10}, 'method');
}).toThrow();
}).toThrow(
'Cannot spy the method property because it is not a function; number given instead',
);
});

it('supports restoring all spies', () => {
Expand Down Expand Up @@ -1427,13 +1431,17 @@ describe('moduleMocker', () => {
it('should throw on invalid input', () => {
expect(() => {
moduleMocker.spyOn(null, 'method');
}).toThrow();
}).toThrow('Cannot read prop');
expect(() => {
moduleMocker.spyOn({}, 'method');
}).toThrow();
}).toThrow(
'Cannot spy the method property because it is not a function; undefined given instead',
);
expect(() => {
moduleMocker.spyOn({method: 10}, 'method');
}).toThrow();
}).toThrow(
'Cannot spy the method property because it is not a function; number given instead',
);
});

it('supports restoring all spies', () => {
Expand Down
Expand Up @@ -132,7 +132,7 @@ describe('Runtime', () => {
const runtime = await createRuntime(__filename);
expect(() => {
runtime.requireMock(runtime.__mockRootPath, 'DoesntExist');
}).toThrow();
}).toThrow("Cannot find module 'DoesntExist' from 'root.js'");
});

it('uses manual mocks when using a custom resolver', async () => {
Expand Down
Expand Up @@ -366,10 +366,10 @@ describe('Runtime requireModule', () => {
const runtime = await createRuntime(__filename);
expect(() =>
runtime.requireModule(runtime.__mockRootPath, 'throwing'),
).toThrow();
).toThrow('throwing');
expect(() =>
runtime.requireModule(runtime.__mockRootPath, 'throwing'),
).toThrow();
).toThrow('throwing');
});

it('overrides module.createRequire', async () => {
Expand Down
8 changes: 5 additions & 3 deletions packages/jest-worker/src/__tests__/index.test.js
Expand Up @@ -100,13 +100,15 @@ it('exposes the right API using passed worker', () => {
it('breaks if any of the forbidden methods is tried to be exposed', () => {
expect(
() => new Farm('/tmp/baz.js', {exposedMethods: ['getStdout']}),
).toThrow();
).toThrow('Cannot define a method called getStdout');

expect(
() => new Farm('/tmp/baz.js', {exposedMethods: ['getStderr']}),
).toThrow();
).toThrow('Cannot define a method called getStderr');

expect(() => new Farm('/tmp/baz.js', {exposedMethods: ['end']})).toThrow();
expect(() => new Farm('/tmp/baz.js', {exposedMethods: ['end']})).toThrow(
'Cannot define a method called end',
);
});

it('works with minimal options', () => {
Expand Down
Expand Up @@ -317,7 +317,7 @@ describe.each([
test('worker stays dead', async () => {
await expect(
async () => await worker.waitForWorkerReady(),
).rejects.toThrow();
).rejects.toThrow('Worker state means it will never be ready: shut-down');
expect(worker.isWorkerRunning()).toBeFalsy();
});

Expand Down Expand Up @@ -385,7 +385,9 @@ describe.each([
test('processes exits', async () => {
worker.forceExit();

await expect(() => worker.waitForWorkerReady()).rejects.toThrow();
await expect(() => worker.waitForWorkerReady()).rejects.toThrow(
'Worker state means it will never be ready: shutting-down',
);
});
});
});
Expand Up @@ -376,7 +376,7 @@ it('throws if child is not forked', () => {
'fooWorks',
[],
]);
}).toThrow();
}).toThrow('Child can only be used on a forked process');

expect(() => {
process.emit('message', [
Expand All @@ -385,5 +385,5 @@ it('throws if child is not forked', () => {
'fooThrows',
[],
]);
}).toThrow();
}).toThrow('Child can only be used on a forked process');
});
Expand Up @@ -402,7 +402,7 @@ it('throws if child is not forked', () => {
'fooWorks',
[],
]);
}).toThrow();
}).toThrow('_worker_threads.parentPort.postMessage is not a function');

expect(() => {
thread.emit('message', [
Expand All @@ -411,5 +411,5 @@ it('throws if child is not forked', () => {
'fooThrows',
[],
]);
}).toThrow();
}).toThrow('_worker_threads.parentPort.postMessage is not a function');
});
20 changes: 13 additions & 7 deletions packages/pretty-format/src/__tests__/prettyFormat.test.ts
Expand Up @@ -7,7 +7,7 @@

/* eslint-disable local/prefer-rest-params-eventually */

import prettyFormat from '../';
import prettyFormat, {PrettyFormatOptions} from '../';

function returnArguments(..._args: Array<unknown>) {
return arguments;
Expand Down Expand Up @@ -667,8 +667,9 @@ describe('prettyFormat()', () => {

it('throws on invalid options', () => {
expect(() => {
// @ts-expect-error: Testing runtime error
prettyFormat({}, {invalidOption: true});
}).toThrow();
}).toThrow('Unknown option "invalidOption".');
});

it('supports plugins', () => {
Expand Down Expand Up @@ -709,9 +710,10 @@ describe('prettyFormat()', () => {

it('throws if plugin does not return a string', () => {
const val = 123;
const options = {
const options: PrettyFormatOptions = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed some simple type errors on the way.

plugins: [
{
// @ts-expect-error: Testing runtime error
print(val: unknown) {
return val;
},
Expand All @@ -723,7 +725,9 @@ describe('prettyFormat()', () => {
};
expect(() => {
prettyFormat(val, options);
}).toThrow();
}).toThrow(
'Plugin must return type "string" but instead returned "number".',
);
});

it('throws PrettyFormatPluginError if test throws an error', () => {
Expand Down Expand Up @@ -795,8 +799,10 @@ describe('prettyFormat()', () => {
prettyFormat(val, {
plugins: [
{
print(val: Array<unknown>, print: any) {
return val.map(item => print(item)).join(' - ');
print(val: unknown, print: any) {
return (val as Array<unknown>)
.map(item => print(item))
.join(' - ');
},
test(val: unknown) {
return Array.isArray(val);
Expand Down Expand Up @@ -965,7 +971,7 @@ describe('prettyFormat()', () => {
it('does not allow indent !== 0 in min mode', () => {
expect(() => {
prettyFormat(1, {indent: 1, min: true});
}).toThrow();
}).toThrow('Options "min" and "indent" cannot be used together.');
});
});
});