Skip to content

Commit c04c1d7

Browse files
dasilvacontinboneskull
authored andcommitted
Make CI run browser tests (#2231)
* Initial prep for browser tests in CI - ⬆️ [email protected] Some fixes that we didn't have were required. - 🐛 Fix browser not being able to require describe/it `support/browser-entry.js` was removing the listener that is in charge of keeping `Mocha.describe/it/suite` etc up-to-date. That listener is now added everytime a `ui` is set up. - Correct isatty for browser * Travis + SauceLabs + Karma setup (first pass) - add sauce connect - prepare to use karma on saucelabs; fix potential windows problems - rewrite `scripts/ensure-compatible-npm.sh` in JS - detect travis node 0.8 env; just upgrade if found - symlink `mocha` to itself for `karma-mocha` - if explicit `--production` flag is present, skip this - don't know how to skip this part if `mocha` is a devDep of some other package - use [email protected]; don't try to install npm@3 - browser tests with karma & phantom - move `support/browser-entry.js` to `browser-entry.js` to keep paths sane - `karma.conf.js` loads data out of `.karma.conf.js` - we have a "main" browser suite - then we have suites for each interface - each suite is a single run of Karma - each suite is executed by the `Makefile` - build `./mocha.js` upon test execution for `karma-mocha` to use - bonus: 3rd-party browserification should theoretically now be possible as per the `./index.js` entry in `package.json`'s `browser` field - add dev deps for karma - test changes: - split `lookupFiles` test into its own file so we can ignore it in Karma - fix broken `test/acceptance/throw.js` for browser - fix `test/acceptance/utils.js`'s `type` tests for PhantomJS - try to reduce build matrix - fix missing targets - fix infinite loop in Makefile - downgrade phantomjs to 1.9.8 - remove some cruft from karma config; try to add sauce labs - try again w/ the saucelabs - typo - remove karma-source-map-support as it's a no-go on IE8 - Require up-to-date mocha.js for any browser tests - Make CI rebuild mocha.js. Make won't rebuild when just checked out by CI because timestamps on source and mocha.js are the same. We could use a script to set the timestamps to the commit time, but it still wouldn't work if mocha.js were committed along with other changes that hadn't been built into it. Which shouldn't happen anyway -- but then again, the point of CI is to see what commits change, it's usually going to need to rebuild mocha.js if it's working right, so it's not going to hurt much to rebuild it every time even on the few times it doesn't need to. * fix IE8 compatibility for browser tests - 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` * Travis + SauceLabs + Karma setup (second pass) - add note about exporting `global` in `browser-entry.js` - remove too-short timeout for "throw" unit tests (because SauceLabs) - revert modification to `isatty()` function of `lib/browser/tty.js` - switch reporter to `karma-spec-reporter` - use custom `karma-no-mocha` package - fixtures in `test/browser-fixtures/` - use single `karma.conf.js`
1 parent b76989c commit c04c1d7

31 files changed

+5143
-4617
lines changed

.travis.yml

+26-11
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,37 @@ dist: trusty
88

99
language: node_js
1010

11-
node_js:
12-
- '6'
13-
- '5'
14-
- '4'
15-
- 'iojs'
16-
- '0.12'
17-
- '0.11'
18-
- '0.10'
19-
- '0.8'
11+
matrix:
12+
include:
13+
- node_js: '6'
14+
env: TARGET="clean lint test-node test-browser"
15+
- node_js: '5'
16+
env: TARGET=test-node
17+
- node_js: '4'
18+
env: TARGET=test-node
19+
- node_js: 'iojs'
20+
env: TARGET=test-node
21+
- node_js: '0.12'
22+
env: TARGET=test-node
23+
- node_js: '0.11'
24+
env: TARGET=test-node
25+
- node_js: '0.10'
26+
env: TARGET=test-node
27+
- node_js: '0.8'
28+
env: TARGET=test-node
2029

2130
before_install:
2231
# node 0.8 won't install our dev deps with an out-of-box npm;
2332
# this upgrades it
24-
- ./scripts/ensure-compatible-npm.sh
33+
- node ./scripts/upgrade-npm.js
2534

26-
script: travis_retry npm test
35+
script: travis_retry make $TARGET
36+
37+
addons:
38+
sauce_connect:
39+
username: mochajs
40+
access_key:
41+
secure: R0HXKtR6F2iDEnItv57BTRyL64XfyIlyyluPLK8G33O/InaQjT3KxGuxevz3nVYIqqnI1MPjYodXcQaqrBOLUVmA2vhBeMHB2OwGc9GAL+HBtB1fh+bQJelkl/XMcTTbC5LIZ6nZjmFnkmjqT3AmUhdDRISgieIFeVY4x48LfiU=
2742

2843
notifications:
2944
urls:

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
Unreleased
2+
==================
3+
4+
* [#2079], [#2231] - Add browser to CI
5+
6+
[#2079]: https://github.com/mochajs/mocha/issues/2079
7+
[#2231]: https://github.com/mochajs/mocha/pull/2231
8+
19
2.4.5 / 2016-01-28
210
==================
311

Makefile

+45-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
BROWSERIFY := node_modules/.bin/browserify
22
ESLINT := node_modules/.bin/eslint
3+
KARMA := node_modules/.bin/karma
34

45
REPORTER ?= spec
56
TM_BUNDLE = JavaScript\ mocha.tmbundle
67
SRC = $(shell find lib -name "*.js" -type f | sort)
8+
TESTS = $(shell find test -name "*.js" -type f | sort)
79
SUPPORT = $(wildcard support/*.js)
810

911
all: mocha.js
1012

1113
mocha.js: $(SRC) $(SUPPORT)
12-
@$(BROWSERIFY) ./support/browser-entry \
14+
@printf "==> [Browser :: build]\n"
15+
@$(BROWSERIFY) ./browser-entry \
1316
--ignore 'fs' \
1417
--ignore 'glob' \
1518
--ignore 'jade' \
@@ -18,48 +21,75 @@ mocha.js: $(SRC) $(SUPPORT)
1821
--exclude './lib-cov/mocha' > $@
1922

2023
clean:
24+
@printf "==> [Clean]\n"
2125
rm -f mocha.js
2226
rm -rf test-outputs
23-
rm -fr lib-cov
27+
rm -rf lib-cov
2428
rm -f coverage.html
2529

2630
test-cov: lib-cov
31+
@printf "==> [Test :: Coverage]\n"
2732
@COV=1 $(MAKE) test REPORTER=html-cov > coverage.html
2833

2934
lib-cov:
35+
@printf "==> [Coverage]\n"
3036
@rm -fr ./$@
3137
@jscoverage lib $@
3238

3339
lint:
40+
@printf "==> [Test :: Lint]\n"
3441
@$(ESLINT) $(SRC)
3542

36-
test: lint test-unit
43+
test-node: test-bdd test-tdd test-qunit test-exports test-unit test-integration test-jsapi test-compilers test-glob test-requires test-reporters test-only
3744

38-
test-all: lint test-bdd test-tdd test-qunit test-exports test-unit test-integration test-jsapi test-compilers test-glob test-requires test-reporters test-only
45+
test-browser: test-browser-unit test-browser-bdd test-browser-qunit test-browser-tdd test-browser-exports
46+
47+
test: lint test-node test-browser
48+
49+
test-browser-unit: mocha.js
50+
@printf "==> [Test :: Browser]\n"
51+
@NODE_PATH=. $(KARMA) start
52+
53+
test-browser-bdd:
54+
@printf "==> [Test :: Browser :: BDD]\n"
55+
@MOCHA_UI=bdd $(MAKE) test-browser-unit
56+
57+
test-browser-qunit:
58+
@printf "==> [Test :: Browser :: QUnit]\n"
59+
@MOCHA_UI=qunit $(MAKE) test-browser-unit
60+
61+
test-browser-tdd:
62+
@printf "==> [Test :: Browser :: TDD]\n"
63+
@MOCHA_UI=tdd $(MAKE) test-browser-unit
3964

4065
test-jsapi:
66+
@printf "==> [Test :: JS API]\n"
4167
@node test/jsapi
4268

4369
test-unit:
70+
@printf "==> [Test :: Unit]\n"
4471
@./bin/mocha \
4572
--reporter $(REPORTER) \
4673
test/acceptance/*.js \
4774
--growl \
4875
test/*.js
4976

5077
test-integration:
78+
@printf "==> [Test :: Integrations]\n"
5179
@./bin/mocha \
5280
--reporter $(REPORTER) \
5381
test/integration/*.js
5482

5583
test-compilers:
84+
@printf "==> [Test :: Compilers]\n"
5685
@./bin/mocha \
5786
--reporter $(REPORTER) \
5887
--compilers coffee:coffee-script/register,foo:./test/compiler/foo \
5988
test/acceptance/test.coffee \
6089
test/acceptance/test.foo
6190

6291
test-requires:
92+
@printf "==> [Test :: Requires]\n"
6393
@./bin/mocha \
6494
--reporter $(REPORTER) \
6595
--compilers coffee:coffee-script/register \
@@ -70,38 +100,45 @@ test-requires:
70100
test/acceptance/require/require.js
71101

72102
test-bdd:
103+
@printf "==> [Test :: BDD]\n"
73104
@./bin/mocha \
74105
--reporter $(REPORTER) \
75106
--ui bdd \
76107
test/acceptance/interfaces/bdd
77108

78109
test-tdd:
110+
@printf "==> [Test :: TDD]\n"
79111
@./bin/mocha \
80112
--reporter $(REPORTER) \
81113
--ui tdd \
82114
test/acceptance/interfaces/tdd
83115

84116
test-qunit:
117+
@printf "==> [Test :: QUnit]\n"
85118
@./bin/mocha \
86119
--reporter $(REPORTER) \
87120
--ui qunit \
88121
test/acceptance/interfaces/qunit
89122

90123
test-exports:
124+
@printf "==> [Test :: Exports]\n"
91125
@./bin/mocha \
92126
--reporter $(REPORTER) \
93127
--ui exports \
94128
test/acceptance/interfaces/exports
95129

96130
test-glob:
131+
@printf "==> [Test :: Glob]\n"
97132
@./test/acceptance/glob/glob.sh
98133

99134
test-reporters:
135+
@printf "==> [Test :: Reporters]\n"
100136
@./bin/mocha \
101137
--reporter $(REPORTER) \
102138
test/reporters/*.js
103139

104140
test-only:
141+
@printf "==> [Test :: Only]\n"
105142
@./bin/mocha \
106143
--reporter $(REPORTER) \
107144
--ui tdd \
@@ -123,11 +160,13 @@ test-only:
123160
test/acceptance/misc/only/qunit
124161

125162
test-mocha:
163+
@printf "==> [Test :: Mocha]\n"
126164
@./bin/mocha \
127165
--reporter $(REPORTER) \
128166
test/mocha
129167

130168
non-tty:
169+
@printf "==> [Test :: Non-TTY]\n"
131170
@./bin/mocha \
132171
--reporter dot \
133172
test/acceptance/interfaces/bdd 2>&1 > /tmp/dot.out
@@ -150,6 +189,7 @@ non-tty:
150189
@cat /tmp/spec.out
151190

152191
tm:
192+
@printf "==> [TM]\n"
153193
@open editors/$(TM_BUNDLE)
154194

155-
.PHONY: test-cov test-jsapi test-compilers watch test test-all test-bdd test-tdd test-qunit test-exports test-unit test-integration non-tty tm clean
195+
.PHONY: test-cov test-jsapi test-compilers watch test test-node test-bdd test-tdd test-qunit test-exports test-unit test-integration non-tty tm clean test-browser test-browser-unit test-browser-bdd test-browser-qunit test-browser-tdd test-browser-exports lint

support/browser-entry.js renamed to browser-entry.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
process.stdout = require('browser-stdout')();
66

7-
var Mocha = require('../');
7+
var Mocha = require('./lib/mocha');
88

99
/**
1010
* Create a Mocha instance.
@@ -159,3 +159,8 @@ Mocha.process = process;
159159

160160
global.Mocha = Mocha;
161161
global.mocha = mocha;
162+
163+
// this allows test/acceptance/required-tokens.js to pass; thus,
164+
// you can now do `const describe = require('mocha').describe` in a
165+
// browser context (assuming browserification). should fix #880
166+
module.exports = global;

karma.conf.js

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
'use strict';
2+
3+
module.exports = function(config) {
4+
var cfg = {
5+
frameworks: [
6+
'browserify',
7+
'expect',
8+
'mocha'
9+
],
10+
files: [
11+
'test/browser-fixtures/bdd.js',
12+
'test/acceptance/*.js'
13+
],
14+
exclude: [
15+
'test/acceptance/http.js',
16+
'test/acceptance/fs.js',
17+
'test/acceptance/lookup-files.js',
18+
'test/acceptance/require/**/*.js',
19+
'test/acceptance/misc/**/*.js'
20+
],
21+
preprocessors: {
22+
'test/**/*.js': ['browserify']
23+
},
24+
browserify: {
25+
debug: true,
26+
configure: function configure(b) {
27+
b.ignore('glob')
28+
.ignore('jade')
29+
.ignore('supports-color')
30+
.exclude('./lib-cov/mocha');
31+
}
32+
},
33+
reporters: ['spec'],
34+
colors: true,
35+
browsers: ['PhantomJS'],
36+
logLevel: config.LOG_INFO,
37+
singleRun: true
38+
};
39+
40+
// see https://github.com/saucelabs/karma-sauce-example
41+
// TO RUN LOCALLY:
42+
// Execute `CI=1 make test-browser`, once you've set the SAUCE_USERNAME and
43+
// SAUCE_ACCESS_KEY env vars.
44+
if (process.env.CI) {
45+
if (!(process.env.SAUCE_USERNAME || process.env.SAUCE_ACCESS_KEY)) {
46+
throw new Error('Must set SAUCE_USERNAME and SAUCE_ACCESS_KEY '
47+
+ 'environment variables!');
48+
}
49+
cfg.reporters.push('saucelabs');
50+
cfg.browsers.push('ie8');
51+
cfg.customLaunchers = {
52+
ie8: {
53+
base: 'SauceLabs',
54+
browserName: 'internet explorer',
55+
platform: 'Windows XP',
56+
version: '8.0'
57+
}
58+
};
59+
60+
cfg.sauceLabs = {
61+
public: 'public'
62+
};
63+
64+
if (process.env.TRAVIS) {
65+
// correlate build/tunnel with Travis
66+
cfg.sauceLabs.build = 'TRAVIS #' + process.env.TRAVIS_BUILD_NUMBER
67+
+ ' (' + process.env.TRAVIS_BUILD_ID + ')';
68+
cfg.sauceLabs.tunnelIdentifier = process.env.TRAVIS_JOB_NUMBER;
69+
cfg.sauceLabs.startConnect = false;
70+
} else {
71+
// otherwise just make something up
72+
cfg.sauceLabs.build = require('os').hostname() + ' (' + Date.now() + ')';
73+
}
74+
75+
// for slow browser booting, ostensibly
76+
cfg.captureTimeout = 120000;
77+
}
78+
79+
// the MOCHA_UI env var will determine if we're running interface-specific
80+
// tets. since you can only load one at a time, each must be run separately.
81+
// each has its own set of acceptance tests and a fixture.
82+
// the "bdd" fixture is used by default.
83+
var ui = process.env.MOCHA_UI;
84+
if (ui) {
85+
if (cfg.sauceLabs) {
86+
cfg.sauceLabs.testName = 'Interface "' + ui + '" integration tests';
87+
}
88+
cfg.files = [
89+
'test/browser-fixtures/' + ui + '.js',
90+
'test/acceptance/interfaces/' + ui + '.js'
91+
];
92+
} else if (cfg.sauceLabs) {
93+
cfg.sauceLabs.testName = 'Unit Tests';
94+
}
95+
96+
config.set(cfg);
97+
};

lib/mocha.js

+17-16
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,6 @@ function Mocha(options) {
9999
if (options.slow) {
100100
this.slow(options.slow);
101101
}
102-
103-
this.suite.on('pre-require', function(context) {
104-
exports.afterEach = context.afterEach || context.teardown;
105-
exports.after = context.after || context.suiteTeardown;
106-
exports.beforeEach = context.beforeEach || context.setup;
107-
exports.before = context.before || context.suiteSetup;
108-
exports.describe = context.describe || context.suite;
109-
exports.it = context.it || context.test;
110-
exports.setup = context.setup || context.beforeEach;
111-
exports.suiteSetup = context.suiteSetup || context.before;
112-
exports.suiteTeardown = context.suiteTeardown || context.after;
113-
exports.suite = context.suite || context.describe;
114-
exports.teardown = context.teardown || context.afterEach;
115-
exports.test = context.test || context.it;
116-
exports.run = context.run;
117-
});
118102
}
119103

120104
/**
@@ -202,6 +186,23 @@ Mocha.prototype.ui = function(name) {
202186
}
203187
}
204188
this._ui = this._ui(this.suite);
189+
190+
this.suite.on('pre-require', function(context) {
191+
exports.afterEach = context.afterEach || context.teardown;
192+
exports.after = context.after || context.suiteTeardown;
193+
exports.beforeEach = context.beforeEach || context.setup;
194+
exports.before = context.before || context.suiteSetup;
195+
exports.describe = context.describe || context.suite;
196+
exports.it = context.it || context.test;
197+
exports.setup = context.setup || context.beforeEach;
198+
exports.suiteSetup = context.suiteSetup || context.before;
199+
exports.suiteTeardown = context.suiteTeardown || context.after;
200+
exports.suite = context.suite || context.describe;
201+
exports.teardown = context.teardown || context.afterEach;
202+
exports.test = context.test || context.it;
203+
exports.run = context.run;
204+
});
205+
205206
return this;
206207
};
207208

0 commit comments

Comments
 (0)