Skip to content

Commit 0520ef9

Browse files
committed
fix async coverage tests
1 parent 2a63c08 commit 0520ef9

5 files changed

Lines changed: 44 additions & 56 deletions

File tree

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(test) {
134-
_require(test, 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: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {

test/fixtures/coverage-code.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ exports.myMethod = function() {
44

55
exports.myAsyncMethod = function(callback) {
66
setTimeout(function() {
7-
callback(123);
7+
callback(123);
88
}, 10000);
9-
};
9+
};
10+
11+
exports.myOtherMethod = function(callback) {
12+
return 321
13+
};

test/fixtures/coverage-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ test('myAsyncMethod test', function() {
99
expect(2);
1010

1111
myAsyncMethod(function(data) {
12-
equal(data, 123, 'myAsyncMethod returns right result');
13-
start();
12+
equal(data, 123, 'myAsyncMethod returns right result');
13+
start();
1414
});
1515
});

test/testrunner.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -155,26 +155,26 @@ chain.add('uncaught exception', function() {
155155
chain.add('coverage', function() {
156156
tr.options.coverage = true;
157157
tr.run({
158-
code: fixtures + '/coverage-code.js',
159-
tests: fixtures + '/coverage-test.js'
158+
code: fixtures + '/coverage-code.js',
159+
tests: fixtures + '/coverage-test.js'
160160
}, function(err, res) {
161-
var stat = {
161+
var stat = {
162162
files: 1,
163-
tests: 2,
164-
assertions: 3,
165-
failed: 0,
166-
passed: 3,
167-
coverage: {
168-
files: 1,
169-
statements: { covered: 4, total: 5 },
170-
branches: { covered: 0, total: 0 },
171-
functions: { covered: 2, total: 3 },
172-
lines: { covered: 4, total: 5 }
173-
}
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+
}
174174
};
175-
delete res.runtime;
175+
delete res.runtime;
176176
a.equal(err, null, 'no errors');
177-
a.deepEqual(stat, res, 'coverage code testing works');
177+
a.deepEqual(stat, res, 'coverage code testing works');
178178
chain.next();
179179
});
180180
});

0 commit comments

Comments
 (0)