Skip to content

Commit

Permalink
convert bundle and browser-specific tests to unexpected
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Hiller <boneskull@boneskull.com>
  • Loading branch information
boneskull committed Apr 21, 2018
1 parent c76b2f4 commit 346ab5c
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 51 deletions.
7 changes: 6 additions & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ module.exports = config => {
const cfg = {
frameworks: [
'browserify',
'unexpected',
'mocha'
],
files: [
Expand Down Expand Up @@ -152,6 +151,12 @@ module.exports = config => {
}
}

cfg.files.unshift(
'node_modules/unexpected/unexpected.js',
{pattern: 'node_modules/unexpected/unexpected.js.map', included: false},
'test/browser-specific/setup.js'
);

config.set(cfg);
};

Expand Down
6 changes: 0 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,6 @@
"karma-mocha": "^1.3.0",
"karma-mocha-reporter": "^2.2.4",
"karma-sauce-launcher": "^1.2.0",
"karma-unexpected": "0.0.2",
"markdown-toc": "^1.2.0",
"markdownlint-cli": "^0.6.0",
"nps": "^5.7.1",
Expand Down
1 change: 1 addition & 0 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- **All assertions should be made using [unexpected](http://unexpected.js.org)**, unless there's a good reason not to. Exceptions include:
- Testing diff output. Mocha generates diff output unless the assertion library decides to do this itself. Since `unexpected` generates its *own* diff output, we need to use an assertion library that does not; we use the built-in `assert` module.
- `test/unit/runnable.spec.js` must avoid 3rd-party code; read source for more info
- Tests asserting interop with other specific assertion libraries.
- All tests have extension `.spec.js`.
- All test fixtures have extension `.fixture.js`.
Expand Down
2 changes: 1 addition & 1 deletion test/browser-specific/esm.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';

it('should register a global if it did not fail', function () {
expect(window.MOCHA_IS_OK).to.be.ok();
expect(window.MOCHA_IS_OK, 'to be ok');
});
1 change: 1 addition & 0 deletions test/browser-specific/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
window.expect = window.weknowhow.expect;
2 changes: 1 addition & 1 deletion test/bundle/amd.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ it('should build a non-broken bundle for AMD', function (done) {
fs.readFile(bundle, 'utf8', function (err, content) {
if (err) { return done(err); }

expect(content).not.to.match(/define.amd/);
expect(content, 'not to match', /define.amd/);
done();
});
});
106 changes: 65 additions & 41 deletions test/unit/runnable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ var utils = mocha.utils;
var Runnable = mocha.Runnable;
var Suite = mocha.Suite;

/**
* Custom assert function.
* Because of the below "poison pill", we cannot trust third-party code
* including assertion libraries, not to call the global functions we're
* poisoning--so we must make our own assertions.
* @param {*} expr - Throws if false
*/
function assert (expr) {
if (!expr) {
throw new Error('assertion failure');
}
}

describe('Runnable(title, fn)', function () {
// For every test we poison the global time-related methods.
// runnable.js etc. should keep its own local copy, in order to fix GH-237.
Expand Down Expand Up @@ -39,23 +52,23 @@ describe('Runnable(title, fn)', function () {
it('should set the timeout', function () {
var run = new Runnable();
run.timeout(1000);
expect(run.timeout(), 'to be', 1000);
assert(run.timeout() === 1000);
});
});

describe('#timeout(ms) when ms>2^31', function () {
it('should set disabled', function () {
var run = new Runnable();
run.timeout(1e10);
expect(run.enableTimeouts(), 'to be', false);
assert(run.enableTimeouts() === false);
});
});

describe('#enableTimeouts(enabled)', function () {
it('should set enabled', function () {
var run = new Runnable();
run.enableTimeouts(false);
expect(run.enableTimeouts(), 'to be', false);
assert(run.enableTimeouts() === false);
});
});

Expand All @@ -68,55 +81,55 @@ describe('Runnable(title, fn)', function () {

it('should set the slow threshold', function () {
run.slow(100);
expect(run.slow(), 'to be', 100);
assert(run.slow() === 100);
});

it('should not set the slow threshold if the parameter is not passed', function () {
run.slow();
expect(run.slow(), 'to be', 75);
assert(run.slow() === 75);
});

it('should not set the slow threshold if the parameter is undefined', function () {
run.slow(undefined);
expect(run.slow(), 'to be', 75);
assert(run.slow() === 75);
});
});

describe('.title', function () {
it('should be present', function () {
expect(new Runnable('foo').title, 'to be', 'foo');
assert(new Runnable('foo').title === 'foo');
});
});

describe('.titlePath()', function () {
it('returns the concatenation of the parent\'s title path and runnable\'s title', function () {
var runnable = new Runnable('bar');
runnable.parent = new Suite('foo');
expect(runnable.titlePath(), 'to equal', ['foo', 'bar']);
assert(JSON.stringify(runnable.titlePath()) === JSON.stringify(['foo', 'bar']));
});
});

describe('when arity >= 1', function () {
it('should be .async', function () {
var run = new Runnable('foo', function (done) {});
expect(run.async, 'to be', 1);
expect(run.sync, 'to be', false);
assert(run.async === 1);
assert(run.sync === false);
});
});

describe('when arity == 0', function () {
it('should be .sync', function () {
var run = new Runnable('foo', function () {});
expect(run.async, 'to be', 0);
expect(run.sync, 'to be', true);
assert(run.async === 0);
assert(run.sync === true);
});
});

describe('#globals', function () {
it('should allow for whitelisting globals', function (done) {
var test = new Runnable('foo', function () {});
expect(test.async, 'to be', 0);
expect(test.sync, 'to be', true);
assert(test.async === 0);
assert(test.sync === true);
test.globals(['foobar']);
test.run(done);
});
Expand All @@ -126,7 +139,7 @@ describe('Runnable(title, fn)', function () {
it('should set the number of retries', function () {
var run = new Runnable();
run.retries(1);
expect(run.retries(), 'to be', 1);
assert(run.retries() === 1);
});
});

Expand Down Expand Up @@ -157,8 +170,8 @@ describe('Runnable(title, fn)', function () {
}

try {
expect(calls, 'to be', 1);
expect(test.duration, 'to be a', 'number');
assert(calls === 1);
assert(typeof test.duration === 'number');
} catch (err) {
done(err);
return;
Expand All @@ -177,8 +190,8 @@ describe('Runnable(title, fn)', function () {
});

test.run(function (err) {
expect(calls, 'to be', 1);
expect(err.message, 'to be', 'fail');
assert(calls === 1);
assert(err.message === 'fail');
done();
});
});
Expand All @@ -193,8 +206,13 @@ describe('Runnable(title, fn)', function () {
function fail () {
test.run(function () {});
}
expect(fail, 'to throw', 'fail');
done();
try {
fail();
done(new Error('failed to throw'));
} catch (e) {
assert(e.message === 'fail');
done();
}
});
});
});
Expand Down Expand Up @@ -238,9 +256,9 @@ describe('Runnable(title, fn)', function () {

test.on('error', function (err) {
++errCalls;
expect(err.message, 'to be', 'done() called multiple times');
expect(calls, 'to be', 1);
expect(errCalls, 'to be', 1);
assert(err.message === 'done() called multiple times');
assert(calls === 1);
assert(errCalls === 1);
done();
});

Expand All @@ -265,9 +283,9 @@ describe('Runnable(title, fn)', function () {

test.on('error', function (err) {
++errCalls;
expect(err.message, 'to be', "fail (and Mocha's done() called multiple times)");
expect(calls, 'to be', 1);
expect(errCalls, 'to be', 1);
assert(err.message === "fail (and Mocha's done() called multiple times)");
assert(calls === 1);
assert(errCalls === 1);
done();
});

Expand All @@ -285,7 +303,7 @@ describe('Runnable(title, fn)', function () {
});

test.run(function (err) {
expect(err.message, 'to be', 'fail');
assert(err.message === 'fail');
done();
});
});
Expand All @@ -297,7 +315,7 @@ describe('Runnable(title, fn)', function () {
});

test.run(function (err) {
expect(err.message, 'to be', utils.undefinedError().message);
assert(err.message === utils.undefinedError().message);
done();
});
});
Expand All @@ -312,7 +330,13 @@ describe('Runnable(title, fn)', function () {
function fail () {
test.run(function () {});
}
expect(fail, 'to throw', 'fail');
try {
fail();
done(new Error('failed to throw'));
} catch (e) {
assert(e.message === 'fail');
done();
}
done();
});
});
Expand All @@ -324,7 +348,7 @@ describe('Runnable(title, fn)', function () {
});

