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

[Discussion] Expect only overrides error stack for built-in matchers #5162

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f39d750
Expect only overrides error stack for built-in matchers
bvaughn Dec 22, 2017
e7d9bbd
Fixed naggy lint issue
bvaughn Dec 22, 2017
9c10921
Replaced for...of with Object.keys().forEach()
bvaughn Dec 22, 2017
eb5d29c
Merged master
bvaughn Jan 2, 2018
ea088cf
Removed JestAssertionError export (as it is no longer needed)
bvaughn Jan 2, 2018
f2e7335
Added custom matcher test
bvaughn Jan 2, 2018
3e00753
Adjusted custom_matcher test to use prettified, non-absolute stack
bvaughn Jan 3, 2018
9e0cfae
test: refactor integration test
SimenB Jan 3, 2018
31f8e58
update with correct snapshot
SimenB Jan 3, 2018
c388a49
test: skip on windows
SimenB Jan 3, 2018
0c5c78c
Clarified comment
bvaughn Jan 3, 2018
5843ab5
Further clarified comment
bvaughn Jan 3, 2018
f902b4b
test: fix test for node 6
SimenB Jan 3, 2018
bd21922
test: move custom matcher stack to separate file to be able to skip i…
SimenB Jan 3, 2018
3ef09ea
Revert "test: fix test for node 6"
SimenB Jan 3, 2018
f88bf07
test: really fix node 6
SimenB Jan 3, 2018
ff50df6
Merge branch 'master' into override-error-stack-only-for-internal-mat…
SimenB Jan 4, 2018
2da0eee
docs: add package prefix to changelog
SimenB Jan 4, 2018
6f6dd91
Merge branch 'master' into override-error-stack-only-for-internal-mat…
bvaughn Jan 4, 2018
e8ab539
Reverted unrelated Markdown changes that had made their way into the …
bvaughn Jan 4, 2018
7dad466
Replaced '__jestInternal' string attribute with a Symbol
bvaughn Jan 4, 2018
179093c
Merge branch 'master' into override-error-stack-only-for-internal-mat…
bvaughn Jan 5, 2018
b8c6bfe
Updated snapshot
bvaughn Jan 5, 2018
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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* `[jest-config]` fix unexpected condition to avoid infinite recursion in
Windows platform. ([#5161](https://github.com/facebook/jest/pull/5161))
* Do not override `Error` stack (with `Error.captureStackTrace`) for custom matchers.
([#5162](https://github.com/facebook/jest/pull/5162))

### Features

Expand Down Expand Up @@ -1347,4 +1349,4 @@ See https://facebook.github.io/jest/blog/2016/12/15/2016-in-jest.html

## <=0.4.0

* See commit history for changes in previous versions of jest.
* See commit history for changes in previous versions of jest.
28 changes: 28 additions & 0 deletions integration_tests/__tests__/__snapshots__/stack_trace.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,31 @@ Time: <<REPLACED>>
Ran all test suites matching /runtime_error.test.js/i.
"
`;

exports[`works with custom matchers 1`] = `
"FAIL __tests__/custom_matcher.test.js
Custom matcher
✓ passes
✓ fails
✕ preserves error stack

● Custom matcher › preserves error stack

qux

44 | const bar = () => baz();
45 | const baz = () => {
> 46 | throw Error('qux');
47 | };
48 |
49 | expect(() => {

at __tests__/custom_matcher.test.js:46:13
at __tests__/custom_matcher.test.js:44:23
at __tests__/custom_matcher.test.js:43:23
at __tests__/custom_matcher.test.js:50:7
at __tests__/custom_matcher.test.js:11:18
at __tests__/custom_matcher.test.js:51:8
Copy link
Member

Choose a reason for hiding this comment

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

this is wrong, we have lost the trace

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, as of your commit.

Copy link
Member

Choose a reason for hiding this comment

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

Indeed. should be fixed now


"
`;
6 changes: 6 additions & 0 deletions integration_tests/__tests__/stack_trace.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,9 @@ describe('Stack Trace', () => {
);
});
});

test('works with custom matchers', () => {
const {stderr} = runJest('stack_trace', ['custom_matcher.test.js']);

expect(extractSummary(stderr).rest).toMatchSnapshot();
});
53 changes: 53 additions & 0 deletions integration_tests/stack_trace/__tests__/custom_matcher.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
'use strict';

function toCustomMatch(callback, expectation) {
const actual = callback();

if (actual !== expectation) {
return {
message: () => `Expected "${expectation}" but got "${actual}"`,
pass: false,
};
} else {
return {pass: true};
}
}

expect.extend({
toCustomMatch,
});

describe('Custom matcher', () => {
// This test is expected to pass
it('passes', () => {
expect(() => 'foo').toCustomMatch('foo');
});

// This test is expected to fail
Copy link
Member

Choose a reason for hiding this comment

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

it passes, is that an issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, my comment is perhaps confusing.

The "test" isn't expected to fail. The custom matcher assertion is expected to fail, which is why it's wrapped in an expect-to-throw check.

I'll update the comment. 😄

it('fails', () => {
expect(() => {
expect(() => 'foo').toCustomMatch('bar');
}).toThrow();
});

// This test fails due to an unrelated/unexpected error
// It will show a helpful stack trace though
it('preserves error stack', () => {
const foo = () => bar();
const bar = () => baz();
const baz = () => {
throw Error('qux');
};

expect(() => {
foo();
}).toCustomMatch('test');
});
});
32 changes: 30 additions & 2 deletions packages/expect/src/__tests__/stacktrace.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@
const jestExpect = require('../');

jestExpect.extend({
toCustomMatch(callback, expectation) {
const actual = callback();

if (actual !== expectation) {
return {
message: () => `Expected "${expectation}" but got "${actual}"`,
pass: false,
};
}

return {pass: true};
},
toMatchPredicate(received, argument) {
argument(received);
return {
Expand All @@ -22,7 +34,7 @@ it('stack trace points to correct location when using matchers', () => {
try {
jestExpect(true).toBe(false);
} catch (error) {
expect(error.stack).toContain('stacktrace.test.js:23');
expect(error.stack).toContain('stacktrace.test.js:35');
}
});

Expand All @@ -32,6 +44,22 @@ it('stack trace points to correct location when using nested matchers', () => {
jestExpect(value).toBe(false);
});
} catch (error) {
expect(error.stack).toContain('stacktrace.test.js:32');
expect(error.stack).toContain('stacktrace.test.js:44');
}
});

it('stack trace points to correct location when throwing from a custom matcher', () => {
try {
jestExpect(() => {
const foo = () => bar();
const bar = () => baz();
const baz = () => {
throw new Error('Expected');
};

foo();
}).toCustomMatch('bar');
} catch (error) {
expect(error.stack).toContain('stacktrace.test.js:57');
}
});
10 changes: 6 additions & 4 deletions packages/expect/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ const makeThrowingMatcher = (
result = matcher.apply(matcherContext, [actual].concat(args));
} catch (error) {
if (
matcher.__jestInternal === true &&
!(error instanceof JestAssertionError) &&
error.name !== 'PrettyFormatPluginError' &&
// Guard for some environments (browsers) that do not support this feature.
Expand Down Expand Up @@ -252,7 +253,8 @@ const makeThrowingMatcher = (
};
};

expect.extend = (matchers: MatchersObject): void => setMatchers(matchers);
expect.extend = (matchers: MatchersObject): void =>
setMatchers(matchers, false);

expect.anything = anything;
expect.any = any;
Expand Down Expand Up @@ -280,9 +282,9 @@ const _validateResult = result => {
};

// add default jest matchers
expect.extend(matchers);
expect.extend(spyMatchers);
expect.extend(toThrowMatchers);
setMatchers(matchers, true);
setMatchers(spyMatchers, true);
setMatchers(toThrowMatchers, true);

expect.addSnapshotSerializer = () => void 0;
expect.assertions = (expected: number) => {
Expand Down
9 changes: 8 additions & 1 deletion packages/expect/src/jest_matchers_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ export const setState = (state: Object) => {

export const getMatchers = () => global[JEST_MATCHERS_OBJECT].matchers;

export const setMatchers = (matchers: MatchersObject) => {
export const setMatchers = (matchers: MatchersObject, isInternal: boolean) => {
Object.keys(matchers).forEach(key => {
const matcher = matchers[key];
Object.defineProperty(matcher, '__jestInternal', {
Copy link
Member

Choose a reason for hiding this comment

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

Can we not use a Symbol instead to make this actually secret?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

value: isInternal,
});
});

Object.assign(global[JEST_MATCHERS_OBJECT].matchers, matchers);
};
1 change: 1 addition & 0 deletions types/Matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type RawMatcherFn = (
expected: any,
actual: any,
options: any,
__jestInternal?: boolean,
) => ExpectationResult;

export type ThrowingMatcherFn = (actual: any) => void;
Expand Down