Skip to content

Commit ef642ff

Browse files
committed
Merge pull request #91 from fyockm/master
Don't skip async tests when calculating coverage
2 parents 374efd9 + 95b69fd commit ef642ff

9 files changed

Lines changed: 82 additions & 54 deletions

File tree

.jshintrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"expr": true,
3+
"jquery": true,
4+
"node": true
5+
}

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
language: node_js
2+
node_js:
3+
- '0.8'
4+
- '0.10'

lib/child.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,19 +120,19 @@ console.error = function(obj) {
120120
return error.apply(this, arguments);
121121
};
122122

123-
function run() {
124-
// require deps
125-
options.deps.forEach(function(dep) {
126-
_require(dep, true);
127-
});
123+
if (options.coverage) {
124+
coverage.instrument(options);
125+
}
128126

129-
// require code
130-
_require(options.code, true);
127+
// require deps
128+
options.deps.forEach(function(dep) {
129+
_require(dep, true);
130+
});
131131

132-
// require tests
133-
options.tests.forEach(function(res) {
134-
_require(res, false);
135-
});
136-
}
132+
// require code
133+
_require(options.code, true);
137134

138-
options.coverage ? coverage.instrument(options, run) : run();
135+
// require tests
136+
options.tests.forEach(function(test) {
137+
_require(test, false);
138+
});

lib/coverage.js

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ exports.add = function(coverage) {
1717
};
1818

1919
exports.get = function() {
20-
var summaries
20+
var summaries;
2121
if (collector) {
2222
summaries = [];
2323
collector.files().forEach(function(file) {
@@ -40,30 +40,14 @@ exports.report = function() {
4040
}
4141
};
4242

43-
function resolvePaths(files) {
44-
if (Array.isArray(files)) {
45-
return files.map(function (file) {
46-
return file.path;
47-
});
48-
}
49-
return [files.path];
50-
}
51-
52-
exports.instrument = function(options, callback) {
53-
istanbul.matcherFor({
54-
includes: resolvePaths(options.code),
55-
excludes: resolvePaths(options.tests)
56-
}, function (err, matcher) {
57-
var instrumenter
43+
exports.instrument = function(options) {
44+
var matcher, instrumenter;
5845

59-
if (err) {
60-
return callback(err)
61-
}
62-
63-
instrumenter = new istanbul.Instrumenter();
64-
istanbul.hook.hookRequire(matcher, instrumenter.instrumentSync.bind(instrumenter));
65-
callback();
66-
});
46+
matcher = function (file) {
47+
return file === options.code.path;
48+
}
49+
instrumenter = new istanbul.Instrumenter();
50+
istanbul.hook.hookRequire(matcher, instrumenter.instrumentSync.bind(instrumenter));
6751
};
6852

6953
if (!istanbul) {

lib/testrunner.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ function runOne(opts, callback) {
8888
callback(null, msg.data);
8989
kill();
9090
} else if (msg.event === 'uncaughtException') {
91-
callback(_.extend(new Error, msg.data));
91+
callback(_.extend(new Error(), msg.data));
9292
kill();
9393
}
9494
});
@@ -150,10 +150,10 @@ exports.run = function(files, callback) {
150150
files = [files];
151151
}
152152

153-
if (options.coverage) coverage.setup();
153+
if (options.coverage || files[0].coverage) coverage.setup();
154154

155155
files.forEach(function(file) {
156-
var opts = _.extend({}, options, file);
156+
var opts = _.extend({}, options, file);
157157

158158
!opts.log && (opts.log = {});
159159
opts.deps = absPaths(opts.deps);

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@
1919
"test": "make test"
2020
},
2121
"dependencies": {
22-
"underscore": "1.3.3",
23-
"argsparser": "0.0.6",
24-
"cli-table": "0.0.2",
25-
"tracejs": "0.1.4"
22+
"underscore": "1.3.3",
23+
"argsparser": "0.0.6",
24+
"cli-table": "0.0.2",
25+
"tracejs": "0.1.4"
2626
},
2727
"devDependencies": {
2828
"chainer": "0.0.5",
2929
"timekeeper": "0.0.2"
3030
},
3131
"optionalDependencies": {
32-
"istanbul": "0.2.4"
32+
"istanbul": "0.2.4"
3333
},
3434
"licenses": [
3535
{

test/fixtures/coverage-code.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
exports.myMethod = function() {
2+
return 123;
3+
};
4+
5+
exports.myAsyncMethod = function(callback) {
6+
setTimeout(function() {
7+
callback(123);
8+
}, 10000);
9+
};
10+
11+
exports.myOtherMethod = function(callback) {
12+
return 321
13+
};

test/fixtures/coverage-test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
test('myMethod test', function() {
2+
equal(myMethod(), 123, 'myMethod returns right result');
3+
});
4+
5+
test('myAsyncMethod test', function() {
6+
ok(true, 'myAsyncMethod started');
7+
8+
stop();
9+
expect(2);
10+
11+
myAsyncMethod(function(data) {
12+
equal(data, 123, 'myAsyncMethod returns right result');
13+
start();
14+
});
15+
});

test/testrunner.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,18 +155,26 @@ chain.add('uncaught exception', function() {
155155
chain.add('coverage', function() {
156156
tr.options.coverage = true;
157157
tr.run({
158-
code: fixtures + '/testrunner-code.js',
159-
tests: fixtures + '/testrunner-tests.js',
158+
code: fixtures + '/coverage-code.js',
159+
tests: fixtures + '/coverage-test.js'
160160
}, function(err, res) {
161-
var coverage = {
161+
var stat = {
162162
files: 1,
163-
statements: { covered: 4, total: 5 },
164-
branches: { covered: 0, total: 0 },
165-
functions: { covered: 2, total: 3 },
166-
lines: { covered: 4, total: 5 }
163+
tests: 2,
164+
assertions: 3,
165+
failed: 0,
166+
passed: 3,
167+
coverage: {
168+
files: 1,
169+
statements: { covered: 6, total: 7 },
170+
branches: { covered: 0, total: 0 },
171+
functions: { covered: 3, total: 4 },
172+
lines: { covered: 6, total: 7 }
173+
}
167174
};
175+
delete res.runtime;
168176
a.equal(err, null, 'no errors');
169-
a.deepEqual(coverage, res.coverage, 'Coverage calculated');
177+
a.deepEqual(stat, res, 'coverage code testing works');
170178
chain.next();
171179
});
172180
});
@@ -176,4 +184,3 @@ chain.add(function() {
176184
});
177185

178186
chain.start();
179-

0 commit comments

Comments
 (0)