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

fix: handle inverse asymmetric matchers correctly #6272

Merged
merged 4 commits into from
May 27, 2018
Merged
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 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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know flow, so please tell me if this is wrong

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't have .not for any() and anything(). You think it's worth to add them?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will spreading work here?

// ...
   any(value: mixed): JestAsymmetricEqualityType,
  ...AsymmetricMatchers,
};

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thymikee .not.Any(Error) might be useful? Explode on errors, but whatever else is accepted?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, why not. We can leave this as a good first issue :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should open one up

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @rickhanlonii 😢


// 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