-
Notifications
You must be signed in to change notification settings - Fork 826
/
test-integration.js
157 lines (133 loc) · 4.52 KB
/
test-integration.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
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
const clearModule = require('clear-module');
const execa = require('execa');
const glob = require('glob');
const ol = require('common-tags').oneLine;
const upath = require('upath');
const seleniumAssistant = require('selenium-assistant');
const constants = require('./utils/constants');
const logHelper = require('../infra/utils/log-helper');
const server = require('../infra/testing/server/index');
function runFiles(filePaths) {
// Mocha can't be run multiple times, which we need for NODE_ENV.
// More info: https://github.com/mochajs/mocha/issues/995
clearModule.all();
const Mocha = require('mocha');
return new Promise((resolve, reject) => {
const mocha = new Mocha({
retries: process.env.TRAVIS || process.env.GITHUB_ACTIONS ? 4 : 1,
timeout: 3 * 60 * 1000,
});
for (const filePath of filePaths) {
mocha.addFile(filePath);
}
// Run the tests.
mocha.run(function (failureCount) {
if (failureCount > 0) {
return reject(new Error(`${failureCount} tests failed.`));
}
resolve();
});
});
}
async function runTestSuite(testPath, nodeEnv, seleniumBrowser, webdriver) {
logHelper.log(ol`Running integration test on ${logHelper.highlight(testPath)}
with NODE_ENV '${nodeEnv}' and browser
'${logHelper.highlight(seleniumBrowser.getPrettyName())}'`);
const options = [];
if (global.cliOptions.grep) {
options.push('--grep', global.cliOptions.grep);
}
const originalNodeEnv = process.env.NODE_ENV;
process.env.NODE_ENV = nodeEnv;
try {
global.__workbox = {
seleniumBrowser,
server,
webdriver,
};
const testFiles = glob.sync(
upath.join(__dirname, '..', testPath, 'test-*.js'),
);
await runFiles(testFiles);
} finally {
process.env.NODE_ENV = originalNodeEnv;
}
}
async function runIntegrationForBrowser(browser) {
const packagesToTest = glob.sync(`test/${global.packageOrStar}/integration`);
for (const buildKey of Object.keys(constants.BUILD_TYPES)) {
const webdriver = await browser.getSeleniumDriver();
const timeout = 2 * 60 * 1000;
webdriver.manage().setTimeouts({
implicit: timeout,
pageLoad: timeout,
script: timeout,
});
for (const packageToTest of packagesToTest) {
const nodeEnv = constants.BUILD_TYPES[buildKey];
try {
await runTestSuite(packageToTest, nodeEnv, browser, webdriver);
} catch (error) {
await seleniumAssistant.killWebDriver(webdriver);
throw error;
}
}
await seleniumAssistant.killWebDriver(webdriver);
}
}
async function test_integration() {
if (process.platform === 'win32') {
logHelper.warn(`Skipping integration tests on Windows.`);
return;
}
// Install the latest Chrome and Firefox webdrivers without referencing
// package-lock.json, to ensure that they're up to date.
await execa('npm', ['install', '--no-save', 'chromedriver', 'geckodriver'], {
preferLocal: true,
});
logHelper.log(`Downloading browsers...`);
const expiration = 24;
await seleniumAssistant.downloadLocalBrowser('chrome', 'stable', expiration);
await seleniumAssistant.downloadLocalBrowser('chrome', 'beta', expiration);
await seleniumAssistant.downloadLocalBrowser('firefox', 'stable', expiration);
await seleniumAssistant.downloadLocalBrowser('firefox', 'beta', expiration);
const packagesToTest = glob.sync(`test/${global.packageOrStar}/integration`);
if (packagesToTest.length === 0) {
return;
}
await server.start();
try {
const localBrowsers = seleniumAssistant.getLocalBrowsers();
for (const localBrowser of localBrowsers) {
switch (localBrowser.getId()) {
case 'firefox':
if (localBrowser.getReleaseName() !== 'unstable') {
await runIntegrationForBrowser(localBrowser);
}
break;
// Temporarily only test the stable release of Chrome, until the next
// https://www.npmjs.com/package/chromedriver release.
case 'chrome':
case 'safari':
if (localBrowser.getReleaseName() === 'stable') {
await runIntegrationForBrowser(localBrowser);
}
break;
default:
logHelper.warn(ol`Skipping integration tests for
${localBrowser.getPrettyName()}.`);
}
}
} finally {
await server.stop();
}
}
module.exports = {
test_integration,
};