Skip to content

Commit

Permalink
Merge add3281 into 575d83d
Browse files Browse the repository at this point in the history
  • Loading branch information
boneskull committed Dec 12, 2017
2 parents 575d83d + add3281 commit 5fe7bad
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 55 deletions.
26 changes: 12 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
BROWSERIFY := "node_modules/.bin/browserify"
BROWSERIFY := "scripts/build.js"
KARMA := "node_modules/.bin/karma"
MOCHA := "bin/mocha"
NYC := "node_modules/.bin/nyc"

ifdef COVERAGE
define test_node
$(NYC) --no-clean --report-dir coverage/reports/$(1) $(MOCHA)
$(NYC) --no-clean --report-dir coverage/reports/$(1) $(MOCHA)
endef
else
test_node := $(MOCHA)
test_node := $(MOCHA)
endif

TM_BUNDLE = JavaScript\ mocha.tmbundle
Expand All @@ -20,13 +20,7 @@ all: mocha.js
mocha.js: $(SRC) browser-entry.js
@printf "==> [Browser :: build]\n"
mkdir -p ${@D}
$(BROWSERIFY) ./browser-entry \
--require buffer/:buffer \
--plugin ./scripts/dedefine \
--ignore 'fs' \
--ignore 'glob' \
--ignore 'path' \
--ignore 'supports-color' > $@
$(BROWSERIFY) > $@

clean:
@printf "==> [Clean]\n"
Expand All @@ -38,7 +32,7 @@ lint:

test-node: test-bdd test-tdd test-qunit test-exports test-unit test-integration test-jsapi test-compilers test-requires test-reporters test-only test-global-only

test-browser: clean mocha.js test-browser-unit test-browser-bdd test-browser-qunit test-browser-tdd test-browser-exports
test-browser: clean mocha.js test-browser-unit test-browser-bdd test-browser-qunit test-browser-tdd test-browser-esm

test: lint test-node test-browser

Expand All @@ -48,15 +42,19 @@ test-browser-unit:

test-browser-bdd:
@printf "==> [Test :: Browser :: BDD]\n"
MOCHA_UI=bdd $(MAKE) test-browser-unit
MOCHA_TEST=bdd $(MAKE) test-browser-unit

test-browser-qunit:
@printf "==> [Test :: Browser :: QUnit]\n"
MOCHA_UI=qunit $(MAKE) test-browser-unit
MOCHA_TEST=qunit $(MAKE) test-browser-unit

test-browser-tdd:
@printf "==> [Test :: Browser :: TDD]\n"
MOCHA_UI=tdd $(MAKE) test-browser-unit
MOCHA_TEST=tdd $(MAKE) test-browser-unit

test-browser-esm:
@printf "==> [Test :: Browser :: ESM]\n"
MOCHA_TEST=esm $(MAKE) test-browser-unit

test-jsapi:
@printf "==> [Test :: JS API]\n"
Expand Down
104 changes: 63 additions & 41 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var baseBundleDirpath = path.join(__dirname, '.karma');
var builder = require('./scripts/build');
var build = builder.build;
var bundlerOptions = builder.options;

