ethworks / bn-chai Goto Github PK
View Code? Open in Web Editor NEWBN chai extends Chai with bn.js operations.
License: MIT License
BN chai extends Chai with bn.js operations.
License: MIT License
Using chai.Assertion.overwriteMethod()
it is possible to extend methods like equal
, so that we could write
let a = new BN(1);
let b = new BN(1);
expect(a).to.equal(b); //or
a.should.equal(b);
I wrote a minimum viable plugin:
module.exports = function(BN) {
return function (chai, utils) {
chai.Assertion.overwriteMethod('equal', function(_super) {
return function(exp) {
var obj = utils.flag(this, 'object');
if (BN.isBN(obj)) {
chai.assert(obj.eq(exp), "BNs don't equal.");
} else {
_super.apply(this, arguments);
}
}
});
};
};
This extends equal
to BN
s. However, I don't know how to display the message that was passed and, ideally, to show a diff.
It may also make sense to wrap the expected exp
into a new BN(exp)
to be more forgiving when passing non-BN
s.
Probably, it is cleaner to use something like let test = new Assertion(...); test.assert(...)
, which I saw in the chai sourcecode, instead of chai.assert()
. However, I didn't understand yet how to fill those ...
s.
I am using this library after following the upgrade guidelines for the new truffle V5 release, see here: https://github.com/trufflesuite/truffle/releases?after=truffle-code-utils%401.1.2-beta.0
Example code:
const bnChai = require('bn-chai');
// Setup should and bnChai as a plugin
require('chai')
.use(require('chai-as-promised'))
.use(bnChai(web3.utils.BN)) // web3 is provided by the test runner in truffle
.should();
// sample assertion
myValue.should.be.eq.BN(0);
// gives the following failure: AssertionError: expected 0 to be below 0
When I one of my tests it always outs the following AssertionError: expected 0 to be below 0
Am I using it wrong?
All the equality assertions come out the same way by looking ?
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.