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

feat: add toIncludeCaseInsensitive matcher #689

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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: 5 additions & 0 deletions .changeset/blue-sheep-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'jest-extended': minor
---

Introduce new matcher `toIncludeCaseInsensitive`
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ coverage
.vs
.vscode
package-lock.json
yarn.lock
1 change: 1 addition & 0 deletions src/matchers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export { toHaveBeenCalledBefore } from './toHaveBeenCalledBefore';
export { toHaveBeenCalledOnce } from './toHaveBeenCalledOnce';
export { toHaveBeenCalledExactlyOnceWith } from './toHaveBeenCalledExactlyOnceWith';
export { toInclude } from './toInclude';
export { toIncludeCaseInsensitive } from './toIncludeCaseInsensitive';
export { toIncludeAllMembers } from './toIncludeAllMembers';
export { toIncludeAllPartialMembers } from './toIncludeAllPartialMembers';
export { toIncludeAnyMembers } from './toIncludeAnyMembers';
Expand Down
23 changes: 23 additions & 0 deletions src/matchers/toIncludeCaseInsensitive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export function toIncludeCaseInsensitive(actual, expected) {
const { printReceived, printExpected, matcherHint } = this.utils;

const pass = String(actual).toLocaleLowerCase().includes(String(expected).toLocaleLowerCase());

return {
pass,
message: () =>
pass
? matcherHint('.not.toIncludeCaseInsensitive') +
'\n\n' +
'Expected string to not include while ignoring case:\n' +
` ${printExpected(expected)}\n` +
'Received:\n' +
` ${printReceived(actual)}`
: matcherHint('.toIncludeCaseInsensitive') +
'\n\n' +
'Expected string to include while ignoring case:\n' +
` ${printExpected(expected)}\n` +
'Received:\n' +
` ${printReceived(actual)}`,
};
}
28 changes: 28 additions & 0 deletions test/matchers/__snapshots__/toIncludeCaseInsensitive.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`.not.toIncludeCaseInsensitive fails when a string does have a given substring 1`] = `
"<dim>expect(</intensity><red>received</color><dim>).not.toIncludeCaseInsensitive(</intensity><green>expected</color><dim>)</intensity>

Expected string to not include while ignoring case:
<green>"ell"</color>
Received:
<red>"hello world"</color>"
`;

exports[`.toIncludeCaseInsensitive fails if string is not included despite case 1`] = `
"<dim>expect(</intensity><red>received</color><dim>).toIncludeCaseInsensitive(</intensity><green>expected</color><dim>)</intensity>

Expected string to include while ignoring case:
<green>"bbbb"</color>
Received:
<red>"aaaa"</color>"
`;

exports[`.toIncludeCaseInsensitive passes if string is included despite case 1`] = `
"<dim>expect(</intensity><red>received</color><dim>).toIncludeCaseInsensitive(</intensity><green>expected</color><dim>)</intensity>

Expected string to include while ignoring case:
<green>"bbbb"</color>
Received:
<red>"aaaa"</color>"
`;
29 changes: 29 additions & 0 deletions test/matchers/toIncludeCaseInsensitive.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as matcher from 'src/matchers/toIncludeCaseInsensitive';

expect.extend(matcher);

describe('.toIncludeCaseInsensitive', () => {
test('passes if string is included despite case', () => {
expect('a').toIncludeCaseInsensitive('A');
expect('aAA').toIncludeCaseInsensitive('aa');
expect('hello world').toIncludeCaseInsensitive('ell');
expect('HELLO world').toIncludeCaseInsensitive('ell');
expect('hello WORLD').toIncludeCaseInsensitive('ELL');
expect('HELLO WORLD').toIncludeCaseInsensitive('ELL');
expect(() => expect('aaaa').toIncludeCaseInsensitive('bbbb')).toThrowErrorMatchingSnapshot();
});

test('fails if string is not included despite case', () => {
expect(() => expect('aaaa').toIncludeCaseInsensitive('bbbb')).toThrowErrorMatchingSnapshot();
});
});

describe('.not.toIncludeCaseInsensitive', () => {
test('passes when a string does not have a given substring', () => {
expect('hello world').not.toIncludeCaseInsensitive('bob');
});

test('fails when a string does have a given substring', () => {
expect(() => expect('hello world').not.toIncludeCaseInsensitive('ell')).toThrowErrorMatchingSnapshot();
});
});
7 changes: 7 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,13 @@ interface CustomMatchers<R> extends Record<string, any> {
*/
toInclude(substring: string): R;

/**
* Use `.toIncludeCaseInsensitive` when checking if a `String` includes the given `String` substring, despite case.
*
* @param {String} substring
*/
toIncludeCaseInsensitive(substring: string): R;

/**
* Use `.toIncludeRepeated` when checking if a `String` includes the given `String` substring the correct number of times.
*
Expand Down
14 changes: 14 additions & 0 deletions website/docs/matchers/String.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ Use `.toInclude` when checking if a `String` includes the given `String` substri
});`}
</TestFile>

### .toIncludeCaseInsensitive(substring)

Use `.toIncludeCaseInsensitive` when checking if a `String` includes the given `String` substring, despite case.

<TestFile name="toIncludeCaseInsensitive">
{`test('passes if string is included despite case', () => {
expect('hello world').toIncludeCaseInsensitive('ell');
expect('HELLO world').toIncludeCaseInsensitive('ell');
expect('hello WORLD').toIncludeCaseInsensitive('ELL');
expect('HELLO WORLD').toIncludeCaseInsensitive('ELL');
expect('hello world').not.toIncludeCaseInsensitive('bob');
});`}
</TestFile>

### .toIncludeRepeated(substring, times)

Use `.toIncludeRepeated` when checking if a `String` includes the given `String` substring the correct number of times.
Expand Down
1 change: 1 addition & 0 deletions website/docs/matchers/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ sidebar_position: 1
- [.toStartWith(prefix)](/docs/matchers/string/#tostartwithprefix)
- [.toEndWith(suffix)](/docs/matchers/string/#toendwithsuffix)
- [.toInclude(substring)](/docs/matchers/string/#toincludesubstring)
- [.toIncludeCaseInsensitive(substring)](/docs/matchers/string/#toIncludeCaseInsensitivesubstring)
- [.toIncludeRepeated(substring, times)](/docs/matchers/string/#toincluderepeatedsubstring-times)
- [.toIncludeMultiple([substring])](/docs/matchers/string/#toincludemultiplesubstring)
- [.toEqualIgnoringWhitespace(string)](/docs/matchers/string/#toequalignoringwhitespacestring)
Expand Down