jasmine2chai
Introduction
Are you migrating your test suite from Jasmine to Mocha? Are you using chai as your assertion library in Mocha?
While Jasmine and Mocha have a very similar test and assertion syntax, they're just different enough to not work out of the box with each other.
Jasmine2Chai uses jscodeshift to (as accurately as possible) transform your test code into something that is compatible with chai.js assertions, while leaving your code style alone.
Installation
npm install -g jasmine2chai
Usage
Print out a converted file to the terminal (stdout):
jasmine2chai myfile.js
Translations
toBe -> to.be
Jasmine assertion:
expect('taco').toBe('taco');
chai assertion:
expect('taco').to.be('taco');
toEqual -> to.equal
Jasmine assertion:
expect('taco').toEqual('taco');
chai assertion:
expect('taco').to.equal('taco');
toMatch -> to.match
Jasmine assertion:
expect('taco').toMatch(/taco/)
chai assertion:
expect('taco').to.match(/taco/);
toBeDefined -> not.to.be.undefined
Jasmine assertion:
expect('taco').toBeDefined();
chai assertion:
expect('taco').to.not.be.undefined;
toBeUndefined -> to.be.undefined
Jasmine assertion:
expect(undefined).toBeUndefined();
chai assertion:
expect(undefined).to.be.undefined;
toBeNull -> to.be.null
Jasmine assertion:
expect(null).toBeNull();
chai assertion:
expect(null).to.be.null;
toBeTruthy -> to.be.ok
Jasmine assertion:
expect(true).toBeTruthy();
chai assertion:
expect(true).to.be.ok;
toBeFalsy -> to.not.be.ok
Jasmine assertion:
expect(false).toBeFalsy();
chai assertion:
expect(false).to.not.be.ok;
toContain -> to.include
Jasmine assertion:
expect([1, 2, 3]).toContain(1);
chai assertion:
expect([1, 2, 3]).to.include(1);
toBeLessThan -> to.be.below
Jasmine assertion:
expect(1).toBeLessThan(2);
chai assertion:
expect(1).to.be.below(2);
toBeGreaterThan -> to.be.above;
Jasmine assertion:
expect(2).toBeGreaterThan(1);
chai assertion:
expect(2).to.be.above(1);
toBeCloseTo -> to.be.closeTo
Jasmine assertion:
expect(2).toBeCloseTo(1,1);
chai assertion:
expect(2).to.be.closeTo(1,1);
toThrow -> to.throw
Jasmine assertion:
expect(function() { throw new Error('hi')).toThrow();
chai assertion:
expect(function() { throw new Error('hi')).to.throw();
toThrowError -> to.throw
Jasmine assertion:
expect(foo).toThrowError("foo bar baz");
expect(foo).toThrowError(/bar/);
expect(foo).toThrowError(TypeError);
expect(foo).toThrowError(TypeError, "foo bar baz");
chai assertion:
expect(foo).to.throw("foo bar baz");
expect(foo).to.throw(/bar/);
expect(foo).to.throw(TypeError);
expect(foo).to.throw(TypeError, "foo bar baz");
Spy Migrations
Important Note:
You'll need the sinon-chai
plugins for this to work. In addition, spyOn
calls are not yet
translated. This only translates the assertions on spies.
toHaveBeenCalled -> to.have.been.called
Jasmine assertion:
expect(foo.setBar).toHaveBeenCalled();
chai assertion:
expect(foo.setBar).to.have.been.called;
toHaveBeenCalledWith -> to.have.been.calledWith
Jasmine assertion:
expect(foo.setBar).toHaveBeenCalledWith(123);
expect(foo.setBar).toHaveBeenCalledWith(456, 'another param');
chai assertion:
expect(foo.setBar).to.have.been.calledWith(123);
expect(foo.setBar).to.have.been.calledWith(456, 'another param');
toHaveBeenCalledTimes -> to.have.callCount
Jasmine assertion:
expect(foo.setBar).toHaveBeenCalledTimes(2);
chai assertion:
expect(foo.setBar).to.have.callCount(2);