A while back I wrote about In App Unit Tests and have been having too much fun creating little starter plunks on Plunker. So...
As a simple demonstration on in-app unit tests I've thrown together a little plunk that shows in-app tests within the a WinJS Pivot. And add to the layers of abstraction I'm using react-winjs which I'm LOVING over normal WinJS development.
Link to: JSPM/React/WinJS/Mocha Plunk
If you like this starter, I have it and a few more linked here: for re-use
I'd like to highlight this particular starter at bit more in this post, not only because there are a few more concepts to this basic plunk, but also because I'm fairly happy with the MochaJS React component that I've now copied around and re-used a few times in some small projects where use In App Unit Tests.
Plunker file overview
index.html
- The index file is a very basic JSPM bootstrap page that loads theapp.jsx
react component.app.jsx
- Defines a WinJS Pivot control where we render the in-appMochaTests.jsx
React component. This also defines the test file(s) and using MochaJS's global detection we can tell the reactMochaTests
component what test files to load and run as well as what globals are allowed to exist.config.js
- This is JSPM's config that defines what version of React we're using and to usebabel
for transpilation.tests.js
- is our Mocha set of unit tests. We can have multiple test files if we want, just have to define what files to load inapp.jsx
.
Lastly the MochaTests.jsx
which I'll include the full component below:
easier to copy-paste inherit for myself in the future
import mocha from 'mocha';
import React from 'react';
export default class MochaTests extends React.Component {
static get propTypes() {
return {
testScripts: React.PropTypes.array.isRequired,
allowedGlobals: React.PropTypes.array
};
}
constructor(props) {
super(props);
}
componentDidMount() {
var testScripts = this.props.testScripts;
var runTests = this.runTests.bind(this);
// for some reason importing mocha with JSPM and ES6 doesn't
// place the mocha globals on the window object. The below
// handles that for us - as well as setting up the rest of the
// test scripts for the first run
mocha.suite.on('pre-require', context => {
var exports = window;
exports.afterEach = context.afterEach || context.teardown;
exports.after = context.after || context.suiteTeardown;
exports.beforeEach = context.beforeEach || context.setup;
exports.before = context.before || context.suiteSetup;
exports.describe = context.describe || context.suite;
exports.it = context.it || context.test;
exports.setup = context.setup || context.beforeEach;
exports.suiteSetup = context.suiteSetup || context.before;
exports.suiteTeardown = context.suiteTeardown || context.after;
exports.suite = context.suite || context.describe;
exports.teardown = context.teardown || context.afterEach;
exports.test = context.test || context.it;
exports.run = context.run;
// now use SystemJS to load all test files
Promise
.all(testScripts.map(function(testScript) {
console.log("Adding Mocha Test File: ", testScript);
return System.import(testScript);
})).then(runTests, function(err) {
console.error("Error loading test modules");
console.error(err);
});
});
mocha.setup('bdd');
}
runTests() {
var allowedGlobals = this.props.allowedGlobals || [];
this.refs.mocha.getDOMNode().innerHTML = "";
mocha.checkLeaks();
mocha.globals(allowedGlobals);
mocha.run();
}
render() {
return (
<div>
<style>{"\
#mocha-stats em { \
color: inherit; \
} \
#mocha-stats { \
position: inherit; \
}\
#mocha .test.fail pre { \
color: red; \
} \
"}</style>
<button onClick={this.runTests.bind(this)}>Rerun Tests</button>
<div id="mocha" ref="mocha"></div>
</div>
);
}
}
Usage example of this React MochaTests
component.
// Define what test files get loaded by the MochaTests component
var testScripts = [
'./tests.js'
];
var allowedTestGlobals = [
// Declare what globals are allowed to be created during any test runs.
];
// Usage of MochaTests in a react render() method.
<MochaTests testScripts={testScripts} allowedGlobals={allowedTestGlobals} />
I'm not expecting to see a large up-tick in WinJS apps out there with in-app unit tests that run in the browser, however hopefully the MochaTests.jsx
React Component is of value to you and can be utilized outside of WinJS within almost any React app.
Please drop a line if you end up using it or if it can be adapted. If there's value in the component, maybe
Known Issue
If the number of tests starts to go beyond the height of the pivot in this sample, it has an issue where the WinJS Pivot cuts off at the bottom not allowing you to scroll and see the rest of the test output. I haven't dug into it yet because I've been clicking the failures: X
link and it filters the U.I. to just the erroring tests.
If you happen to come up with a good solution, drop me a note - I'd love it. Thanks in advance!
Happy Testing!