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/no-alias-methods rule #13371

Merged
merged 4 commits into from
Oct 3, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,12 @@ module.exports = {
],
env: {'jest/globals': true},
excludedFiles: ['**/__typetests__/**'],
extends: ['plugin:jest/style'],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Add jest/no-alias-methods rule also allows to simplify the config.

plugins: ['jest'],
rules: {
'jest/no-alias-methods': 'error',
'jest/no-focused-tests': 'error',
'jest/no-identical-title': 'error',
'jest/prefer-to-be': 'error',
'jest/prefer-to-contain': 'error',
'jest/prefer-to-have-length': 'error',
'jest/valid-expect': 'error',
},
},
Expand Down
18 changes: 9 additions & 9 deletions docs/ExpectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ it('transitions as expected', () => {
test('map calls its argument with a non-null argument', () => {
const mock = jest.fn();
[1].map(x => mock(x));
expect(mock).toBeCalledWith(expect.anything());
expect(mock).toHaveBeenCalledWith(expect.anything());
});
```

Expand All @@ -520,7 +520,7 @@ function getCat(fn) {
test('randocall calls its callback with a class instance', () => {
const mock = jest.fn();
getCat(mock);
expect(mock).toBeCalledWith(expect.any(Cat));
expect(mock).toHaveBeenCalledWith(expect.any(Cat));
});

function randocall(fn) {
Expand All @@ -530,7 +530,7 @@ function randocall(fn) {
test('randocall calls its callback with a number', () => {
const mock = jest.fn();
randocall(mock);
expect(mock).toBeCalledWith(expect.any(Number));
expect(mock).toHaveBeenCalledWith(expect.any(Number));
});
```

Expand Down Expand Up @@ -709,7 +709,7 @@ For example, let's say that we expect an `onPress` function to be called with an
test('onPress gets called with the right thing', () => {
const onPress = jest.fn();
simulatePresses(onPress);
expect(onPress).toBeCalledWith(
expect(onPress).toHaveBeenCalledWith(
expect.objectContaining({
x: expect.any(Number),
y: expect.any(Number),
Expand Down Expand Up @@ -1548,15 +1548,15 @@ test('throws on octopus', () => {
}

// Test that the error message says "yuck" somewhere: these are equivalent
expect(drinkOctopus).toThrowError(/yuck/);
expect(drinkOctopus).toThrowError('yuck');
expect(drinkOctopus).toThrow(/yuck/);
expect(drinkOctopus).toThrow('yuck');

// Test the exact error message
expect(drinkOctopus).toThrowError(/^yuck, octopus flavor$/);
expect(drinkOctopus).toThrowError(new Error('yuck, octopus flavor'));
expect(drinkOctopus).toThrow(/^yuck, octopus flavor$/);
expect(drinkOctopus).toThrow(new Error('yuck, octopus flavor'));

// Test that we get a DisgustingFlavorError
expect(drinkOctopus).toThrowError(DisgustingFlavorError);
expect(drinkOctopus).toThrow(DisgustingFlavorError);
});
```

Expand Down
4 changes: 2 additions & 2 deletions docs/GlobalAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ const binaryStringToNumber = binString => {
describe('binaryStringToNumber', () => {
describe('given an invalid binary string', () => {
test('composed of non-numbers throws CustomError', () => {
expect(() => binaryStringToNumber('abc')).toThrowError(CustomError);
expect(() => binaryStringToNumber('abc')).toThrow(CustomError);
});

test('with extra whitespace throws CustomError', () => {
expect(() => binaryStringToNumber(' 100')).toThrowError(CustomError);
expect(() => binaryStringToNumber(' 100')).toThrow(CustomError);
});
});

Expand Down
4 changes: 2 additions & 2 deletions docs/MockFunctionAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,8 @@ test('calculate calls add', () => {
// requiring `add`.
calculate(mockAdd, 1, 2);

expect(mockAdd).toBeCalledTimes(1);
expect(mockAdd).toBeCalledWith(1, 2);
expect(mockAdd).toHaveBeenCalledTimes(1);
expect(mockAdd).toHaveBeenCalledWith(1, 2);
});
```

Expand Down
8 changes: 4 additions & 4 deletions e2e/__tests__/failureDetailsProperty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,19 @@ test('that the failureDetails property is set', () => {
Object {
"actual": "",
"error": Object {
"message": "expect(received).rejects.toThrowError()
"message": "expect(received).rejects.toThrow()

Received promise resolved instead of rejected
Resolved to value: 1",
},
"expected": "",
"matcherName": "",
"message": "Error: expect(received).rejects.toThrowError()
"message": "Error: expect(received).rejects.toThrow()

Received promise resolved instead of rejected
Resolved to value: 1",
"passed": false,
"stack": "Error: expect(received).rejects.toThrowError()
"stack": "Error: expect(received).rejects.toThrow()

Received promise resolved instead of rejected
Resolved to value: 1
Expand Down Expand Up @@ -245,7 +245,7 @@ test('that the failureDetails property is set', () => {
],
Array [
Object {
"message": "expect(received).rejects.toThrowError()
"message": "expect(received).rejects.toThrow()

Received promise resolved instead of rejected
Resolved to value: 1",
Expand Down
2 changes: 1 addition & 1 deletion e2e/failureDetails-property/__tests__/tests.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ it('throws!', () => {
});

test('promise rejection', async () => {
await expect(Promise.resolve(1)).rejects.toThrowError();
await expect(Promise.resolve(1)).rejects.toThrow();
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ jest.mock('@@storage/track/Track');

test('relative import', () => {
const track = new Track();
expect(track.someRandomFunction).not.toBeCalled();
expect(track.someRandomFunction).not.toHaveBeenCalled();
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ jest.mock('@@storage/track/Track');

test('through moduleNameMapper', () => {
const track = new Track();
expect(track.someRandomFunction).not.toBeCalled();
expect(track.someRandomFunction).not.toHaveBeenCalled();
});
2 changes: 1 addition & 1 deletion examples/jquery/__tests__/display_user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ it('displays a user after a click', () => {

// Assert that the fetchCurrentUser function was called, and that the
// #username span's inner text was updated as we'd expect it to.
expect(fetchCurrentUser).toBeCalled();
expect(fetchCurrentUser).toHaveBeenCalled();
expect($('#username').text()).toBe('Johnny Cash - Logged In');
});
2 changes: 1 addition & 1 deletion examples/jquery/__tests__/fetch_current_user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ it('calls into $.ajax with the correct params', () => {

// Now make sure that $.ajax was properly called during the previous
// 2 lines
expect($.ajax).toBeCalledWith({
expect($.ajax).toHaveBeenCalledWith({
success: expect.any(Function),
type: 'GET',
url: 'http://example.com/currentUser',
Expand Down
6 changes: 3 additions & 3 deletions examples/timer/__tests__/infinite_timer_game.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ it('schedules a 10-second timer after 1 second', () => {

// At this point in time, there should have been a single call to
// setTimeout to schedule the end of the game in 1 second.
expect(setTimeout).toBeCalledTimes(1);
expect(setTimeout).toHaveBeenCalledTimes(1);
expect(setTimeout).toHaveBeenNthCalledWith(1, expect.any(Function), 1000);

// Fast forward and exhaust only currently pending timers
// (but not any new timers that get created during that process)
jest.runOnlyPendingTimers();

// At this point, our 1-second timer should have fired its callback
expect(callback).toBeCalled();
expect(callback).toHaveBeenCalled();

// And it should have created a new timer to start the game over in
// 10 seconds
expect(setTimeout).toBeCalledTimes(2);
expect(setTimeout).toHaveBeenCalledTimes(2);
expect(setTimeout).toHaveBeenNthCalledWith(2, expect.any(Function), 10000);
});
16 changes: 8 additions & 8 deletions examples/timer/__tests__/timer_game.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ describe('timerGame', () => {
const timerGame = require('../timerGame');
timerGame();

expect(setTimeout).toBeCalledTimes(1);
expect(setTimeout).toBeCalledWith(expect.any(Function), 1000);
expect(setTimeout).toHaveBeenCalledTimes(1);
expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 1000);
});

it('calls the callback after 1 second via runAllTimers', () => {
Expand All @@ -23,14 +23,14 @@ describe('timerGame', () => {
timerGame(callback);

// At this point in time, the callback should not have been called yet
expect(callback).not.toBeCalled();
expect(callback).not.toHaveBeenCalled();

// Fast-forward until all timers have been executed
jest.runAllTimers();

// Now our callback should have been called!
expect(callback).toBeCalled();
expect(callback).toBeCalledTimes(1);
expect(callback).toHaveBeenCalled();
expect(callback).toHaveBeenCalledTimes(1);
});

it('calls the callback after 1 second via advanceTimersByTime', () => {
Expand All @@ -40,13 +40,13 @@ describe('timerGame', () => {
timerGame(callback);

// At this point in time, the callback should not have been called yet
expect(callback).not.toBeCalled();
expect(callback).not.toHaveBeenCalled();

// Fast-forward until all timers have been executed
jest.advanceTimersByTime(1000);

// Now our callback should have been called!
expect(callback).toBeCalled();
expect(callback).toBeCalledTimes(1);
expect(callback).toHaveBeenCalled();
expect(callback).toHaveBeenCalledTimes(1);
});
});
14 changes: 6 additions & 8 deletions examples/typescript/__tests__/calc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('calc - mocks', () => {
const result = calc('Sub', [2, 2]);

expect(result).toBe(0);
expect(mockSub).toBeCalledWith(2, 2);
expect(mockSub).toHaveBeenCalledWith(2, 2);
});

it('returns result from sum', () => {
Expand All @@ -34,7 +34,7 @@ describe('calc - mocks', () => {
const result = calc('Sum', [1, 1]);

expect(result).toBe(2);
expect(mockSum).toBeCalledWith(1, 1);
expect(mockSum).toHaveBeenCalledWith(1, 1);
});

it('adds last result to memory', () => {
Expand All @@ -47,7 +47,7 @@ describe('calc - mocks', () => {

expect(sumResult).toBe(2);
expect(memoryResult).toBe(2);
expect(MockMemory.prototype.add).toBeCalledWith(2);
expect(MockMemory.prototype.add).toHaveBeenCalledWith(2);
});

it('subtracts last result to memory', () => {
Expand All @@ -60,7 +60,7 @@ describe('calc - mocks', () => {

expect(sumResult).toBe(2);
expect(memoryResult).toBe(2);
expect(MockMemory.prototype.subtract).toBeCalledWith(2);
expect(MockMemory.prototype.subtract).toHaveBeenCalledWith(2);
});

it('clears the memory', () => {
Expand All @@ -77,15 +77,13 @@ describe('calc - mocks', () => {
expect(memoryResult).toBe(2);
expect(sumResult2).toBe(4);
expect(clearResult).toBe(4);
expect(MockMemory.prototype.reset).toBeCalledTimes(1);
expect(MockMemory.prototype.reset).toHaveBeenCalledTimes(1);
});

it('throws an error when invalid Op is passed', () => {
const calc = makeCalc(memory);

// @ts-expect-error
expect(() => calc('Multiply', [2, 3])).toThrowError(
new Error('Invalid op'),
);
expect(() => calc('Multiply', [2, 3])).toThrow(new Error('Invalid op'));
});
});
Loading