Skip to content

Commit

Permalink
chore(lint): use jest/prefer-to-be rule (#13369)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrazauskas committed Oct 3, 2022
1 parent 04dae6c commit 8219820
Show file tree
Hide file tree
Showing 178 changed files with 760 additions and 759 deletions.
7 changes: 7 additions & 0 deletions .eslintrc.cjs
Expand Up @@ -166,6 +166,7 @@ module.exports = {
rules: {
'jest/no-focused-tests': 'error',
'jest/no-identical-title': 'error',
'jest/prefer-to-be': 'error',
'jest/valid-expect': 'error',
},
},
Expand All @@ -191,6 +192,12 @@ module.exports = {
'sort-keys': 'off',
},
},
{
files: ['**/UsingMatchers.md/**'],
rules: {
'jest/prefer-to-be': 'off',
},
},

// snapshots in examples plus inline snapshots need to keep backtick
{
Expand Down
6 changes: 3 additions & 3 deletions docs/Es6ClassMocks.md
Expand Up @@ -81,7 +81,7 @@ it('We can check if the consumer called a method on the class instance', () => {
// mock.instances is available with automatic mocks:
const mockSoundPlayerInstance = SoundPlayer.mock.instances[0];
const mockPlaySoundFile = mockSoundPlayerInstance.playSoundFile;
expect(mockPlaySoundFile.mock.calls[0][0]).toEqual(coolSoundFileName);
expect(mockPlaySoundFile.mock.calls[0][0]).toBe(coolSoundFileName);
// Equivalent to above check:
expect(mockPlaySoundFile).toHaveBeenCalledWith(coolSoundFileName);
expect(mockPlaySoundFile).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -349,7 +349,7 @@ jest.mock('./sound-player', () => {
});
```

This will let us inspect usage of our mocked class, using `SoundPlayer.mock.calls`: `expect(SoundPlayer).toHaveBeenCalled();` or near-equivalent: `expect(SoundPlayer.mock.calls.length).toEqual(1);`
This will let us inspect usage of our mocked class, using `SoundPlayer.mock.calls`: `expect(SoundPlayer).toHaveBeenCalled();` or near-equivalent: `expect(SoundPlayer.mock.calls.length).toBe(1);`

### Mocking non-default class exports

Expand Down Expand Up @@ -444,6 +444,6 @@ it('We can check if the consumer called a method on the class instance', () => {
const soundPlayerConsumer = new SoundPlayerConsumer();
const coolSoundFileName = 'song.mp3';
soundPlayerConsumer.playSomethingCool();
expect(mockPlaySoundFile.mock.calls[0][0]).toEqual(coolSoundFileName);
expect(mockPlaySoundFile.mock.calls[0][0]).toBe(coolSoundFileName);
});
```
38 changes: 19 additions & 19 deletions docs/JestObjectAPI.md
Expand Up @@ -229,17 +229,17 @@ const example = jest.createMockFromModule('../example');

test('should run example code', () => {
// creates a new mocked function with no formal arguments.
expect(example.function.name).toEqual('square');
expect(example.function.length).toEqual(0);
expect(example.function.name).toBe('square');
expect(example.function.length).toBe(0);

// async functions get the same treatment as standard synchronous functions.
expect(example.asyncFunction.name).toEqual('asyncSquare');
expect(example.asyncFunction.length).toEqual(0);
expect(example.asyncFunction.name).toBe('asyncSquare');
expect(example.asyncFunction.length).toBe(0);

// creates a new class with the same interface, member functions and properties are mocked.
expect(example.class.constructor.name).toEqual('Bar');
expect(example.class.foo.name).toEqual('foo');
expect(example.class.array.length).toEqual(0);
expect(example.class.constructor.name).toBe('Bar');
expect(example.class.foo.name).toBe('foo');
expect(example.class.array.length).toBe(0);

// creates a deeply cloned version of the original object.
expect(example.object).toEqual({
Expand All @@ -251,12 +251,12 @@ test('should run example code', () => {
});

// creates a new empty array, ignoring the original array.
expect(example.array.length).toEqual(0);
expect(example.array.length).toBe(0);

// creates a new property with the same primitive value as the original property.
expect(example.number).toEqual(123);
expect(example.string).toEqual('baz');
expect(example.boolean).toEqual(true);
expect(example.number).toBe(123);
expect(example.string).toBe('baz');
expect(example.boolean).toBe(true);
expect(example.symbol).toEqual(Symbol.for('a.b.c'));
});
```
Expand Down Expand Up @@ -380,15 +380,15 @@ test('moduleName 1', () => {
return jest.fn(() => 1);
});
const moduleName = require('../moduleName');
expect(moduleName()).toEqual(1);
expect(moduleName()).toBe(1);
});

test('moduleName 2', () => {
jest.doMock('../moduleName', () => {
return jest.fn(() => 2);
});
const moduleName = require('../moduleName');
expect(moduleName()).toEqual(2);
expect(moduleName()).toBe(2);
});
```

Expand All @@ -403,15 +403,15 @@ test('moduleName 1', () => {
return jest.fn(() => 1);
});
const moduleName = require('../moduleName');
expect(moduleName()).toEqual(1);
expect(moduleName()).toBe(1);
});

test('moduleName 2', () => {
jest.doMock<typeof import('../moduleName')>('../moduleName', () => {
return jest.fn(() => 2);
});
const moduleName = require('../moduleName');
expect(moduleName()).toEqual(2);
expect(moduleName()).toBe(2);
});
```

Expand All @@ -435,8 +435,8 @@ test('moduleName 1', () => {
};
});
return import('../moduleName').then(moduleName => {
expect(moduleName.default).toEqual('default1');
expect(moduleName.foo).toEqual('foo1');
expect(moduleName.default).toBe('default1');
expect(moduleName.foo).toBe('foo1');
});
});

Expand All @@ -449,8 +449,8 @@ test('moduleName 2', () => {
};
});
return import('../moduleName').then(moduleName => {
expect(moduleName.default).toEqual('default2');
expect(moduleName.foo).toEqual('foo2');
expect(moduleName.default).toBe('default2');
expect(moduleName.foo).toBe('foo2');
});
});
```
Expand Down
2 changes: 1 addition & 1 deletion docs/MockFunctions.md
Expand Up @@ -79,7 +79,7 @@ expect(someMockFunction.mock.instances.length).toBe(2);

// The object returned by the first instantiation of this function
// had a `name` property whose value was set to 'test'
expect(someMockFunction.mock.instances[0].name).toEqual('test');
expect(someMockFunction.mock.instances[0].name).toBe('test');

// The first argument of the last call to the function was 'test'
expect(someMockFunction.mock.lastCall[0]).toBe('test');
Expand Down
8 changes: 4 additions & 4 deletions docs/TutorialAsync.md
Expand Up @@ -68,7 +68,7 @@ import * as user from '../user';
// The assertion for a promise must be returned.
it('works with promises', () => {
expect.assertions(1);
return user.getUserName(4).then(data => expect(data).toEqual('Mark'));
return user.getUserName(4).then(data => expect(data).toBe('Mark'));
});
```

Expand All @@ -81,7 +81,7 @@ There is a less verbose way using `resolves` to unwrap the value of a fulfilled
```js
it('works with resolves', () => {
expect.assertions(1);
return expect(user.getUserName(5)).resolves.toEqual('Paul');
return expect(user.getUserName(5)).resolves.toBe('Paul');
});
```

Expand All @@ -94,13 +94,13 @@ Writing tests using the `async`/`await` syntax is also possible. Here is how you
it('works with async/await', async () => {
expect.assertions(1);
const data = await user.getUserName(4);
expect(data).toEqual('Mark');
expect(data).toBe('Mark');
});

// async/await can also be used with `.resolves`.
it('works with async/await and resolves', async () => {
expect.assertions(1);
await expect(user.getUserName(5)).resolves.toEqual('Paul');
await expect(user.getUserName(5)).resolves.toBe('Paul');
});
```

Expand Down
4 changes: 2 additions & 2 deletions docs/TutorialReact.md
Expand Up @@ -282,11 +282,11 @@ it('CheckboxWithLabel changes the text after click', () => {
// Render a checkbox with label in the document
const checkbox = shallow(<CheckboxWithLabel labelOn="On" labelOff="Off" />);

expect(checkbox.text()).toEqual('Off');
expect(checkbox.text()).toBe('Off');

checkbox.find('input').simulate('change');

expect(checkbox.text()).toEqual('On');
expect(checkbox.text()).toBe('On');
});
```

Expand Down
2 changes: 1 addition & 1 deletion docs/TutorialjQuery.md
Expand Up @@ -55,7 +55,7 @@ test('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($('#username').text()).toEqual('Johnny Cash - Logged In');
expect($('#username').text()).toBe('Johnny Cash - Logged In');
});
```

Expand Down
Expand Up @@ -32,7 +32,7 @@ exports[`print correct error message with nested test definitions outside descri
14 |
> 15 | test('inner test', () => {
| ^
16 | expect(getTruthy()).toEqual('This test should not have run');
16 | expect(getTruthy()).toBe('This test should not have run');
17 | });
18 | });
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/cliHandlesExactFilenames.test.ts
Expand Up @@ -47,5 +47,5 @@ test('CLI skips exact file names if no matchers matched', () => {

expect(result.exitCode).toBe(1);
expect(result.stdout).toMatch(/No tests found([\S\s]*)2 files checked./);
expect(result.stderr).toEqual('');
expect(result.stderr).toBe('');
});
2 changes: 1 addition & 1 deletion e2e/__tests__/config.test.ts
Expand Up @@ -73,5 +73,5 @@ test('negated flags override previous flags', () => {
'--silent',
]);

expect(globalConfig.silent).toEqual(true);
expect(globalConfig.silent).toBe(true);
});
12 changes: 6 additions & 6 deletions e2e/__tests__/crawlSymlinks.test.ts
Expand Up @@ -48,16 +48,16 @@ test('Node crawler picks up symlinked files when option is set as flag', () => {
'--no-watchman',
]);

expect(stdout).toEqual('');
expect(stdout).toBe('');
expect(stderr).toContain('Test Suites: 1 passed, 1 total');
expect(exitCode).toEqual(0);
expect(exitCode).toBe(0);
});

test('Node crawler does not pick up symlinked files by default', () => {
const {stdout, stderr, exitCode} = runJest(DIR, ['--no-watchman']);
expect(stdout).toContain('No tests found, exiting with code 1');
expect(stderr).toEqual('');
expect(exitCode).toEqual(1);
expect(stderr).toBe('');
expect(exitCode).toBe(1);
});

test('Should throw if watchman used with haste.enableSymlinks', () => {
Expand All @@ -71,13 +71,13 @@ test('Should throw if watchman used with haste.enableSymlinks', () => {

const {exitCode, stderr, stdout} = run1;

expect(stdout).toEqual('');
expect(stdout).toBe('');
expect(stderr).toMatchInlineSnapshot(`
"Validation Error:
haste.enableSymlinks is incompatible with watchman
Either set haste.enableSymlinks to false or do not use watchman"
`);
expect(exitCode).toEqual(1);
expect(exitCode).toBe(1);
});
2 changes: 1 addition & 1 deletion e2e/__tests__/doneInHooks.test.ts
Expand Up @@ -11,5 +11,5 @@ import runJest from '../runJest';
skipSuiteOnJasmine();
test('`done()` works properly in hooks', () => {
const {exitCode} = runJest('done-in-hooks');
expect(exitCode).toEqual(0);
expect(exitCode).toBe(0);
});
2 changes: 1 addition & 1 deletion e2e/__tests__/failureDetailsProperty.test.ts
Expand Up @@ -20,7 +20,7 @@ test('that the failureDetails property is set', () => {
]);

// safety check: if the reporter errors it'll show up here
expect(stderr).toStrictEqual('');
expect(stderr).toBe('');

const output = JSON.parse(removeStackTraces(stdout));

Expand Down
8 changes: 4 additions & 4 deletions e2e/__tests__/hasteMapSha1.test.ts
Expand Up @@ -66,13 +66,13 @@ test('exits the process after test are done but before timers complete', async (

// Ignored files do not get the SHA-1 computed.

expect(hasteFS.getSha1(path.join(DIR, 'fileWithExtension.ignored'))).toBe(
null,
);
expect(
hasteFS.getSha1(path.join(DIR, 'fileWithExtension.ignored')),
).toBeNull();

expect(
hasteFS.getSha1(
path.join(DIR, 'node_modules/bar/fileWithExtension.ignored'),
),
).toBe(null);
).toBeNull();
});
20 changes: 10 additions & 10 deletions e2e/__tests__/multiProjectRunner.test.ts
Expand Up @@ -200,8 +200,8 @@ test.each([{projectPath: 'packages/somepackage'}, {projectPath: 'packages/*'}])(
const {stdout, stderr, exitCode} = runJest(DIR, ['--no-watchman']);
expect(stderr).toContain('PASS somepackage packages/somepackage/test.js');
expect(stderr).toContain('Test Suites: 1 passed, 1 total');
expect(stdout).toEqual('');
expect(exitCode).toEqual(0);
expect(stdout).toBe('');
expect(exitCode).toBe(0);
},
);

Expand Down Expand Up @@ -250,8 +250,8 @@ test.each([
const {stdout, stderr, exitCode} = runJest(DIR, ['--no-watchman']);
expect(stderr).toContain(`PASS ${displayName} ${projectPath}/test.js`);
expect(stderr).toContain('Test Suites: 1 passed, 1 total');
expect(stdout).toEqual('');
expect(exitCode).toEqual(0);
expect(stdout).toBe('');
expect(exitCode).toBe(0);
},
);

Expand Down Expand Up @@ -284,8 +284,8 @@ test('projects can be workspaces with non-JS/JSON files', () => {
expect(stderr).toContain('PASS packages/project1/__tests__/file1.test.js');
expect(stderr).toContain('PASS packages/project2/__tests__/file2.test.js');
expect(stderr).toContain('Ran all test suites in 2 projects.');
expect(stdout).toEqual('');
expect(exitCode).toEqual(0);
expect(stdout).toBe('');
expect(exitCode).toBe(0);
});

test('objects in project configuration', () => {
Expand All @@ -310,8 +310,8 @@ test('objects in project configuration', () => {
expect(stderr).toContain('PASS __tests__/file1.test.js');
expect(stderr).toContain('PASS __tests__/file2.test.js');
expect(stderr).toContain('Ran all test suites in 2 projects.');
expect(stdout).toEqual('');
expect(exitCode).toEqual(0);
expect(stdout).toBe('');
expect(exitCode).toBe(0);
});

test('allows a single project', () => {
Expand All @@ -330,8 +330,8 @@ test('allows a single project', () => {
const {stdout, stderr, exitCode} = runJest(DIR, ['--no-watchman']);
expect(stderr).toContain('PASS __tests__/file1.test.js');
expect(stderr).toContain('Test Suites: 1 passed, 1 total');
expect(stdout).toEqual('');
expect(exitCode).toEqual(0);
expect(stdout).toBe('');
expect(exitCode).toBe(0);
});

test('resolves projects and their <rootDir> properly', () => {
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/runProgrammaticallyMultipleProjects.test.ts
Expand Up @@ -14,6 +14,6 @@ const dir = resolve(__dirname, '../run-programmatically-multiple-projects');
test('run programmatically with multiple projects', () => {
const {stderr, exitCode} = run('node run-jest.js', dir);
const {summary} = extractSummary(stripAnsi(stderr));
expect(exitCode).toEqual(0);
expect(exitCode).toBe(0);
expect(summary).toMatchSnapshot('summary');
});

0 comments on commit 8219820

Please sign in to comment.