Include toBeCloseTo in Jest .toMatchObject
I'm testing that an object matches a set of fields, but one of them is floating point and I need to use .toBeCloseTo. How can that be done within one expect?
expect(foo).toMatchObject({
bar: 'baz',
value: ???.toBeCloseTo(5), // TODO
});
I could use expect(foo.value).toBeCloseTo(5), but I don't want to break the logic into multiple expects, one for each floating point number.
javascript unit-testing jestjs
add a comment |
I'm testing that an object matches a set of fields, but one of them is floating point and I need to use .toBeCloseTo. How can that be done within one expect?
expect(foo).toMatchObject({
bar: 'baz',
value: ???.toBeCloseTo(5), // TODO
});
I could use expect(foo.value).toBeCloseTo(5), but I don't want to break the logic into multiple expects, one for each floating point number.
javascript unit-testing jestjs
expect(foo.value).toBeCloseTo(Math.round(foo.value));something like this, I guess.
– Jai
Nov 19 '18 at 6:37
add a comment |
I'm testing that an object matches a set of fields, but one of them is floating point and I need to use .toBeCloseTo. How can that be done within one expect?
expect(foo).toMatchObject({
bar: 'baz',
value: ???.toBeCloseTo(5), // TODO
});
I could use expect(foo.value).toBeCloseTo(5), but I don't want to break the logic into multiple expects, one for each floating point number.
javascript unit-testing jestjs
I'm testing that an object matches a set of fields, but one of them is floating point and I need to use .toBeCloseTo. How can that be done within one expect?
expect(foo).toMatchObject({
bar: 'baz',
value: ???.toBeCloseTo(5), // TODO
});
I could use expect(foo.value).toBeCloseTo(5), but I don't want to break the logic into multiple expects, one for each floating point number.
javascript unit-testing jestjs
javascript unit-testing jestjs
asked Nov 19 '18 at 6:31
Dan DascalescuDan Dascalescu
66.2k21199272
66.2k21199272
expect(foo.value).toBeCloseTo(Math.round(foo.value));something like this, I guess.
– Jai
Nov 19 '18 at 6:37
add a comment |
expect(foo.value).toBeCloseTo(Math.round(foo.value));something like this, I guess.
– Jai
Nov 19 '18 at 6:37
expect(foo.value).toBeCloseTo(Math.round(foo.value)); something like this, I guess.– Jai
Nov 19 '18 at 6:37
expect(foo.value).toBeCloseTo(Math.round(foo.value)); something like this, I guess.– Jai
Nov 19 '18 at 6:37
add a comment |
1 Answer
1
active
oldest
votes
Issue
The docs for toMatchObject states "You can match properties against values or against matchers".
Unfortunately, toBeCloseTo is not currently available as an asymmetric matcher, it looks like these are the only asymmetric matchers currently provided by Jest.
Solution
If you are using Jest v23 or higher you can create your own, essentially duplicating toBeCloseTo using expect.extend:
expect.extend({
toBeAround(actual, expected, precision = 2) {
const pass = Math.abs(expected - actual) < Math.pow(10, -precision) / 2;
if (pass) {
return {
message: () => `expected ${actual} not to be around ${expected}`,
pass: true
};
} else {
return {
message: () => `expected ${actual} to be around ${expected}`,
pass: false
}
}
}
});
const foo = {
bar: 'baz',
value: 4.9999
};
test('foo', () => {
expect(foo.value).toBeAround(5, 3); // SUCCESS in Jest > v20
expect(foo).toMatchObject({
bar: 'baz',
value: expect.toBeAround(5, 3) // SUCCESS only in Jest > v23
});
});
Note that expect.extend creates a matcher that can be used within functions like toMatchObject only in Jest v23 and higher.
Alternate Solution
From this post by a Jest collaborator: "Although it is implied but not currently documented, Jest assertions evaluate asymmetric matcher objects as defined in Jasmine".
An asymmetric matcher using the logic from toBeCloseTo can be created like this:
const closeTo = (expected, precision = 2) => ({
asymmetricMatch: (actual) => Math.abs(expected - actual) < Math.pow(10, -precision) / 2
});
const foo = {
bar: 'baz',
value: 4.9999
};
test('foo', () => {
expect(foo).toMatchObject({
bar: 'baz',
value: closeTo(5, 3) // SUCCESS
});
});
1
Interesting, iffalseis passed as the second argument on this line then Jest v23 creates asymmetric matchers for all of the built-in matchers making it possible to usetoBeCloseTowithintoMatchObject. I might open up an issue to ask whytrueis being passed there.
– brian-lives-outdoors
Nov 25 '18 at 5:05
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53369407%2finclude-tobecloseto-in-jest-tomatchobject%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Issue
The docs for toMatchObject states "You can match properties against values or against matchers".
Unfortunately, toBeCloseTo is not currently available as an asymmetric matcher, it looks like these are the only asymmetric matchers currently provided by Jest.
Solution
If you are using Jest v23 or higher you can create your own, essentially duplicating toBeCloseTo using expect.extend:
expect.extend({
toBeAround(actual, expected, precision = 2) {
const pass = Math.abs(expected - actual) < Math.pow(10, -precision) / 2;
if (pass) {
return {
message: () => `expected ${actual} not to be around ${expected}`,
pass: true
};
} else {
return {
message: () => `expected ${actual} to be around ${expected}`,
pass: false
}
}
}
});
const foo = {
bar: 'baz',
value: 4.9999
};
test('foo', () => {
expect(foo.value).toBeAround(5, 3); // SUCCESS in Jest > v20
expect(foo).toMatchObject({
bar: 'baz',
value: expect.toBeAround(5, 3) // SUCCESS only in Jest > v23
});
});
Note that expect.extend creates a matcher that can be used within functions like toMatchObject only in Jest v23 and higher.
Alternate Solution
From this post by a Jest collaborator: "Although it is implied but not currently documented, Jest assertions evaluate asymmetric matcher objects as defined in Jasmine".
An asymmetric matcher using the logic from toBeCloseTo can be created like this:
const closeTo = (expected, precision = 2) => ({
asymmetricMatch: (actual) => Math.abs(expected - actual) < Math.pow(10, -precision) / 2
});
const foo = {
bar: 'baz',
value: 4.9999
};
test('foo', () => {
expect(foo).toMatchObject({
bar: 'baz',
value: closeTo(5, 3) // SUCCESS
});
});
1
Interesting, iffalseis passed as the second argument on this line then Jest v23 creates asymmetric matchers for all of the built-in matchers making it possible to usetoBeCloseTowithintoMatchObject. I might open up an issue to ask whytrueis being passed there.
– brian-lives-outdoors
Nov 25 '18 at 5:05
add a comment |
Issue
The docs for toMatchObject states "You can match properties against values or against matchers".
Unfortunately, toBeCloseTo is not currently available as an asymmetric matcher, it looks like these are the only asymmetric matchers currently provided by Jest.
Solution
If you are using Jest v23 or higher you can create your own, essentially duplicating toBeCloseTo using expect.extend:
expect.extend({
toBeAround(actual, expected, precision = 2) {
const pass = Math.abs(expected - actual) < Math.pow(10, -precision) / 2;
if (pass) {
return {
message: () => `expected ${actual} not to be around ${expected}`,
pass: true
};
} else {
return {
message: () => `expected ${actual} to be around ${expected}`,
pass: false
}
}
}
});
const foo = {
bar: 'baz',
value: 4.9999
};
test('foo', () => {
expect(foo.value).toBeAround(5, 3); // SUCCESS in Jest > v20
expect(foo).toMatchObject({
bar: 'baz',
value: expect.toBeAround(5, 3) // SUCCESS only in Jest > v23
});
});
Note that expect.extend creates a matcher that can be used within functions like toMatchObject only in Jest v23 and higher.
Alternate Solution
From this post by a Jest collaborator: "Although it is implied but not currently documented, Jest assertions evaluate asymmetric matcher objects as defined in Jasmine".
An asymmetric matcher using the logic from toBeCloseTo can be created like this:
const closeTo = (expected, precision = 2) => ({
asymmetricMatch: (actual) => Math.abs(expected - actual) < Math.pow(10, -precision) / 2
});
const foo = {
bar: 'baz',
value: 4.9999
};
test('foo', () => {
expect(foo).toMatchObject({
bar: 'baz',
value: closeTo(5, 3) // SUCCESS
});
});
1
Interesting, iffalseis passed as the second argument on this line then Jest v23 creates asymmetric matchers for all of the built-in matchers making it possible to usetoBeCloseTowithintoMatchObject. I might open up an issue to ask whytrueis being passed there.
– brian-lives-outdoors
Nov 25 '18 at 5:05
add a comment |
Issue
The docs for toMatchObject states "You can match properties against values or against matchers".
Unfortunately, toBeCloseTo is not currently available as an asymmetric matcher, it looks like these are the only asymmetric matchers currently provided by Jest.
Solution
If you are using Jest v23 or higher you can create your own, essentially duplicating toBeCloseTo using expect.extend:
expect.extend({
toBeAround(actual, expected, precision = 2) {
const pass = Math.abs(expected - actual) < Math.pow(10, -precision) / 2;
if (pass) {
return {
message: () => `expected ${actual} not to be around ${expected}`,
pass: true
};
} else {
return {
message: () => `expected ${actual} to be around ${expected}`,
pass: false
}
}
}
});
const foo = {
bar: 'baz',
value: 4.9999
};
test('foo', () => {
expect(foo.value).toBeAround(5, 3); // SUCCESS in Jest > v20
expect(foo).toMatchObject({
bar: 'baz',
value: expect.toBeAround(5, 3) // SUCCESS only in Jest > v23
});
});
Note that expect.extend creates a matcher that can be used within functions like toMatchObject only in Jest v23 and higher.
Alternate Solution
From this post by a Jest collaborator: "Although it is implied but not currently documented, Jest assertions evaluate asymmetric matcher objects as defined in Jasmine".
An asymmetric matcher using the logic from toBeCloseTo can be created like this:
const closeTo = (expected, precision = 2) => ({
asymmetricMatch: (actual) => Math.abs(expected - actual) < Math.pow(10, -precision) / 2
});
const foo = {
bar: 'baz',
value: 4.9999
};
test('foo', () => {
expect(foo).toMatchObject({
bar: 'baz',
value: closeTo(5, 3) // SUCCESS
});
});
Issue
The docs for toMatchObject states "You can match properties against values or against matchers".
Unfortunately, toBeCloseTo is not currently available as an asymmetric matcher, it looks like these are the only asymmetric matchers currently provided by Jest.
Solution
If you are using Jest v23 or higher you can create your own, essentially duplicating toBeCloseTo using expect.extend:
expect.extend({
toBeAround(actual, expected, precision = 2) {
const pass = Math.abs(expected - actual) < Math.pow(10, -precision) / 2;
if (pass) {
return {
message: () => `expected ${actual} not to be around ${expected}`,
pass: true
};
} else {
return {
message: () => `expected ${actual} to be around ${expected}`,
pass: false
}
}
}
});
const foo = {
bar: 'baz',
value: 4.9999
};
test('foo', () => {
expect(foo.value).toBeAround(5, 3); // SUCCESS in Jest > v20
expect(foo).toMatchObject({
bar: 'baz',
value: expect.toBeAround(5, 3) // SUCCESS only in Jest > v23
});
});
Note that expect.extend creates a matcher that can be used within functions like toMatchObject only in Jest v23 and higher.
Alternate Solution
From this post by a Jest collaborator: "Although it is implied but not currently documented, Jest assertions evaluate asymmetric matcher objects as defined in Jasmine".
An asymmetric matcher using the logic from toBeCloseTo can be created like this:
const closeTo = (expected, precision = 2) => ({
asymmetricMatch: (actual) => Math.abs(expected - actual) < Math.pow(10, -precision) / 2
});
const foo = {
bar: 'baz',
value: 4.9999
};
test('foo', () => {
expect(foo).toMatchObject({
bar: 'baz',
value: closeTo(5, 3) // SUCCESS
});
});
edited Nov 25 '18 at 5:25
answered Nov 25 '18 at 5:05
brian-lives-outdoorsbrian-lives-outdoors
5,090322
5,090322
1
Interesting, iffalseis passed as the second argument on this line then Jest v23 creates asymmetric matchers for all of the built-in matchers making it possible to usetoBeCloseTowithintoMatchObject. I might open up an issue to ask whytrueis being passed there.
– brian-lives-outdoors
Nov 25 '18 at 5:05
add a comment |
1
Interesting, iffalseis passed as the second argument on this line then Jest v23 creates asymmetric matchers for all of the built-in matchers making it possible to usetoBeCloseTowithintoMatchObject. I might open up an issue to ask whytrueis being passed there.
– brian-lives-outdoors
Nov 25 '18 at 5:05
1
1
Interesting, if
false is passed as the second argument on this line then Jest v23 creates asymmetric matchers for all of the built-in matchers making it possible to use toBeCloseTo within toMatchObject. I might open up an issue to ask why true is being passed there.– brian-lives-outdoors
Nov 25 '18 at 5:05
Interesting, if
false is passed as the second argument on this line then Jest v23 creates asymmetric matchers for all of the built-in matchers making it possible to use toBeCloseTo within toMatchObject. I might open up an issue to ask why true is being passed there.– brian-lives-outdoors
Nov 25 '18 at 5:05
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53369407%2finclude-tobecloseto-in-jest-tomatchobject%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
expect(foo.value).toBeCloseTo(Math.round(foo.value));something like this, I guess.– Jai
Nov 19 '18 at 6:37