(Translated by https://www.hiragana.jp/)
GitHub - svenkatreddy/ultimate-chai: Chai library with cross mixture of sinon-chai, dirty-chai, chai-as-promised included
Skip to content

Chai library with cross mixture of sinon-chai, dirty-chai, chai-as-promised included

License

Notifications You must be signed in to change notification settings

svenkatreddy/ultimate-chai

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ultimate-chai

Build Status

NPM

chai with sinon-chai, dirty-chai, chai-as-promised with mixed features

Now supports Chai 4.x

Install

npm install ultimate-chai --save-dev

Usage

const chai = require('ultimate-chai');
const expect = chai.expect;
expect(true).to.be.true();

(or)

const expect = require('ultimate-chai').expect;
expect(true).to.be.true();

Dirty Chai Assertions

The following built-in assertions are modified by this plugin to now use the function-call form:

method assertion
ok expect('sample').to.be.ok()
true expect(true).to.be.true()
false expect(false).to.be.true()
null expect(null).to.be.null()
undefined expect(undefined).to.be.true()
exist expect('sample').to.exist()
empty expect('').to.empty()
arguments arguments()
Arguments Arguments()

Examples

it('should have ok method from dirty-chai', (done) => {
  expect('ok').to.be.ok();
  done();
});
it('should have false method from dirty-chai', (done) => {
  expect(true).to.be.true.and.not.false();
  done();
});
it('should have true method from dirty-chai', (done) => {
  expect(true).to.be.true();
  done();
});
it('should have null method from dirty-chai', (done) => {
  expect(null).to.be.null();
  done();
});
it('should have exist method from dirty-chai', (done) => {
  expect(undefined).to.be.undefined();
  done();
});
it('should have exist method from dirty-chai', (done) => {
  expect('test').to.exist();
  done();
});
it('should have empty method from dirty-chai', (done) => {
  expect('').to.be.empty();
  done();
});
it('should have empty method from dirty-chai', (done) => {
  expect('').to.be.empty();
  done();
});

chai-as-promised

Please note there is () invocation at end

return promise.should.be.fulfilled();
return promise.should.eventually.deep.equal("foo");
return promise.should.become("foo"); // same as `.eventually.deep.equal`
return promise.should.be.rejected();
return promise.should.be.rejectedWith(Error); // other variants of Chai's `throw` assertion work too.

Examples

const sampleSuccess = () => new Promise((resolve, reject) => {
     setTimeout(function(){
       resolve({foo: "bar"});
     },100); 
    });
    
    const sampleReject = () => new Promise((resolve, reject) => {
     setTimeout(function(){
       reject(new Error('error'));
     },100); 
    });
        
    it('should have eventually method from chai-as-promised', () => {
      return expect(sampleSuccess()).to.eventually.have.property("foo");
    });
    
    it('should have fulfilled method from chai-as-promised', () => {
      return expect(sampleSuccess()).to.be.fulfilled();
    });
    
    it('should have become method from chai-as-promised', () => {
      return expect(sampleSuccess()).to.become({foo: "bar"});
    });
    
    it('should have rejected method from chai-as-promised', () => {
      return expect(sampleReject()).to.be.rejected();
    });
    
    it('should have rejected method from chai-as-promised', () => {
      return expect(sampleReject()).to.be.rejectedWith(Error);
    });

Sinon.JS Assertions for Chai

please note the difference all methods ends with () invocation

All of your favorite Sinon.JS assertions made their way into Sinon–Chai. We show the should syntax here; the expect equivalent is also available.

Sinon.JS property/method Sinon–Chai assertion
called spy.should.have.been.called()
callCount spy.should.have.callCount(n)
calledOnce spy.should.have.been.calledOnce()
calledTwice spy.should.have.been.calledTwice()
calledThrice spy.should.have.been.calledThrice()
calledBefore spy1.should.have.been.calledBefore(spy2)
calledAfter spy1.should.have.been.calledAfter(spy2)
calledWithNew spy.should.have.been.calledWithNew()
alwaysCalledWithNew spy.should.always.have.been.calledWithNew()
calledOn spy.should.have.been.calledOn(context)
alwaysCalledOn spy.should.always.have.been.calledOn(context)
calledWith spy.should.have.been.calledWith(...args)
alwaysCalledWith spy.should.always.have.been.calledWith(...args)
calledWithExactly spy.should.have.been.calledWithExactly(...args)
alwaysCalledWithExactly spy.should.always.have.been.calledWithExactly(...args)
calledWithMatch spy.should.have.been.calledWithMatch(...args)
alwaysCalledWithMatch spy.should.always.have.been.calledWithMatch(...args)
returned spy.should.have.returned(returnVal)
alwaysReturned spy.should.have.always.returned(returnVal)
threw spy.should.have.thrown(errorObjOrErrorTypeStringOrNothing)
alwaysThrew spy.should.have.always.thrown(errorObjOrErrorTypeStringOrNothing)

examples

const testspy = sinon.spy();
    
    it('should have called method from sinon-chai', () => {
      testspy("foo");    
      expect(testspy).to.have.been.called();
    });
        
    it('should have calledWith method from sinon-chai', () => {
      testspy("foo");    
      expect(testspy).to.have.been.calledWith("foo");
    });
    
    it('should have calledWith method from sinon-chai', () => {
      const callCountSpy = sinon.spy();
      callCountSpy();
      callCountSpy('foo');
      callCountSpy('wow');
      expect(callCountSpy).to.have.callCount(3);
    });
    
    it('should have calledOnce method from sinon-chai', () => {
      const callCountSpy = sinon.spy();
      callCountSpy();
      expect(callCountSpy).to.have.been.calledOnce();
    });
    
    it('should have calledTwice method from sinon-chai', () => {
      const callCountSpy = sinon.spy();
      callCountSpy();
      callCountSpy('foo');
      expect(callCountSpy).to.have.been.calledTwice();
    });
    
    it('should have calledThrice method from sinon-chai', () => {
      const callCountSpy = sinon.spy();
      callCountSpy();
      callCountSpy('foo');
      callCountSpy('bar');
      expect(callCountSpy).to.have.been.calledThrice();
    });
    
    it('should have calledBefore method from sinon-chai', () => {
      const spy1 = sinon.spy();
      const spy2 = sinon.spy();
      spy1();
      spy2();
      expect(spy1).to.have.been.calledBefore(spy2);
    });
    
    it('should have calledBefore method from sinon-chai', () => {
      const spy1 = sinon.spy();
      const spy2 = sinon.spy();
      spy1();
      spy2();
      expect(spy1).to.have.been.calledBefore(spy2);
    });
    
    it('should have calledAfter method from sinon-chai', () => {
      const spy1 = sinon.spy();
      const spy2 = sinon.spy();
      spy1();
      spy2();
      expect(spy2).to.have.been.calledAfter(spy1);
    });
    
    it('should have calledAfter method from sinon-chai', () => {
      const spy = sinon.spy();
      new spy();
      expect(spy).to.have.been.calledWithNew();
    });
    
    it('should have alwaysCalledWithNew method from sinon-chai', () => {
      const spy = sinon.spy();
      new spy();
      new spy();
      expect(spy).to.always.have.been.calledWithNew();
    });

you still can use Chai 3.x with ultimate-chai 3.x

About

Chai library with cross mixture of sinon-chai, dirty-chai, chai-as-promised included

Resources

License

Stars

Watchers

Forks

Packages

No packages published