File: test-cover-command.js

package info (click to toggle)
node-istanbul 0.4.5%2Bds%2B~cs56.14.45-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,664 kB
  • sloc: javascript: 23,438; sh: 28; makefile: 8; xml: 1
file content (266 lines) | stat: -rw-r--r-- 13,118 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*jslint nomen: true */
var path = require('path'),
    fs = require('fs'),
    rimraf = require('rimraf'),
    mkdirp = require('mkdirp'),
    COMMAND = 'cover',
    DIR = path.resolve(__dirname, 'sample-project'),
    DIR_LINK = path.resolve(__dirname, 'sample-project-link'),
    OUTPUT_DIR = path.resolve(DIR, 'coverage'),
    helper = require('../cli-helper'),
    existsSync = fs.existsSync || path.existsSync,
    run = helper.runCommand.bind(null, COMMAND),
    Report = require('../../lib/report');

module.exports = {
    setUp: function (cb) {
        rimraf.sync(OUTPUT_DIR);
        mkdirp.sync(OUTPUT_DIR);
        helper.resetOpts();
        cb();
    },
    tearDown: function (cb) {
        rimraf.sync(OUTPUT_DIR);
        cb();
    },
    /*
    "should cover tests as expected": function (test) {
        helper.setOpts({ lazyHook : true });
        run([ 'test/run.js', '-v' ], function (results) {
            test.ok(results.succeeded());
            test.ok(results.grepError(/Module load hook:/));
            test.ok(existsSync(path.resolve(OUTPUT_DIR, 'lcov.info')));
            test.ok(existsSync(path.resolve(OUTPUT_DIR, 'lcov-report')));
            test.ok(existsSync(path.resolve(OUTPUT_DIR, 'coverage.json')));
            var coverage = JSON.parse(fs.readFileSync(path.resolve(OUTPUT_DIR, 'coverage.json'), 'utf8')),
                filtered;
            filtered = Object.keys(coverage).filter(function (k) { return k.match(/foo/) || k.match(/bar/); });
            test.ok(filtered.length === 2);
            test.done();
        });
    },
    "should cover tests running every possible report": function (test) {
        helper.setOpts({ lazyHook : true });
        var cmd = [ 'test/run.js', '-v', '--print=none' ];
        Report.getReportList().forEach(function (r) {
            cmd.push('--report=' + r);
        });
        run(cmd, function (results) {
            test.ok(results.succeeded());
            test.ok(existsSync(path.resolve(OUTPUT_DIR, 'lcov.info')));
            test.ok(existsSync(path.resolve(OUTPUT_DIR, 'lcov-report')));
            test.ok(existsSync(path.resolve(OUTPUT_DIR, 'coverage.json')));
            test.ok(existsSync(path.resolve(OUTPUT_DIR, 'coverage-final.json')));
            test.ok(existsSync(path.resolve(OUTPUT_DIR, 'cobertura-coverage.xml')));
            test.ok(existsSync(path.resolve(OUTPUT_DIR, 'clover.xml')));
            test.done();
        });
    },
    "should include all files after running tests": function (test) {
        helper.setOpts({ lazyHook : true });
        run([ 'test/run.js', '0', '-v', '--include-all-sources', '-x', 'lib/util/bad.js', '-x', 'lib/util/es-module.js' ], function (results) {
            test.ok(results.succeeded());
            test.ok(existsSync(path.resolve(OUTPUT_DIR, 'lcov.info')));
            test.ok(existsSync(path.resolve(OUTPUT_DIR, 'lcov-report')));
            test.ok(existsSync(path.resolve(OUTPUT_DIR, 'coverage.json')));
            var coverage = JSON.parse(fs.readFileSync(path.resolve(OUTPUT_DIR, 'coverage.json'), 'utf8')),
                unloadedFilePath = path.resolve(OUTPUT_DIR, '..', 'includeAllSources', 'unloadedFile.js'),
                unloadedFileWithFnPath = path.resolve(OUTPUT_DIR, '..', 'includeAllSources', 'unloadedFileWithFunctionDeclaration.js'),
                unloadedFile = coverage[unloadedFilePath],
                unloadedFileWithFn = coverage[unloadedFileWithFnPath];

            Object.keys(unloadedFile.s).forEach(function (statement) {
                test.ok(unloadedFile.s[statement] === 0);
            });
            Object.keys(unloadedFileWithFn.s).forEach(function (statement) {
                test.ok(unloadedFileWithFn.s[statement] === 0);
            });

            test.done();
        });
    },
    "should include all files after running tests in back-compat mode": function (test) {
        helper.setOpts({ lazyHook : true });
        run([ 'test/run.js', '0', '-v', '--preload-sources', '-x', 'lib/util/bad.js', '-x', 'lib/util/es-module.js' ], function (results) {
            test.ok(results.succeeded());
            test.ok(existsSync(path.resolve(OUTPUT_DIR, 'lcov.info')));
            test.ok(existsSync(path.resolve(OUTPUT_DIR, 'lcov-report')));
            test.ok(existsSync(path.resolve(OUTPUT_DIR, 'coverage.json')));
            var coverage = JSON.parse(fs.readFileSync(path.resolve(OUTPUT_DIR, 'coverage.json'), 'utf8')),
                unloadedFilePath = path.resolve(OUTPUT_DIR, '..', 'includeAllSources', 'unloadedFile.js'),
                unloadedFileWithFnPath = path.resolve(OUTPUT_DIR, '..', 'includeAllSources', 'unloadedFileWithFunctionDeclaration.js'),
                unloadedFile = coverage[unloadedFilePath],
                unloadedFileWithFn = coverage[unloadedFileWithFnPath];

            Object.keys(unloadedFile.s).forEach(function (statement) {
                test.ok(unloadedFile.s[statement] === 0);
            });
            Object.keys(unloadedFileWithFn.s).forEach(function (statement) {
                test.ok(unloadedFileWithFn.s[statement] === 0);
            });

            test.ok(results.grepError(/The preload-sources option is deprecated/));
            test.done();
        });
    },
    */
    //"should cover tests as expected without extra noise and not covering excluded files": function (test) {
    //    helper.setOpts({ lazyHook : true });
    //    run([ 'test/run.js', '-x', '**/foo.js' ], function (results) {
    //        test.ok(results.succeeded());
    //        test.ok(!results.grepError(/Module load hook:/));
    //        test.ok(existsSync(path.resolve(OUTPUT_DIR, 'lcov.info')));
    //        test.ok(existsSync(path.resolve(OUTPUT_DIR, 'lcov-report')));
    //        test.ok(existsSync(path.resolve(OUTPUT_DIR, 'coverage.json')));
    //        var coverage = JSON.parse(fs.readFileSync(path.resolve(OUTPUT_DIR, 'coverage.json'), 'utf8')),
    //            filtered;
    //        filtered = Object.keys(coverage).filter(function (k) { return k.match(/foo/); });
    //        test.ok(filtered.length === 0);
    //        test.done();
    //    });
    //},
    //"should cover tests as expected without extra noise and using includes": function (test) {
    //    helper.setOpts({ lazyHook : true });
    //    run([ 'test/run.js', '-i', '**/foo.js' ], function (results) {
    //        test.ok(results.succeeded());
    //        test.ok(!results.grepError(/Module load hook:/));
    //        test.ok(existsSync(path.resolve(OUTPUT_DIR, 'lcov.info')));
    //        test.ok(existsSync(path.resolve(OUTPUT_DIR, 'lcov-report')));
    //        test.ok(existsSync(path.resolve(OUTPUT_DIR, 'coverage.json')));
    //        var coverage = JSON.parse(fs.readFileSync(path.resolve(OUTPUT_DIR, 'coverage.json'), 'utf8')),
    //            filtered;
    //        filtered = Object.keys(coverage).filter(function (k) { return k.match(/foo/); });
    //        test.ok(filtered.length !== 0);
    //        filtered = Object.keys(coverage).filter(function (k) { return k.match(/bar/); });
    //        test.ok(filtered.length === 0);
    //        test.done();
    //    });
    //},
    "should skip reporting when requested": function (test) {
        helper.setOpts({ lazyHook : true });
        run([ 'test/run.js', '--report', 'none', '--print', 'detail' ], function (results) {
            test.ok(results.succeeded());
            test.ok(!existsSync(path.resolve(OUTPUT_DIR, 'lcov.info')));
            test.ok(!existsSync(path.resolve(OUTPUT_DIR, 'lcov-report')));
            test.ok(existsSync(path.resolve(OUTPUT_DIR, 'coverage.json')));
            test.done();
        });
    },
    "should use non-default report format when requested": function (test) {
        helper.setOpts({ lazyHook : true });
        run([ 'test/run.js', '--report', 'lcovonly' ], function (results) {
            test.ok(results.succeeded());
            test.ok(existsSync(path.resolve(OUTPUT_DIR, 'lcov.info')));
            test.ok(!existsSync(path.resolve(OUTPUT_DIR, 'lcov-report')));
            test.ok(existsSync(path.resolve(OUTPUT_DIR, 'coverage.json')));
            test.done();
        });
    },
    "should cover nothing when everything excluded": function (test) {
        helper.setOpts({ lazyHook : true });
        run([ 'test/run.js', '-x', '**/*.js' ], function (results) {
            test.ok(results.succeeded());
            test.ok(!existsSync(path.resolve(OUTPUT_DIR, 'lcov.info')));
            test.ok(!existsSync(path.resolve(OUTPUT_DIR, 'lcov-report')));
            test.ok(!existsSync(path.resolve(OUTPUT_DIR, 'coverage.json')));
            test.done();
        });
    },
    /*
    "should cover everything under the sun when default excludes are suppressed": function (test) {
        helper.setOpts({ lazyHook : true });
        run([ 'test/run.js', '--no-default-exclude' ], function (results) {
            test.ok(results.succeeded());
            test.ok(existsSync(path.resolve(OUTPUT_DIR, 'lcov.info')));
            test.ok(existsSync(path.resolve(OUTPUT_DIR, 'lcov-report')));
            test.ok(existsSync(path.resolve(OUTPUT_DIR, 'coverage.json')));
            var coverage = JSON.parse(fs.readFileSync(path.resolve(OUTPUT_DIR, 'coverage.json'), 'utf8')),
                filtered;
            filtered = Object.keys(coverage).filter(function (k) { return k.match(/node_modules/); });
            test.ok(filtered.length === 1);
            test.done();
        });
    },
    */
    "should barf when no file is provided": function (test) {
        run([], function (results) {
            test.ok(!results.succeeded());
            test.ok(results.grepError(/Need a filename argument for the cover command/));
            test.done();
        });
    },
    "should barf on invalid command": function (test) {
        run([ 'foobar' ], function (results) {
            test.ok(!results.succeeded());
            test.ok(results.grepError(/Unable to resolve file/));
            test.done();
        });
    },
    /*
    "should work with RequireJS and AMD modules": function (test) {
        helper.setOpts({ lazyHook : true });
        run([ 'test/amd-run.js', '-v', '--hook-run-in-context' ], function (results) {
            test.ok(results.succeeded());
            test.ok(results.grepError(/Module load hook:/));
            test.ok(existsSync(path.resolve(OUTPUT_DIR, 'lcov.info')));
            test.ok(existsSync(path.resolve(OUTPUT_DIR, 'lcov-report')));
            test.ok(existsSync(path.resolve(OUTPUT_DIR, 'coverage.json')));
            var coverage = JSON.parse(fs.readFileSync(path.resolve(OUTPUT_DIR, 'coverage.json'), 'utf8')),
                ipsumPath = path.join('amd', 'ipsum'),
                loremPath = path.join('amd', 'lorem'),
                filtered;
            filtered = Object.keys(coverage).filter(function (k) { return k.indexOf(ipsumPath) >= 0 || k.indexOf(loremPath) >= 0; });
            test.ok(filtered.length === 2);
            test.ok(filtered.length === Object.keys(coverage).length);
            test.done();
        });
    },
    */
    "should barf when  post-require-hook not available": function (test) {
        run([ 'test/run.js', '-v', '-x', '**/foo.js', '--post-require-hook', 'does-not-exist' ], function (results) {
            test.ok(!results.succeeded());
            test.ok(results.grepError(/Unable to resolve \[does-not-exist\] as a node module/));
            test.done();
        });
    },
    /*
    "should not introduce globals in the middle of running a test": function (test) {
        helper.setOpts({ lazyHook : true });
        run([ 'test/global-leak.js', '-v' ], function (results) {
            test.ok(results.succeeded());
            test.done();
        });
    },
    "should cover tests when under symlink": function (test) {
        try {
            fs.symlinkSync(DIR, DIR_LINK);
        } catch (ex) {
            if (ex.code === 'EPERM') {
                console.error('#');
                console.error('# Skipping symlink test');
                console.error('# ' + ex.message);
                console.error('#');
                test.ok(true);
                test.done();
            } else {
              throw ex;
            }
            return;
        }
        helper.setOpts({ cwd: DIR_LINK, lazyHook : true });
        run([ 'test/run.js', '-v' ], function (results) {
            test.ok(results.succeeded());
            test.ok(results.grepError(/Module load hook:/));
            test.ok(existsSync(path.resolve(OUTPUT_DIR, 'lcov.info')));
            test.ok(existsSync(path.resolve(OUTPUT_DIR, 'lcov-report')));
            test.ok(existsSync(path.resolve(OUTPUT_DIR, 'coverage.json')));
            var coverage = JSON.parse(fs.readFileSync(path.resolve(OUTPUT_DIR, 'coverage.json'), 'utf8')),
                filtered;
            filtered = Object.keys(coverage).filter(function (k) { return k.match(/foo/) || k.match(/bar/); });
            test.ok(filtered.length === 2);
            fs.unlinkSync(DIR_LINK);
            test.done();
        });
    }
    */
};