Skip to content

Commit ffa5114

Browse files
committed
Added a testSetup file to configure Mocha, added scripts to the package.json file to execute Mocha, and added a couple of example unit tests - to exercise Mocha and JSDOM
1 parent f5b72b5 commit ffa5114

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

buildScripts/testSetup.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// This file isn't transpiled, so must use CommonJS and ES5
2+
3+
// Register babel to transpile before our tests run
4+
require('babel-register')();
5+
6+
// Disable webpack features that Mocha doesn't understand
7+
require.extensions['.css'] = function() {};

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
"description": "JavaScript development environment Pluralsight course by Cory House",
55
"scripts": {
66
"prestart": "babel-node buildScripts/startMessage.js",
7-
"start":"npm-run-all --parallel security-check open:src lint:watch",
7+
"start":"npm-run-all --parallel security-check open:src lint:watch test:watch",
88
"open:src": "babel-node buildScripts/srcServer.js",
99
"lint": "esw webpack.config.* src buildScripts --color",
1010
"lint:watch": "npm run lint -- --watch",
1111
"security-check": "nsp check",
1212
"localtunnel": "lt --port 3000 --subdomain kevin",
13-
"share": "npm-run-all --parallel open:src localtunnel"
13+
"share": "npm-run-all --parallel open:src localtunnel",
14+
"test": "mocha --reporter progress buildScripts/testSetup.js \"src/**/*.test.js\"",
15+
"test:watch": "npm test -- --watch"
1416
},
1517
"author": "Cory House",
1618
"license": "MIT",

src/index.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {expect} from 'chai';
2+
import jsdom from 'jsdom';
3+
import fs from 'fs';
4+
5+
describe('Our first test', function() {
6+
it('should pass', function() {
7+
expect(true).to.equal(true);
8+
});
9+
});
10+
11+
describe('index.html', function() {
12+
it('should say hello', function(done) {
13+
const index = fs.readFileSync('./src/index.html', 'utf-8');
14+
jsdom.env(index, function(err, window) {
15+
const h1 = window.document.getElementsByTagName('h1')[0];
16+
expect(h1.innerHTML).to.equal("Hello World!!!");
17+
done();
18+
window.close();
19+
});
20+
});
21+
});

0 commit comments

Comments
 (0)