turbo87 / chai-roughly Goto Github PK
View Code? Open in Web Editor NEWdeep equals assertions with tolerance for chai
License: MIT License
deep equals assertions with tolerance for chai
License: MIT License
Great plugin! It would be excellent if it also worked with regular equals assertions.
expect(3.99999).to.roughly.equal(4)
--> true
When using .roughly()
, when a test (legitimately) fails, the error message is misleading. Here is an example:
const chai = require('chai');
const { expect } = chai;
chai.use(require('chai-roughly'));
describe('example test set', () => {
it('example test', () => {
const expected = { num1: 12.39197, num2: 5, num3: 19 };
const actual = { num1: 12.3919725, num2: 5, num3: 17 };
expect(actual).to.roughly(0.001).deep.equal(expected); // fails (which is correct)
});
});
The issue isn't that the test fails. The issue is that the failure message looks like this:
AssertionError: expected { num1: 12.3919725, num2: 5, num3: 17 } to roughly deeply equal { num1: 12.39197, num2: 5, num3: 19 }
+ expected - actual
{
- "num1": 12.3919725
+ "num1": 12.39197
"num2": 5
- "num3": 17
+ "num3": 19
}
This makes it look like the different values for num1
are also part of the reason why it failed, but the only reason it failed is that the num3
values are not within range. If you edit the num3
values to match, the test no longer fails.
Error raised for:
expect({ value: 0 }).to.roughly.deep.equal({ value: 1e-17 });
which is wrong since 1e-17 < 1e-6 (default tolerance)
We ran into some issues with our build today that came back chai-roughly depending on a forked version of deep-eql. Is it possible to un-fork deep-eql at this point? What was the reason for the fork?
cc @wittrock
Nice work on this! It's a feature I've always thought was missing from Chai.
In case there are other TypeScript users out there, you can get this to type-check in TS by putting this in a chai-roughly.d.ts
file somewhere:
declare module '~chai/lib/Assertion' {
export interface Assertion {
roughly: RoughAssertion;
}
interface RoughAssertion extends Assertion {
(tolerance: number): Assertion;
}
}
I need the same functionality but i need to use it with .include
"operator". Can you provide some guidance on how to do it ? thank you.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.