mcmath / chai-iterator Goto Github PK
View Code? Open in Web Editor NEWChai assertions for iterable objects
License: MIT License
Chai assertions for iterable objects
License: MIT License
Hi @mcmath, I just wanted to check as there have been no commits to master since march 2017 and no responce to issues or pull requests that have been around for over a month now.
It would be great to have chai-iterator work with chai@4 so I was wondering if you can find time to fix up and publish a new release to npm. If you are unable to maintain chai-iterator right now I would be very happy to help out in any way I can.
Cheers. ๐
iterable
must have an [@@iterator]()
method and its return value must be an iterator
.
iterator
must have an next()
method and its return value must be an iteratorResult
.
iteratorResult
is an object, it must have a boolean done
property, and when done
property is false, value
property must be present.
In other words, if chai really say iterable
, it should be possible to get iteratorResult
!
It is difficult to obtain iteratorResult
on the premise that there are no side effects. (Ex: queue, stack)
However, when calling [@@iterator]()
it is very uncommon case that there are side effects other than for debugging purposes.
if (!(value != null && typeof value[Symbol.iterator] === 'function')) {
return false;
}
const maybeIterator = value[Symbol.iterator]();
if (typeof maybeIterator !== 'object' || typeof maybeIterator.next !== 'function') {
return false;
}
return true;
Hi,
Thanks for this great package.
I'm trying to use it with TypeScript. Typings are installed and I'm importing it like this:
import * as chaiIterator from 'chai-iterator';
But it gives me the following error:
[ts] Module ''chai-iterator'' resolves to a non-module entity and cannot be imported using this construct.
Any ideas?
I cannot use require syntax.
This code:
import chai = require("chai");
import { expect } from 'chai';
import chaiIterator = require("chai-iterator");
chai.use(chaiIterator);
expect([2, 3, 5]).to.iterate.for.lengthOf(3);
Throws the following error.
AssertionError: expected undefined to be iterable
Here is a repo with reproduction: https://github.com/nunof07/chai-iterator-test
Can't quite figure out is going wrong. _obj
is undefined in Assertion.addProperty
.
Users should be able to test the length of an iterable, i.e., the number of values it yields before it is done. In particular, the following should be possible:
function* count(max) {
for (let i = 0; i < max; i++) {
yield i;
}
}
count(10).should.iterate.for.lengthOf(10);
count(10).should.iterate.for.length.above(9);
count(10).should.iterate.for.length.below(11);
count(10).should.iterate.for.length.of.at.least(10);
count(10).should.iterate.for.length.of.at.most(10);
count(10).should.iterate.for.length.within(9, 10);
Equivalent assertions should be available using the assert
style.
These assertions should be implemented in such a way that the iteration stops as soon as possible, so that even infinite iterators may be tested for length.
Created a class for nodejs, and a vanilla @@iterator implementation according to the reference :
class DStorage {
//...
[Symbol.iterator]() {
const ctx = this
return {
next: () => {
if (typeof this._current === 'undefined' || typeof this._current.value === 'undefined') return {done: true}
this._current = this._current.next
return {value: this._current.value, done: false}
},
_current: ctx.root
}
}
When testing this with chai-iterator the result was:
TypeError: iterable[Symbol.iterator] is not a function
To overcome this issue it turns out that I have to either 1) rewrite the above implementation in a generator form (*[Symbol.iterator]
) or 2) I have to add unnecessary extra @@iterator like this:
class DStorage {
//...
[Symbol.iterator]() {
const ctx = this
return {
next: () => {
if (typeof this._current === 'undefined' || typeof this._current.value === 'undefined') return {done: true}
this._current = this._current.next
return {value: this._current.value, done: false}
},
_current: ctx.root,
[Symbol.iterator]() { return this; }
}
}
The extra [Symbol.iterator]
within the iterator is not required or even directly mentioned in the iterable protocol reference.
I'm using chai
version 4.1.2
. With node
version 8.9.1
LTS.
The following program runs just fine:
const chai = require('chai');
chai.assert.isObject({});
but once the chai-iterator library is in use
const chai = require('chai');
const chaiIterator = require('chai-iterator');
chai.use(chaiIterator);
chai.assert.isObject({});
the following error appears:
/Users/mr6502/projects/project/node_modules/chai/lib/chai/assertion.js:141
throw new AssertionError(msg, {
^
AssertionError: expected undefined to be an object
at Function.assert.isObject (/Users/mr6502/projects/project/node_modules/chai/lib/chai/interface/assert.js:602:58)
at Object.<anonymous> (/Users/mr6502/projects/project/a.txt:4:13)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Function.Module.runMain (module.js:676:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
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.