Assume is an expect inspired assertion library who's sole purpose is to create
a working and human readable assert library for browsers and node. The library
is designed to work with different assertion styles.
I've been trying out a lot of libraries over the last couple of years and none
of the assertion libraries that I found "tickled my fancy". They either only
worked in node or had really bad browser support. I wanted something that I can
use starting from Internet Explorer 5 to the latest version while maintaining
the expect like API that we all know and love. Writing tests should be dead
simple and not cause any annoyances. This library attempts to achieve all of
this.
Assume is written with client and server-side JavaScript in mind and uses the commonjs module system to export it self. The library is released in the public npm registry and can be installed using:
npm install --save-dev assume
The --save-dev flag tells npm to automatically add this package.json and it's
installed version to the devDependencies of your module.
As code is written against the commonjs module system we also ship a standalone
version in the module which introduces an assume global. The standalone
version can be found in the dist folder after installation. The dist file is
not commited to GitHub.
We support a lot of different syntaxes and assertion styles. The only thing we
will no (read never) support is the should syntax as we will never extend
build-in objects/primitives of JavaScript.
The default syntax that we support is modeled after expect so you can replace
any assertion library which implements this API by simply changing the require
to:
var expect = require('assume');
expect('foo').equals('foo');
expect('foo').is.a('string');
As you can see in the example above the is property is used to make the
assertion more readable. We support the following aliases which allow these kind
of chains:
tobebeeniswasandhashavehadwiththatatofsomedoesdiditselfwhichSo you can just write:
assume(100).is.at.most(100);
But do note that these aliases are optionally so the above example can also be written as:
assume(100).most(1000);
The module can be configured globally be changing the properties on the config
object:
var assume = require('assume');
assume.config.includeStack = false;
Or locally for each assertions by supplying the assume function with an
optional configuration object:
assume('foo', { includeStack: false }).is.a('buffer');
The following options can be configured:
includeStack Should we output a stack trace. Defaults to true.showDIff Show difference between the given and expected values. Defaults
to true.Certain assertions only work in certain JavaScript/EcmaScript environments.
Things like the generator assertions only work in ES6 as the function * is
invalid syntax. The results of the feature detection is publicly stored in the
assume.supports object. You can use this object to add some conditional tests
to your test suite. The following features are currently detected:
if (assume.supports.native) {
it('does things', function () {
..
});
}
If you are a plugin author, feel free to add your own feature detections to this object (as long as you do not override any pre-existing values).
The performance testing is only available for environments that use V8 and more
specifically the --allow-natives-syntax flags. These flags can be supplied in
[chrome before you start browser][flags]. These flags are necessary to get
access to the V8 internals which expose optimization and de-optimization
information.
If you are running iojs or node on the server, you can pass in these flags
directly:
iojs --allow-natives-syntax
If you are using mocha as test runner you usually add mocha as executable.
But unfortunately, the mocha binary doesn't allow you to pass V8 flags. So
instead of using the mocha binary directly you can use the node and call the
_mocha binary instead:
node --allow-natives-syntax --harmony ./node_modules/mocha/bin/_mocha test/test.js
You can check if your host environment supports these performance tests by
checking the assume.supports.native variable.
There are various of assertions available in assume. If you want the failed assertion to include a custom message or reason you can always add this as last argument of the assertion function.
assume(true).is.false('how odd, true is not a false');
The behaviours of the assertions can be chained using special "flags" or "prefixes". We currently support the following prefixes.
.not, .doesnt, .dont Instead of assuming that your assertions assert to
true they will now assert for false..deep, .deeply, .strict .strictly Instructs the assertions to do a
deep equal, so it checks if the contents match instead of an object it
self matches.For example:
assume(false).is.not.true();
assume({foo:'bar'}).deep.equals({foo:'bar'});
Now, a special word of caution for those of you who are using this library to
write cross browser tests. Internet Explorer has issues when you use
keywords as functions. Using the true(), instanceof() etc. functions to
assert you will run in to issues. So the rule of thumb here is that if you need
to do cross browser support do not assert with the keyword based names.
Let's take a closer look to all assertions that we're supporting:
Asserts if the given value is the correct type. We need to use Object.toString
here because there are some implementation bugs the typeof operator:
functionAs well as all common flaws like Arrays being seen as Objects etc. This eliminates all these edge cases.
assume([]).is.a('array');
instanceof is a keyword and might cause cross browser issues
Asserts if the given value is one of the acceptable types.
The same caveats regarding typeof apply as described in a, an.
assume([]).is.oneOfType(['array', 'string']);
Asserts that the value is instanceof the given constructor.
function Classy() {}
var classes = new Classy();
assume(classes).is.an.instanceOf(Classy);
Assert that value includes a given value. I know this sounds vague but an example might be more useful here. It can check this for strings, objects and arrays.
assume({foo: 'bar'}).contains('foo');
assume('hello world').includes('world');
assume([1,3,4]).contains(1);
Assert that the value is truthy.
assume(1).is.ok();
assume(0).is.not.ok();
assume(true).is.ok();
Assert that the value is falsey.
assume(0).is.falsely();
assume(true).is.not.falsey();
assume(null).is.falsely;
Explicitly check that the value is the boolean true.
assume(true).true();
true is a keyword and might cause cross browser issues
Explicitly check that the value is the boolean false.
assume(false).false();
false is a keyword and might cause cross browser issues
Check if the value not not null.
assume('hello').exists();
assume(undefined).exists(); // throws
Assert if the given value has the given length. It accepts arrays, strings,
functions, object and anything else that has a .length property.
assume({ foo: 'bar' }).has.length(1);
assume([1,2,3,4,5,6]).is.size(6)
Short hand function for assume(val).length(0) so it can check if objects,
arrays, strings are empty.
assume([]).empty();
assume('').empty();
assume({}).empty();
//
// Also works against everything that has a `.length` property
//
localStorage.clear();
assume(localStorage).is.empty();
Assert if the value is above the given value. If you need greater or equal check
out the least method. If value to assert is not a number we automatically
extract the length out of it so you can use it check the length of arrays etc.
assume(100).is.above(10);
Assert if the value is above or equal to the given value. If you just need
greater check out the above method. If value to assert is not a number we
automatically extract the length out of it so you can use it check the length of
arrays etc.
assume(100).is.least(10);
assume(100).is.least(100);
Assert if the value is less than the given value. If you need less or equal
check out the most method. If value to assert is not a number we automatically
extract the length out of it so you can use it check the length of arrays etc.
assume(10).is.below(100);
Assert if the value is less or equal to the given value. If you just need less,
check out the less method. If value to assert is not a number we automatically
extract the length out of it so you can use it check the length of arrays etc.
assume(10).is.most(100);
assume(100).is.most(100);
Check if the value is between or equal to a given range. If value to assert is not a number we automatically extract the length out of it so you can use it check the length of arrays etc.
assume(100).is.between(90, 100);
assume([1, 213, 13, 94, 5, 6, 7]).is.between(2, 10);
Assert that the value has the specified property and optionally deeply check its value.
assume({foo: 'bar'}).owns('foo');
assume({foo: 'bar'}).owns('foo', 'bar');
Matches the value against a given Regular Expression. If a string is given
instead of an actual Regular Expression we automatically transform it to an new
RegExp.
assume('hello world').matches(/world$/);
Assert that given value is strictly (===) equal to the supplied value.
assume('foo').equals('foo');
assume(13424).equals(13424);
Assert that the given value deeply equals the supplied value.
assume([1,2]).eql([1,2]);
Assert that the value is either one of the values of the given array. It can be
prefixed with .deep for deep assertions.
```js assume('foo').is.either(['bar', 'banana', 'foo']); assume({ foo: 'bar' }).is.either(['bar', 'banana',
$ claude mcp add assume \
-- python -m otcore.mcp_server <graph>