Skip to content
This repository has been archived by the owner on Jun 9, 2020. It is now read-only.

Commit

Permalink
Merge pull request #6 from Turbo87/qunit
Browse files Browse the repository at this point in the history
Add QUnit integration
  • Loading branch information
Turbo87 authored Nov 26, 2016
2 parents cbcc240 + 6529b27 commit f184d0d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
34 changes: 34 additions & 0 deletions addon/qunit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { expect, util } from 'chai';

/**
* Returns a function that calls the passed in function with a wrapped
* expect() and the original `assert` as parameters.
*/
export function withChai(fn) {
return function(assert) {

function _expect(...args) {
let assertion = expect(...args);
let originalAssert = assertion.assert;
assertion.assert = function(...args) {
let message = util.flag(assertion, 'message');

try {
originalAssert.apply(this, args);
assert.pushResult({ result: true, message });

} catch (error) {
assert.pushResult({
result: false,
actual: error.showDiff ? error.actual : undefined,
expected: error.showDiff ? error.expected : undefined,
message: error.message,
});
}
};
return assertion;
}

return fn.call(this, _expect, assert);
};
}
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ module.exports = {
app.import('vendor/shims/chai.js', { type: 'test' });
},

treeForAddon: function() {
if (this.app.tests) {
return this._super.treeForAddon.apply(this, arguments);
}
},

treeForVendor: function(tree) {
var chaiPath = path.dirname(resolve.sync('chai'));
var chaiTree = new Funnel(chaiPath, {
Expand Down
13 changes: 13 additions & 0 deletions tests/acceptance/index-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { test } from 'qunit';
import { withChai } from 'ember-cli-chai/qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';

moduleForAcceptance('Acceptance | index');

test('visiting /', withChai(function(expect) {
visit('/');

andThen(function() {
expect(currentURL()).to.equal('/');
});
}));
5 changes: 5 additions & 0 deletions tests/unit/chai-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { module, test } from 'qunit';
import { expect } from 'chai';
import { withChai } from 'ember-cli-chai/qunit';

module('Chai.js');

Expand All @@ -8,3 +9,7 @@ test('it works', function(assert) {
assert.throws(() => expect(true).to.be.false);
});

test('it has QUnit integration', withChai(function(expect) {
expect(true).to.be.true;
expect(5, '5 < 10').to.be.below(10);
}));
1 change: 1 addition & 0 deletions vendor/shims/chai.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ self.expect = self.chai.expect;

config: self.chai.config,
use: self.chai.use,
util: self.chai.util,

Assertion: self.chai.Assertion,
AssertionError: self.chai.AssertionError,
Expand Down

0 comments on commit f184d0d

Please sign in to comment.