var browserPlatformPairs = {
'chrome@latest': 'Windows 8',
Expand Down Expand Up @@ -32,23 +35,24 @@ module.exports = function (config) {
preprocessors: {
'test/**/*.js': ['browserify']
},
browserify: {
browserify: Object.assign({
insertGlobalVars: bundlerOptions.insertGlobalVars
}, {
debug: true,
configure: function configure (b) {
b.ignore('glob')
.ignore('fs')
.ignore('path')
.ignore('supports-color')
.require(path.join(__dirname, 'node_modules', 'buffer'), {expose: 'buffer'})
build(b)
.on('bundled', function (err, content) {
if (!err && bundleDirpath) {
if (err) {
throw err;
}
if (bundleDirpath) {
// write bundle to directory for debugging
fs.writeFileSync(path.join(bundleDirpath,
'bundle.' + Date.now() + '.js'), content);
fs.writeFileSync(path.join(bundleDirpath, 'mocha.' + Date.now() +
'.js'), content);
}
});
}
},
}),
reporters: ['mocha'],
colors: true,
browsers: ['PhantomJS'],
Expand All @@ -63,18 +67,8 @@ module.exports = function (config) {
}
};

// see https://github.com/saucelabs/karma-sauce-example

// We define the browser to run on the Saucelabs Infrastructure
// via the environment variables BROWSER and PLATFORM.
// PLATFORM is e.g. "Windows"
// BROWSER is expected to be in the format "<name>@<version>",
// e.g. "MicrosoftEdge@latest"
// See https://wiki.saucelabs.com/display/DOCS/Platform+Configurator#/
// for available browsers.

// TO RUN LOCALLY, execute:
// `CI=1 SAUCE_USERNAME=<user> SAUCE_ACCESS_KEY=<key> BROWSER=<browser> PLATFORM=<platform> make test-browser`
// TO RUN AGAINST SAUCELABS LOCALLY, execute:
// `CI=1 SAUCE_USERNAME=<user> SAUCE_ACCESS_KEY=<key> make test-browser`
var env = process.env;
var sauceConfig;

Expand All @@ -86,8 +80,8 @@ module.exports = function (config) {
if (env.SAUCE_USERNAME && env.SAUCE_ACCESS_KEY) {
// correlate build/tunnel with Travis
sauceConfig = {
build: 'TRAVIS #' + env.TRAVIS_BUILD_NUMBER +
' (' + env.TRAVIS_BUILD_ID + ')',
build: 'TRAVIS #' + env.TRAVIS_BUILD_NUMBER + ' (' +
env.TRAVIS_BUILD_ID + ')',
tunnelIdentifier: env.TRAVIS_JOB_NUMBER,
startConnect: false
};
Expand All @@ -96,8 +90,7 @@ module.exports = function (config) {
console.error('No SauceLabs credentials present');
}
} else if (env.APPVEYOR) {
console.error('AppVeyor detected');
bundleDirpath = path.join(baseBundleDirpath, process.env.APPVEYOR_BUILD_ID);
throw new Error('no browser tests should run on AppVeyor!');
} else {
console.error('Local/unknown environment detected');
bundleDirpath = path.join(baseBundleDirpath, 'local');
Expand All @@ -124,21 +117,50 @@ module.exports = function (config) {
addSauceTests(cfg);
}

// the MOCHA_UI env var will determine if we're running interface-specific
// tests. since you can only load one at a time, each must be run separately.
// each has its own set of acceptance tests and a fixture.
// the "bdd" fixture is used by default.
var ui = env.MOCHA_UI;
if (ui) {
if (cfg.sauceLabs) {
cfg.sauceLabs.testName = 'Interface "' + ui + '" integration tests';
}
cfg.files = [
'test/browser-fixtures/' + ui + '.fixture.js',
'test/interfaces/' + ui + '.spec.js'
];
} else if (cfg.sauceLabs) {
cfg.sauceLabs.testName = 'Unit Tests';
/* the MOCHA_TEST env var will be set for "special" cases of tests.
* these may require different interfaces or other setup which make
* them unable to be batched w/ the rest.
*/
var MOCHA_TEST = env.MOCHA_TEST;
switch (MOCHA_TEST) {
case 'bdd':
case 'tdd':
case 'qunit':
if (cfg.sauceLabs) {
cfg.sauceLabs.testName =
'Interface "' + MOCHA_TEST + '" Integration Tests';
}
cfg.files = [
'test/browser-fixtures/' + MOCHA_TEST + '.fixture.js',
'test/interfaces/' + MOCHA_TEST + '.spec.js'
];
break;

case 'esm':
// for now we will only run against Chrome to test this.
if (cfg.sauceLabs) {
cfg.sauceLabs.testName = 'ESM Integration Tests';
cfg.browsers = ['chrome@latest'];
var launcher = cfg.customLaunchers['chrome@latest'];
cfg.customLaunchers = {
'chrome@latest': launcher
};
} else if (!env.TRAVIS) {
cfg.browsers = ['Chrome'];
} else {
console.error(
'skipping ESM tests & exiting; no SauceLabs nor local run detected');
process.exit(0);
}
cfg.files = [
'test/browser-fixtures/esm.fixture.html',
'test/browser-specific/esm.spec.js'
];
break;
default:
if (cfg.sauceLabs) {
cfg.sauceLabs.testName = 'Unit Tests';
}
}

config.set(cfg);
Expand Down
57 changes: 57 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@
"supports-color": "4.4.0"
},
"devDependencies": {
"aliasify": "^2.1.0",
"assert": "^1.4.1",
"browserify": "^14.4.0",
"buffer": "^4.9.1",
Expand Down
46 changes: 46 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env node
'use strict';

/**
* Mocha's build script which is sadly too complex to manage from the command
* line
* @type {Browserify}
*/

const browserify = require('browserify');
const path = require('path');
const dedefine = require('./dedefine');
const aliasify = require('aliasify');

const options = {
basedir: path.join(__dirname, '..'),
entries: ['./browser-entry.js'],
insertGlobalVars: {
Buffer (file, basedir) {
const filepath = path.join(path.relative(path.dirname(file), basedir),
'node_modules',
'buffer');
return `require('${filepath}').Buffer`;
}
}
};

const build = (b) => b.ignore('fs')
.ignore('glob')
.ignore('path')
.ignore('supports-color')
.transform(aliasify, {
replacements: {
'^buffer/?': () => require.resolve('buffer/index.js')
},
global: true
})
.plugin(dedefine);

exports.build = build;
exports.options = options;

if (require.main === module) {
build(browserify(options)).bundle()
.pipe(process.stdout);
}
7 changes: 7 additions & 0 deletions test/browser-fixtures/esm.fixture.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
delete window.require;
</script>
<script type="module">
import '/base/mocha.js';
window.MOCHA_IS_OK = true;
</script>
5 changes: 5 additions & 0 deletions test/browser-specific/esm.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

it('should register a global if it did not fail', function () {
expect(window.MOCHA_IS_OK).to.be.ok();
});

0 comments on commit 5fe7bad

Please sign in to comment.