Skip to content

Commit 488dd4d

Browse files
committed
Adding better webpack bundling for tests and sinon support.
1 parent d01a36d commit 488dd4d

File tree

6 files changed

+37
-10
lines changed

6 files changed

+37
-10
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,19 @@ Building the source will drop under the 'lib' folder, preserving the directory s
141141
**/*.d.ts - All typescript definitions will be dropped as well.
142142
```
143143

144+
# Tests
145+
146+
In order for tests to work, we need to create a webpack bundle of all the tests and their dependencies. This process requires a tests.js file at
147+
your src root that has the following content:
148+
149+
```typescript
150+
// require all modules ending in ".test." from the
151+
// current directory and all subdirectories
152+
153+
var testsContext = require.context('../../lib', true, /.test.$/);
154+
155+
testsContext.keys().forEach(testsContext);
156+
```
144157

145158
# Adding custom build tasks to the task dependency tree
146159

karma.conf.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ let build = require('./lib/index');
77
let testConfig = build.config.test;
88
let path = require('path');
99
let bindPolyfillPath = require.resolve('phantomjs-polyfill/bind-polyfill.js');
10+
let debugRun = (process.argv.indexOf('--debug') > -1);
1011

11-
module.exports = function(config) {
12+
module.exports = function (config) {
1213

1314
config.set({
1415

@@ -18,12 +19,11 @@ module.exports = function(config) {
1819

1920
// frameworks to use
2021
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
21-
//frameworks: ['mocha', 'common_js'],
22-
frameworks: ['mocha'],
22+
frameworks: testConfig.frameworks,
2323

2424

2525
// list of files / patterns to load in the browser
26-
files: [ bindPolyfillPath ].concat(testConfig.paths.include),
26+
files: [bindPolyfillPath].concat(testConfig.paths.include),
2727

2828

2929
// list of files to exclude
@@ -33,8 +33,7 @@ module.exports = function(config) {
3333
// preprocess matching files before serving them to the browser
3434
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
3535
preprocessors: {
36-
'lib/**/!(*.test).js': ['coverage', 'webpack'],
37-
'**/*.test.js': ['webpack']
36+
[ testConfig.paths.include[0] ]: [ 'webpack' ]
3837
},
3938

4039

@@ -53,9 +52,15 @@ module.exports = function(config) {
5352
},
5453

5554
webpack: {
56-
// webpack configuration
55+
// webpack configuration
5756
module: {
58-
postLoaders: [{
57+
loaders: [
58+
{
59+
test: /sinon\.js$/,
60+
loader: "imports?define=>false"
61+
}
62+
],
63+
postLoaders: debugRun ? null : [{
5964
test: /\.js/,
6065
exclude: /(test|node_modules|bower_components)/,
6166
loader: require.resolve('istanbul-instrumenter-loader')
@@ -77,6 +82,7 @@ module.exports = function(config) {
7782

7883
plugins: [
7984
require('karma-mocha'),
85+
require('karma-sinon'),
8086
require('istanbul-instrumenter-loader'),
8187
require('karma-coverage'),
8288
require('karma-mocha-clean-reporter'),

lib/options/test.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
export interface ITestOptions {
2+
frameworks: string[];
23
paths: {
34
sourceMatch: string[];
45
include: string[];
56
exclude: string[];
67
};
78
}
89
declare var _default: {
10+
frameworks: string[];
911
paths: {
1012
sourceMatch: string[];
1113
include: string[];

lib/options/test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
Object.defineProperty(exports, "__esModule", { value: true });
22
exports.default = {
3+
frameworks: ['mocha', 'sinon'],
34
paths: {
45
sourceMatch: ['src/**/*.js'],
5-
include: ['lib/**/*.test.js'],
6+
include: ['lib/tests.js'],
67
exclude: []
78
}
89
};

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626
"gulp-texttojs": "^1.0.2",
2727
"gulp-tslint": "^3.3.0",
2828
"gulp-typescript": "^2.9.0",
29+
"istanbul-instrumenter-loader": "^0.1.3",
2930
"karma": "^0.13.10",
3031
"karma-coverage": "^0.5.2",
3132
"karma-mocha": "^0.2.0",
3233
"karma-mocha-clean-reporter": "0.0.1",
3334
"karma-phantomjs-launcher": "^0.2.1",
35+
"karma-sinon": "^1.0.4",
3436
"karma-webpack": "^1.7.0",
3537
"lodash": "^3.10.1",
3638
"merge2": "^0.3.6",
@@ -40,6 +42,7 @@
4042
"require-dir": "^0.3.0",
4143
"resolve": "^1.1.6",
4244
"run-sequence": "^1.1.4",
45+
"sinon": "^1.17.2",
4346
"systemjs-builder": "^0.14.9",
4447
"typescript": "^1.6.2",
4548
"webpack": "^1.12.9"

src/options/test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export interface ITestOptions {
2+
frameworks: string[];
23
paths: {
34
sourceMatch: string[];
45
include: string[];
@@ -7,9 +8,10 @@ export interface ITestOptions {
78
}
89

910
export default {
11+
frameworks: ['mocha', 'sinon'],
1012
paths: {
1113
sourceMatch: ['src/**/*.js'],
12-
include: ['lib/**/*.test.js'],
14+
include: ['lib/tests.js'],
1315
exclude: []
1416
}
1517
};

0 commit comments

Comments
 (0)