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

add inverse expect asymmetric matchers #5517

Merged
merged 6 commits into from
Mar 4, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
33 changes: 31 additions & 2 deletions docs/ExpectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,37 @@ test('prepareState prepares a valid state', () => {
The `expect.hasAssertions()` call ensures that the `prepareState` callback
actually gets called.

### `expect.not.arrayContaining(array)`

`expect.not.arrayContaining(array)` matches a received array which contains none of
the elements in the expected array. That is, the expected array **is not a subset**
of the received array.

It is the inverse of `expect.arrayContaining`.

### `expect.not.objectContaining(object)`

`expect.not.objectContaining(object)` matches any received object that does not recursively
match the expected properties. That is, the expected object **is not a subset** of
the received object. Therefore, it matches a received object which contains
properties that are **not** in the expected object.

It is the inverse of `expect.objectContaining`.

### `expect.not.stringContaining(string)`

`expect.not.stringContaining(string)` matches any received string that does not contain the
exact expected string.

It is the inverse of `expect.stringContaining`.

### `expect.not.stringMatching(regexp)`

`expect.not.stringMatching(regexp)` matches any received string that does not match the
expected regexp.

It is the inverse of `expect.stringMatching`.

### `expect.objectContaining(object)`

`expect.objectContaining(object)` matches any received object that recursively
Expand Down Expand Up @@ -302,8 +333,6 @@ test('onPress gets called with the right thing', () => {

### `expect.stringContaining(string)`

##### available in Jest **19.0.0+**

`expect.stringContaining(string)` matches any received string that contains the
exact expected string.

Expand Down
105 changes: 102 additions & 3 deletions packages/expect/src/__tests__/asymmetric_matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ const {
any,
anything,
arrayContaining,
arrayNotContaining,
objectContaining,
objectNotContaining,
stringContaining,
stringNotContaining,
stringMatching,
stringNotMatching,
} = require('../asymmetric_matchers');

test('Any.asymmetricMatch()', () => {
Expand Down Expand Up @@ -89,6 +93,27 @@ test('ArrayContaining throws for non-arrays', () => {
}).toThrow();
});

test('ArrayNotContaining matches', () => {
jestExpect(arrayNotContaining(['foo']).asymmetricMatch(['bar'])).toBe(true);
});

test('ArrayNotContaining does not match', () => {
[
arrayNotContaining([]).asymmetricMatch('jest'),
arrayNotContaining(['foo']).asymmetricMatch(['foo']),
arrayNotContaining(['foo']).asymmetricMatch(['foo', 'bar']),
arrayNotContaining([]).asymmetricMatch({}),
].forEach(test => {
jestExpect(test).toEqual(false);
});
});

test('ArrayNotContaining throws for non-arrays', () => {
jestExpect(() => {
arrayNotContaining('foo').asymmetricMatch([]);
}).toThrow();
});

test('ObjectContaining matches', () => {
[
objectContaining({}).asymmetricMatch('jest'),
Expand Down Expand Up @@ -139,32 +164,106 @@ test('ObjectContaining throws for non-objects', () => {
jestExpect(() => objectContaining(1337).asymmetricMatch()).toThrow();
});

test('ObjectNotContaining matches', () => {
[
objectNotContaining({}).asymmetricMatch('jest'),
objectNotContaining({foo: 'foo'}).asymmetricMatch({bar: 'bar'}),
objectNotContaining({foo: 'foo'}).asymmetricMatch({foo: 'foox'}),
objectNotContaining({foo: undefined}).asymmetricMatch({}),
].forEach(test => {
jestExpect(test).toEqual(true);
});
});

test('ObjectNotContaining does not match', () => {
[
objectNotContaining({foo: 'foo'}).asymmetricMatch({
foo: 'foo',
jest: 'jest',
}),
objectNotContaining({foo: undefined}).asymmetricMatch({foo: undefined}),
objectNotContaining({
first: objectNotContaining({second: {}}),
}).asymmetricMatch({first: {second: {}}}),
].forEach(test => {
jestExpect(test).toEqual(false);
});
});

test('ObjectNotContaining throws for non-objects', () => {
jestExpect(() => objectNotContaining(1337).asymmetricMatch()).toThrow();
});

test('StringContaining matches string against string', () => {
jestExpect(stringContaining('en*').asymmetricMatch('queen*')).toBe(true);
jestExpect(stringContaining('en').asymmetricMatch('queue')).toBe(false);
jestExpect(stringContaining('en').asymmetricMatch({})).toBe(false);
});

test('StringContaining throws for non-strings', () => {
jestExpect(() => {
stringContaining([1]).asymmetricMatch('queen');
}).toThrow();

jestExpect(() => {
stringContaining('en*').asymmetricMatch(1);
}).toThrow();
});

test('StringNotContaining matches string against string', () => {
jestExpect(stringNotContaining('en*').asymmetricMatch('queen*')).toBe(false);
jestExpect(stringNotContaining('en').asymmetricMatch('queue')).toBe(true);
});

test('StringNotContaining throws for non-strings', () => {
jestExpect(() => {
stringNotContaining([1]).asymmetricMatch('queen');
}).toThrow();

jestExpect(() => {
stringNotContaining('en*').asymmetricMatch(1);
}).toThrow();
});

test('StringMatching matches string against regexp', () => {
jestExpect(stringMatching(/en/).asymmetricMatch('queen')).toBe(true);
jestExpect(stringMatching(/en/).asymmetricMatch('queue')).toBe(false);
jestExpect(stringMatching(/en/).asymmetricMatch({})).toBe(false);
});

test('StringMatching matches string against string', () => {
jestExpect(stringMatching('en').asymmetricMatch('queen')).toBe(true);
jestExpect(stringMatching('en').asymmetricMatch('queue')).toBe(false);
jestExpect(stringMatching('en').asymmetricMatch({})).toBe(false);
});

test('StringMatching throws for non-strings and non-regexps', () => {
jestExpect(() => {
stringMatching([1]).asymmetricMatch('queen');
}).toThrow();
});

test('StringMatching throws for non-string actual values', () => {
jestExpect(() => {
stringMatching('en').asymmetricMatch(1);
}).toThrow();
});

test('StringNotMatching matches string against regexp', () => {
jestExpect(stringNotMatching(/en/).asymmetricMatch('queen')).toBe(false);
jestExpect(stringNotMatching(/en/).asymmetricMatch('queue')).toBe(true);
});

test('StringNotMatching matches string against string', () => {
jestExpect(stringNotMatching('en').asymmetricMatch('queen')).toBe(false);
jestExpect(stringNotMatching('en').asymmetricMatch('queue')).toBe(true);
});

test('StringNotMatching throws for non-strings and non-regexps', () => {
jestExpect(() => {
stringNotMatching([1]).asymmetricMatch('queen');
}).toThrow();
});

test('StringNotMatching throws for non-string actual values', () => {
jestExpect(() => {
stringNotMatching('en').asymmetricMatch(1);
}).toThrow();
});
17 changes: 16 additions & 1 deletion packages/expect/src/__tests__/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'use strict';

const {stringify} = require('jest-matcher-utils');
const {getObjectSubset, getPath} = require('../utils');
const {emptyObject, getObjectSubset, getPath} = require('../utils');

describe('getPath()', () => {
test('property exists', () => {
Expand Down Expand Up @@ -107,3 +107,18 @@ describe('getObjectSubset()', () => {
);
});
});

describe('emptyObject()', () => {
test('matches an empty object', () => {
expect(emptyObject({})).toBe(true);
});

test('does not match an object with keys', () => {
expect(emptyObject({foo: undefined})).toBe(false);
});

test('does not match a non-object', () => {
expect(emptyObject(null)).toBe(false);
expect(emptyObject(34)).toBe(false);
});
});
Loading