Skip to content

Commit

Permalink
fix IE8 compatibility for browser tests
Browse files Browse the repository at this point in the history
- Avoid `Array.prototype.map` in test
- Workaround for missing `Object.create`
- Use a shim for `Date.prototype.toISOString`
- Use simple number math instead of array indexing for interface tests
- Use `expect` instead of `should`
- Avoid builtin function in stringify test (A quick check revealed that stringify does not treat toString specially anyway, and IE8 ignores the toString assigned to the object, so use a different property name)
- Use `karma-expect` for automatic browser tests
- Remove `karma-should`
  • Loading branch information
ScottFreeCode authored and boneskull committed May 21, 2016
1 parent a03ae0e commit c962a51
Show file tree
Hide file tree
Showing 20 changed files with 217 additions and 192 deletions.
2 changes: 1 addition & 1 deletion .karma-config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ function getConfig() {
var cfg = {
frameworks: [
'browserify',
'should',
'expect',
'mocha'
],
files: [
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
Unreleased
==================

* [#2079], [#2231] - Add browser to CI

[#2079]: https://github.com/mochajs/mocha/issues/2079
[#2231]: https://github.com/mochajs/mocha/pull/2231

2.4.5 / 2016-01-28
==================

Expand Down
10 changes: 7 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var join = require('path').join;
var readdirSync = require('fs').readdirSync;
var statSync = require('fs').statSync;
var watchFile = require('fs').watchFile;
var toISOString = require('to-iso-string');

/**
* Ignored directories.
Expand Down Expand Up @@ -479,9 +480,12 @@ function jsonStringify(object, spaces, depth) {
: val.toString();
break;
case 'date':
var sDate = isNaN(val.getTime()) // Invalid date
? val.toString()
: val.toISOString();
var sDate;
if (isNaN(val.getTime())) { // Invalid date
sDate = val.toString();
} else {
sDate = val.toISOString ? val.toISOString() : toISOString(val);
}
val = '[Date: ' + sDate + ']';
break;
case 'buffer':
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@
"Jordan Sexton <jordan@jordansexton.com>",
"Jussi Virtanen <jussi.k.virtanen@gmail.com>",
"Katie Gengler <katiegengler@gmail.com>",
"Kazuhito Hokamura <k.hokamura@gmail.com>"
"Kazuhito Hokamura <k.hokamura@gmail.com>",
"Scott Santucci <ScottFreeCode@gmail.com>"
],
"license": "MIT",
"repository": {
Expand Down Expand Up @@ -285,16 +286,18 @@
"browserify": "^13.0.0",
"coffee-script": "~1.8.0",
"eslint": "^1.2.1",
"expect.js": "^0.3.1",
"karma": "^0.13.22",
"karma-browserify": "^5.0.5",
"karma-expect": "^1.1.2",
"karma-mocha": "^1.0.1",
"karma-mocha-clean-reporter": "0.0.1",
"karma-phantomjs-launcher": "^0.2.3",
"karma-sauce-launcher": "^1.0.0",
"karma-should": "^1.0.0",
"phantomjs": "1.9.8",
"should": "~8.0.0",
"through2": "~0.6.5",
"to-iso-string": "0.0.2",
"watchify": "^3.7.0"
},
"files": [
Expand Down
18 changes: 9 additions & 9 deletions test/acceptance/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ describe('Context', function(){
})

it('should work', function(){
this.calls.should.eql(['before', 'before two']);
expect(this.calls).to.eql(['before', 'before two']);
this.calls.push('test');
})

after(function(){
this.calls.should.eql(['before', 'before two', 'test']);
expect(this.calls).to.eql(['before', 'before two', 'test']);
this.calls.push('after two');
})
})

after(function(){
this.calls.should.eql(['before', 'before two', 'test', 'after two']);
expect(this.calls).to.eql(['before', 'before two', 'test', 'after two']);
})
})

Expand All @@ -36,7 +36,7 @@ describe('Context Siblings', function(){
})

it('should work', function(){
this.hiddenFromSibling.should.eql('This should be hidden')
expect(this.hiddenFromSibling).to.eql('This should be hidden')
})
})

Expand All @@ -46,27 +46,27 @@ describe('Context Siblings', function(){
})

it('should not have value set within a sibling describe', function(){
'This should be hidden'.should.not.eql(this.hiddenFromSibling);
expect('This should be hidden').not.to.eql(this.hiddenFromSibling);
this.visibleFromTestSibling = 'Visible from test sibling';
})

it('should allow test siblings to modify shared context', function(){
'Visible from test sibling'.should.eql(this.visibleFromTestSibling);
expect('Visible from test sibling').to.eql(this.visibleFromTestSibling);
})

it('should have reset this.calls before describe', function(){
this.calls.should.eql(['before', 'before sibling']);
expect(this.calls).to.eql(['before', 'before sibling']);
})
})

after(function(){
this.calls.should.eql(['before', 'before sibling']);
expect(this.calls).to.eql(['before', 'before sibling']);
})

})

describe('timeout()', function(){
it('should return the timeout', function(){
this.timeout().should.equal(200);
expect(this.timeout()).to.equal(200);
});
});
2 changes: 1 addition & 1 deletion test/acceptance/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ server.listen(8888);
describe('http', function(){
it('should provide an example', function(done){
http.get({ path: '/', port: 8888 }, function(res){
res.should.have.property('statusCode', 200);
expect(res).to.have.property('statusCode', 200);
done();
})
})
Expand Down
36 changes: 17 additions & 19 deletions test/acceptance/interfaces/bdd.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
describe('Array', function(){
describe('#indexOf()', function(){
it('should return -1 when the value is not present', function(){
[1,2,3].indexOf(5).should.equal(-1);
[1,2,3].indexOf(0).should.equal(-1);
describe('integer primitives', function(){
describe('arithmetic', function(){
it('should add', function(){
expect(1 + 1).to.equal(2);
expect(2 + 2).to.equal(4);
})

it('should return the correct index when the value is present', function(){
[1,2,3].indexOf(1).should.equal(0);
[1,2,3].indexOf(2).should.equal(1);
[1,2,3].indexOf(3).should.equal(2);
it('should subtract', function(){
expect(1 - 1).to.equal(0);
expect(2 - 1).to.equal(1);
})
})
})

describe('Array', function(){
describe('#pop()', function(){
it('should remove and return the last value', function(){
var arr = [1,2,3];
arr.pop().should.equal(3);
arr.should.eql([1,2]);
describe('integer primitives', function(){
describe('arithmetic is not', function(){
it('should add', function(){
expect(1 + 1).not.to.equal(3);
expect(2 + 2).not.to.equal(5);
})
})
})

context('Array', function(){
context('test suite', function(){
beforeEach(function(){
this.arr = [1,2,3];
this.number = 5;
})

specify('has a length property', function(){
this.arr.length.should.equal(3);
specify('share a property', function(){
expect(this.number).to.equal(5);
})
})
12 changes: 6 additions & 6 deletions test/acceptance/interfaces/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ exports.Array = {

after: function(){
calls.push('after');
calls.should.eql([
expect(calls).to.eql([
'before'
, 'before each'
, 'one'
Expand All @@ -29,15 +29,15 @@ exports.Array = {

'should return -1 when the value is not present': function(){
calls.push('one');
[1,2,3].indexOf(5).should.equal(-1);
[1,2,3].indexOf(0).should.equal(-1);
expect([1,2,3].indexOf(5)).to.equal(-1);
expect([1,2,3].indexOf(0)).to.equal(-1);
},

'should return the correct index when the value is present': function(){
calls.push('two');
[1,2,3].indexOf(1).should.equal(0);
[1,2,3].indexOf(2).should.equal(1);
[1,2,3].indexOf(3).should.equal(2);
expect([1,2,3].indexOf(1)).to.equal(0);
expect([1,2,3].indexOf(2)).to.equal(1);
expect([1,2,3].indexOf(3)).to.equal(2);
}
}
};
18 changes: 9 additions & 9 deletions test/acceptance/interfaces/qunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ function ok(expr, msg) {
if (!expr) throw new Error(msg);
}

suite('Array');
suite('integer primitives');

test('#length', function(){
var arr = [1,2,3];
ok(arr.length == 3);
test('should add', function(){
var number = 2 + 2;
ok(number == 4);
});

test('#indexOf()', function(){
var arr = [1,2,3];
ok(arr.indexOf(1) == 0);
ok(arr.indexOf(2) == 1);
ok(arr.indexOf(3) == 2);
test('should decrement', function(){
var number = 3;
ok(--number == 2);
ok(--number == 1);
ok(--number == 0);
});

suite('String');
Expand Down
33 changes: 16 additions & 17 deletions test/acceptance/interfaces/tdd.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
suite('Array', function(){
suite('#indexOf()', function(){
var initialValue = 32;
suite('integer primitives', function(){
suite('arithmetic', function(){
var initialValue = 41;

suiteSetup(function(done){
initialValue.should.eql(32);
initialValue = 42;
expect(initialValue).to.eql(41);
initialValue += 1;
done();
});

test('should return -1 when the value is not present', function(){
initialValue.should.eql(42);
[1,2,3].indexOf(5).should.equal(-1);
[1,2,3].indexOf(0).should.equal(-1);
test('should add', function(){
expect(initialValue).to.eql(42);
expect(1 + 1).to.equal(2);
expect(2 + 2).to.equal(4);
});

test('should return the correct index when the value is present', function(){
initialValue.should.eql(42);
[1,2,3].indexOf(1).should.equal(0);
[1,2,3].indexOf(2).should.equal(1);
[1,2,3].indexOf(3).should.equal(2);
test('should subtract', function(){
expect(initialValue).to.eql(42);
expect(1 - 1).to.equal(0);
expect(2 - 1).to.equal(1);
});

test.skip('should skip this test', function(){
var zero = 0;
zero.should.equal(1, 'this test should have been skipped');
expect(zero).to.equal(1, 'this test should have been skipped');
});

suite.skip('should skip this suite', function(){
test('should skip this test', function(){
var zero = 0;
zero.should.equal(1, 'this test should have been skipped');
expect(zero).to.equal(1, 'this test should have been skipped');
});
});

suiteTeardown(function(done){
initialValue.should.eql(42);
expect(initialValue).to.eql(42);
done();
});
});
Expand Down
38 changes: 18 additions & 20 deletions test/acceptance/lookup-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,35 @@ describe('lookupFiles', function() {
});

it('should not choke on symlinks', function() {
utils.lookupFiles('/tmp', ['js'], false)
.should
.containEql('/tmp/mocha-utils-link.js')
expect(utils.lookupFiles('/tmp', ['js'], false))
.to
.contain('/tmp/mocha-utils-link.js')
.and
.containEql('/tmp/mocha-utils.js')
.contain('/tmp/mocha-utils.js')
.and
.have
.lengthOf(2);
existsSync('/tmp/mocha-utils-link.js')
.should
.be
.true();
.length(2);
expect(existsSync('/tmp/mocha-utils-link.js'))
.to
.be(true);
fs.renameSync('/tmp/mocha-utils.js', '/tmp/bob');
existsSync('/tmp/mocha-utils-link.js')
.should
.be
.false();
utils.lookupFiles('/tmp', ['js'], false)
.should
expect(existsSync('/tmp/mocha-utils-link.js'))
.to
.be(false);
expect(utils.lookupFiles('/tmp', ['js'], false))
.to
.eql([]);
});

it('should accept a glob "path" value', function() {
utils.lookupFiles('/tmp/mocha-utils*', ['js'], false)
.should
.containEql('/tmp/mocha-utils-link.js')
expect(utils.lookupFiles('/tmp/mocha-utils*', ['js'], false))
.to
.contain('/tmp/mocha-utils-link.js')
.and
.containEql('/tmp/mocha-utils.js')
.contain('/tmp/mocha-utils.js')
.and
.have
.lengthOf(2);
.length(2);
});

afterEach(function() {
Expand Down
6 changes: 3 additions & 3 deletions test/acceptance/misc/only/bdd.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
describe('should only run .only test in this bdd suite', function() {
it('should not run this test', function() {
var zero = 0;
zero.should.equal(1, 'this test should have been skipped');
expect(zero).to.equal(1, 'this test should have been skipped');
});
it.only('should run this test', function() {
var zero = 0;
zero.should.equal(0, 'this .only test should run');
expect(zero).to.equal(0, 'this .only test should run');
});
it('should run this test, not (includes the title of the .only test)', function() {
var zero = 0;
zero.should.equal(1, 'this test should have been skipped');
expect(zero).to.equal(1, 'this test should have been skipped');
});
});
Loading

0 comments on commit c962a51

Please sign in to comment.