Skip to content

Commit

Permalink
fix: handle inverse asymmetric matchers correctly (#6272)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored and thymikee committed May 27, 2018
1 parent 40d15f9 commit f976da6
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## master

### Fixes

* `[pretty-format]` Serialize inverse asymmetric matchers correctly
([#6272](https://github.com/facebook/jest/pull/6272))

## 23.0.0

### Features
Expand Down
16 changes: 10 additions & 6 deletions flow-typed/npm/jest_v21.x.x.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,14 @@ declare var xit: typeof it;
/** A disabled individual test */
declare var xtest: typeof it;

type AsymmetricMatchers = {
arrayContaining(value: Array<mixed>): void,
objectContaining(value: Object): void,
/** Matches any received string that contains the exact expected string. */
stringContaining(value: string): void,
stringMatching(value: string | RegExp): void,
}

/** The expect function is used every time you want to test a value */
declare var expect: {
/** The object that you want to make assertions against */
Expand All @@ -549,12 +557,8 @@ declare var expect: {
hasAssertions(): void,
any(value: mixed): JestAsymmetricEqualityType,
anything(): void,
arrayContaining(value: Array<mixed>): void,
objectContaining(value: Object): void,
/** Matches any received string that contains the exact expected string. */
stringContaining(value: string): void,
stringMatching(value: string | RegExp): void,
};
not: AsymmetricMatchers,
} & AsymmetricMatchers;

// TODO handle return type
// http://jasmine.github.io/2.4/introduction.html#section-Spies
Expand Down
28 changes: 28 additions & 0 deletions packages/pretty-format/src/__tests__/asymmetric_matcher.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,41 @@ test(`arrayContaining()`, () => {
]`);
});

test(`arrayNotContaining()`, () => {
const result = prettyFormat(expect.not.arrayContaining([1, 2]), options);
expect(result).toEqual(`ArrayNotContaining [
1,
2,
]`);
});

test(`objectContaining()`, () => {
const result = prettyFormat(expect.objectContaining({a: 'test'}), options);
expect(result).toEqual(`ObjectContaining {
"a": "test",
}`);
});

test(`objectNotContaining()`, () => {
const result = prettyFormat(
expect.not.objectContaining({a: 'test'}),
options,
);
expect(result).toEqual(`ObjectNotContaining {
"a": "test",
}`);
});

test(`stringContaining(string)`, () => {
const result = prettyFormat(expect.stringContaining('jest'), options);
expect(result).toEqual(`StringContaining "jest"`);
});

test(`not.stringContaining(string)`, () => {
const result = prettyFormat(expect.not.stringContaining('jest'), options);
expect(result).toEqual(`StringNotContaining "jest"`);
});

test(`stringMatching(string)`, () => {
const result = prettyFormat(expect.stringMatching('jest'), options);
expect(result).toEqual('StringMatching /jest/');
Expand All @@ -105,6 +128,11 @@ test(`stringMatching(regexp) {escapeRegex: true}`, () => {
expect(result).toEqual('StringMatching /regexp\\\\d/gi');
});

test(`stringNotMatching(string)`, () => {
const result = prettyFormat(expect.not.stringMatching('jest'), options);
expect(result).toEqual('StringNotMatching /jest/');
});

test(`supports multiple nested asymmetric matchers`, () => {
const result = prettyFormat(
{
Expand Down
20 changes: 16 additions & 4 deletions packages/pretty-format/src/plugins/asymmetric_matcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export const serialize = (
): string => {
const stringedValue = val.toString();

if (stringedValue === 'ArrayContaining') {
if (
stringedValue === 'ArrayContaining' ||
stringedValue === 'ArrayNotContaining'
) {
if (++depth > config.maxDepth) {
return '[' + stringedValue + ']';
}
Expand All @@ -37,7 +40,10 @@ export const serialize = (
);
}

if (stringedValue === 'ObjectContaining') {
if (
stringedValue === 'ObjectContaining' ||
stringedValue === 'ObjectNotContaining'
) {
if (++depth > config.maxDepth) {
return '[' + stringedValue + ']';
}
Expand All @@ -57,15 +63,21 @@ export const serialize = (
);
}

if (stringedValue === 'StringMatching') {
if (
stringedValue === 'StringMatching' ||
stringedValue === 'StringNotMatching'
) {
return (
stringedValue +
SPACE +
printer(val.sample, config, indentation, depth, refs)
);
}

if (stringedValue === 'StringContaining') {
if (
stringedValue === 'StringContaining' ||
stringedValue === 'StringNotContaining'
) {
return (
stringedValue +
SPACE +
Expand Down

0 comments on commit f976da6

Please sign in to comment.