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

Pass expected and actual objects from matchers and custom errors from jasmine #2198

Merged
merged 2 commits into from
Dec 8, 2016
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
2 changes: 1 addition & 1 deletion integration_tests/__tests__/json-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ test('JSON is available in the global scope', () => {
});

test('JSON.parse creates objects from within this context', () => {
expect(JSON.parse("{}").constructor).toBe(Object);
expect(JSON.parse('{}').constructor).toBe(Object);
});
4 changes: 3 additions & 1 deletion packages/jest-jasmine2/vendor/jasmine-2.5.2.js
Original file line number Diff line number Diff line change
Expand Up @@ -1612,7 +1612,9 @@ getJasmineRequireObj().buildExpectationResult = function() {
matcherName: options.matcherName,
message: message(),
stack: stack(),
passed: options.passed
passed: options.passed,
// CUSTOM JEST CHANGE: we pass error message to the result.
error: options.error
Copy link
Collaborator

Choose a reason for hiding this comment

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

Anything added to the Jasmine fork should have an appropriate comment, so it's easier to migrate to newer versions later, e.g.:

// CUSTOM JEST CHANGE: we pass error message to the result.
error: options.error

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@thymikee Done.

};

if(!result.passed) {
Expand Down
11 changes: 7 additions & 4 deletions packages/jest-matchers/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@ const utils = require('jest-matcher-utils');

const GLOBAL_STATE = Symbol.for('$$jest-matchers-object');

class JestAssertionError extends Error {}
class JestAssertionError extends Error {
matcherResult: any
}

if (!global[GLOBAL_STATE]) {
Object.defineProperty(
global,
GLOBAL_STATE,
{value: {
matchers: Object.create(null),
matchers: Object.create(null),
state: {
assertionsExpected: null,
assertionsMade: 0,
assertionsExpected: null,
assertionsMade: 0,
suppressedErrors: [],
},
}},
Expand Down Expand Up @@ -114,6 +116,7 @@ const makeThrowingMatcher = (
if ((result.pass && isNot) || (!result.pass && !isNot)) { // XOR
const message = getMessage(result.message);
const error = new JestAssertionError(message);
error.matcherResult = result;
// Remove this function from the stack trace frame.
Error.captureStackTrace(error, throwingMatcher);

Expand Down
4 changes: 2 additions & 2 deletions packages/jest-matchers/src/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const matchers: MatchersObject = {
(diffString ? `\n\nDifference:\n\n${diffString}` : '');
};

return {message, pass};
return {actual: received, expected, message, name: 'toBe', pass};
},

toBeCloseTo(
Expand Down Expand Up @@ -396,7 +396,7 @@ const matchers: MatchersObject = {
(diffString ? `\n\nDifference:\n\n${diffString}` : '');
};

return {message, pass};
return {actual: received, expected, message, name: 'toEqual', pass};
},

toHaveLength(received: any, length: number) {
Expand Down