diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b0847c078ec..d6d834363e51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/flow-typed/npm/jest_v21.x.x.js b/flow-typed/npm/jest_v21.x.x.js index 50dc49e23b3c..3ffc4de6720c 100644 --- a/flow-typed/npm/jest_v21.x.x.js +++ b/flow-typed/npm/jest_v21.x.x.js @@ -537,6 +537,14 @@ declare var xit: typeof it; /** A disabled individual test */ declare var xtest: typeof it; +type AsymmetricMatchers = { + arrayContaining(value: Array): 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 */ @@ -549,12 +557,8 @@ declare var expect: { hasAssertions(): void, any(value: mixed): JestAsymmetricEqualityType, anything(): void, - arrayContaining(value: Array): 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 diff --git a/packages/pretty-format/src/__tests__/asymmetric_matcher.test.js b/packages/pretty-format/src/__tests__/asymmetric_matcher.test.js index a5a38aafcc90..3bb6c4005cbd 100644 --- a/packages/pretty-format/src/__tests__/asymmetric_matcher.test.js +++ b/packages/pretty-format/src/__tests__/asymmetric_matcher.test.js @@ -72,6 +72,14 @@ 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 { @@ -79,11 +87,26 @@ test(`objectContaining()`, () => { }`); }); +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/'); @@ -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( { diff --git a/packages/pretty-format/src/plugins/asymmetric_matcher.js b/packages/pretty-format/src/plugins/asymmetric_matcher.js index 4c92b6c87ab6..7cdb099fc30e 100644 --- a/packages/pretty-format/src/plugins/asymmetric_matcher.js +++ b/packages/pretty-format/src/plugins/asymmetric_matcher.js @@ -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 + ']'; } @@ -37,7 +40,10 @@ export const serialize = ( ); } - if (stringedValue === 'ObjectContaining') { + if ( + stringedValue === 'ObjectContaining' || + stringedValue === 'ObjectNotContaining' + ) { if (++depth > config.maxDepth) { return '[' + stringedValue + ']'; } @@ -57,7 +63,10 @@ export const serialize = ( ); } - if (stringedValue === 'StringMatching') { + if ( + stringedValue === 'StringMatching' || + stringedValue === 'StringNotMatching' + ) { return ( stringedValue + SPACE + @@ -65,7 +74,10 @@ export const serialize = ( ); } - if (stringedValue === 'StringContaining') { + if ( + stringedValue === 'StringContaining' || + stringedValue === 'StringNotContaining' + ) { return ( stringedValue + SPACE +