test.run(function (err) {
expect(err.message, 'to be', 'fail');
assert(err.message === 'fail');
done();
});
});
Expand All @@ -337,7 +361,7 @@ describe('Runnable(title, fn)', function () {
});

test.run(function (err) {
expect(err.message, 'to be', 'done() invoked with non-Error: {"error":"Test error"}');
assert(err.message === 'done() invoked with non-Error: {"error":"Test error"}');
done();
});
});
Expand All @@ -350,7 +374,7 @@ describe('Runnable(title, fn)', function () {
});

test.run(function (err) {
expect(err.message, 'to be', 'done() invoked with non-Error: Test error');
assert(err.message === 'done() invoked with non-Error: Test error');
done();
});
});
Expand All @@ -367,8 +391,8 @@ describe('Runnable(title, fn)', function () {
});
test.timeout(50);
test.run(function (err) {
expect(err, 'to be ok');
expect(callCount, 'to be', 1);
assert(err);
assert(callCount === 1);
done();
});
});
Expand Down Expand Up @@ -427,7 +451,7 @@ describe('Runnable(title, fn)', function () {
});

test.run(function (err) {
expect(err, 'to be', expectedErr);
assert(err === expectedErr);
done();
});
});
Expand All @@ -449,7 +473,7 @@ describe('Runnable(title, fn)', function () {
});

test.run(function (err) {
expect(err.message, 'to be', expectedErr.message);
assert(err.message === expectedErr.message);
done();
});
});
Expand All @@ -468,7 +492,7 @@ describe('Runnable(title, fn)', function () {

test.timeout(10);
test.run(function (err) {
expect(err, 'to have message', /Timeout of 10ms exceeded.*\(\/some\/path\)$/);
assert(/Timeout of 10ms exceeded.*\(\/some\/path\)$/.test(err.message));
done();
});
});
Expand All @@ -492,7 +516,7 @@ describe('Runnable(title, fn)', function () {
});
// runner sets the state
test.run(function () {
expect(test.isFailed(), 'not to be ok');
assert(!test.isFailed());
});
});

Expand All @@ -502,7 +526,7 @@ describe('Runnable(title, fn)', function () {
// runner sets the state
test.state = 'failed';
test.run(function () {
expect(test.isFailed(), 'not to be ok');
assert(!test.isFailed());
});
});

Expand All @@ -514,7 +538,7 @@ describe('Runnable(title, fn)', function () {
return true;
};
test.run(function () {
expect(test.isFailed(), 'not to be ok');
assert(!test.isFailed());
});
});
});
Expand Down

0 comments on commit 346ab5c

Please sign in to comment.