-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
174 lines (138 loc) · 4.84 KB
/
test.js
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
const SpecReporter = require('jasmine-spec-reporter');
const chalk = require('chalk');
const istanbul = require('gulp-istanbul');
const tap = require('gulp-tap');
const plumber = require('gulp-plumber');
const jasmine = require('gulp-jasmine');
const gulpRemapIstanbul = require('remap-istanbul/lib/gulpRemapIstanbul');
const fs = require('fs');
const path = require('path');
const merge = require('gulp-merge-json');
const replace = require('gulp-replace');
const KarmaServer = require('karma').Server;
const {build} = require('./build');
const {clean} = require('./clean');
function task(cli, project) {
cli.command('test [environment]', 'Run tests')
.option('-s, --serial', 'Run environments in serial (default is parallel)')
.action(function (args, callback) {
return clean(project, this, ['coverage', 'dist'])
.then(() => {
let testPromiseFactories = [];
if (!args.environment || args.environment == 'server') {
let serverTest = build(project, this, 'server')
.then(() => instrumentServer(project, this))
.then(() => testServer(project, this));
testPromiseFactories.push(() => serverTest);
}
if (!args.environment || args.environment == 'browser') {
testPromiseFactories.push(() => testBrowser(project, this));
}
if (args.options.serial) {
//run promises in serial
return testPromiseFactories.reduce((prior, next) => {
return prior.then(() => next());
}, Promise.resolve()); // initial
}
return Promise.all(testPromiseFactories.map(pf => pf()));
})
.then(() => remapCoverage(project, this));
});
}
function instrumentServer(project, cli) {
return new Promise((resolve, reject) => {
cli.log('Instrumenting server files');
const config = project.paths.destination.server + '/**/*.js';
project.gulp.src(config, {cwd: project.basePath})
// Covering files
.pipe(istanbul())
// Force `require` to return covered files
.pipe(istanbul.hookRequire())
.on('finish', resolve);
});
}
/**
* Run tests for server
* @param project
* @param cli
* @returns {Promise}
*/
function testServer(project, cli) {
return new Promise((resolve, reject) => {
cli.log('Testing server');
const config = {
source: [
project.paths.destination.server + '/**/*.js',
'!' + project.paths.destination.server + '/**/bootstrap.js'
],
coverage: project.paths.destination.coverage + '/server/js'
};
cli.log('Testing server');
Error.stackTraceLimit = Infinity;
require('core-js');
require('reflect-metadata');
require('zone.js/dist/zone-node');
require('zone.js/dist/async-test');
project.gulp.src(config.source, {cwd: project.basePath})
.pipe(plumber(reject))
// Run specs
.pipe(jasmine({
verbose: true,
reporter: new SpecReporter({
displayFailuresSummary: false
})
})
)
// Creating the reports after tests ran
.pipe(istanbul.writeReports({
dir: config.coverage,
reporters: ['json']
}))
.on('end', resolve);
});
}
function testBrowser(project, cli) {
return new Promise((resolve, reject) => {
cli.log('Testing browser');
new KarmaServer({
configFile: path.resolve(__dirname, '../..', 'browser/karma.conf.js'),
basePath: project.basePath,
singleRun: true,
}, () => {
cli.log('Test complete');
resolve();
}).start();
});
}
function remapCoverage(project, cli) {
return new Promise((resolve, reject) => {
cli.log('Building coverage output');
const config = {
source: [
project.resolvePath('./coverage/**/js/coverage-final.json'),
],
coverage: project.paths.destination.coverage
};
project.gulp.src(config.source, {cwd: project.basePath})
.pipe(plumber(reject))
.pipe(tap((file) => {
cli.log(chalk.blue('[merge]'), chalk.cyan('merging coverage info:', file.path));
}))
.pipe(merge('summary.json'))
.pipe(project.gulp.dest(project.paths.destination.coverage))
.pipe(gulpRemapIstanbul({
reports: {
'json': project.resolvePath('./coverage/summary/coverage.json'),
'html': project.resolvePath('./coverage/summary/html-report'),
'text': project.resolvePath('./coverage/summary/text-summary', true),
'lcovonly': project.resolvePath('./coverage/summary/lcov.info')
}
}))
.on('finish', () => {
cli.log(fs.readFileSync(project.resolvePath('./coverage/summary/text-summary'))
.toString());
resolve();
});
});
}
module.exports = {task};