-
-
Notifications
You must be signed in to change notification settings - Fork 218
/
Gruntfile.js
143 lines (130 loc) · 4.91 KB
/
Gruntfile.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
/* globals module, process, require */
'use strict';
module.exports = function (grunt) {
var headless = require('mocha-headless-chrome'),
_ = require('lodash'),
fs = require('fs');
// use localhost unless we're running on travis
var BASE_ADDRESS = process.env.WEB_TEST_PORT_8000_TCP_ADDR || 'localhost',
BASE_URL = 'http://' + BASE_ADDRESS + ':8000/mocha/';
/*
* To run all tests:
* grunt test
*
* To run a single test:
* grunt test:<app>
*
* To add a new app to test:
* - Add the app name to this list
* - Create a test runner view at corehq/apps/<app>/templates/<app>/spec/mocha.html
* - Test in the browser at http://localhost:8000/mocha/<app>
*
* To add a new section to an existing app:
* - Add <app>/<section> to this list
* - Create a test runner view at corehq/apps/<app>/templates/<app>/spec/<section>/mocha.html
* - Test in the browser at http://localhost:8000/mocha/<app>/<section>
*/
var apps = [
'app_manager',
'export/ko',
'notifications/bootstrap3',
'notifications/bootstrap5',
'reports_core/choiceListUtils',
'locations',
'userreports',
'cloudcare',
'cloudcare/form_entry',
'hqwebapp/bootstrap3',
'hqwebapp/bootstrap5',
'case_importer',
];
var extensions = _.split(process.env.JS_TEST_EXTENSIONS || '', ','),
testPaths = _.filter(_.concat(apps, extensions), function (path) { return path !== ''; });
var runTest = function (queuedTests, taskPromise, finishedTests, failures) {
if (finishedTests === undefined) {
finishedTests = [];
failures = {};
}
if (queuedTests.length === 0) {
if (!_.isEmpty(failures)) {
printFailures(failures);
grunt.fail.fatal("Javascript tests failed.");
}
taskPromise();
return;
}
var currentApp = queuedTests[0],
currentTestPath = BASE_URL + currentApp,
testText = "Running Test '" + currentApp + "'",
reporter = grunt.option('verbose') ? 'spec' : 'dot',
runnerOptions = {
file: currentTestPath,
visible: false,
timeout: 120000,
reporter: reporter,
};
// For running in docker
if (process.env.PUPPETEER_SKIP_DOWNLOAD) {
runnerOptions.executablePath = 'google-chrome-stable';
}
grunt.log.writeln("\n");
grunt.log.writeln(testText.bold);
grunt.log.write(currentTestPath.italic.cyan);
headless.runner(runnerOptions).then(function (data) {
if (data.result.failures.length) {
failures[currentApp] = data.result.failures;
}
if (grunt.option('coverage')) {
var coverageDir = './coverage-js/',
filePath = coverageDir + currentApp.replace(/\//g, '-') + '.json';
if (!fs.existsSync(coverageDir)) {
fs.mkdir(coverageDir, error =>
error && grunt.log.write(error));
}
fs.writeFile(filePath, JSON.stringify(data.coverage), { flag: 'w+' }, error =>
error && grunt.log.write(error)
);
}
finishedTests.push(currentApp);
runTest(
_.without(queuedTests, currentApp),
taskPromise,
finishedTests,
failures
);
});
};
var printFailures = function (failures) {
grunt.log.writeln("\n");
var numFailures = _.flatten(_.values(failures)).length + " test(s) failed.";
grunt.log.writeln(numFailures.bold.red);
_.forIn(failures, function (errors, appName) {
grunt.log.writeln("\n");
var failSummary = " has " + errors.length + " error(s)";
grunt.log.write(appName.bold);
grunt.log.writeln(failSummary.bold.red);
_.each(errors, function (error) {
grunt.log.write("\nERROR: ".bold.red);
grunt.log.writeln(error.fullTitle);
grunt.log.error(error.err.stack);
grunt.log.write("\n");
});
});
};
grunt.task.registerTask(
'test',
'Runs Javascript Tests. Pass in an argument to run a specific test',
function (arg) {
var paths = testPaths;
if (arg) {
paths = [arg];
}
var testStatement = "Running tests: " + paths.join(', ');
grunt.log.writeln(testStatement.bold.green);
runTest(paths, this.async());
}
);
grunt.registerTask('list', 'Lists all available apps to test', function () {
testPaths.forEach(function (app) { console.log('"' + app + '"'); });
});
};