forked from marsjaninzmarsa/polyfill-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
karma.conf.js
177 lines (155 loc) · 5.92 KB
/
karma.conf.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
175
176
177
// eslint-disable-next-line unicorn/prevent-abbreviations
"use strict";
const path = require('path');
const karmaPolyfillLibraryPlugin = require('./karma-polyfill-library-plugin');
const globby = require('globby');
const proclaim = path.resolve(require.resolve('proclaim'));
const fs = require('fs');
function getBrowsersFor(feature) {
const UA = require('@financial-times/polyfill-useragent-normaliser');
const TOML = require('@iarna/toml');
// Grab all the browsers from BrowserStack which are officially supported by the polyfil service.
const browserlist = TOML.parse(fs.readFileSync("./test/polyfills/browsers.toml", 'utf-8'));
const browserstackBrowsers = TOML.parse(fs.readFileSync('./test/polyfills/browserstackBrowsers.toml', 'utf-8'));
const browsersWeSupport = browserlist.browsers.filter(uaString => new UA(uaString).meetsBaseline());
const browsersWeSupportForThisFeature = browsersWeSupport.filter(uaString => {
const meta = TOML.parse(fs.readFileSync(path.resolve(__dirname, 'polyfills', feature, 'config.toml'), 'utf-8'));
const ua = new UA(uaString);
const isBrowserMatch = meta.browsers && meta.browsers[ua.getFamily()] && ua.satisfies(meta.browsers[ua.getFamily()]);
return isBrowserMatch;
});
function useragentToBrowserObject(browserWithVersion) {
const [browser, version] = browserWithVersion.split("/");
const browserObject = browserstackBrowsers.browsers.find(browserObject => {
if (browser === browserObject.os && version === browserObject.os_version) {
return true;
} else if (browser === browserObject.browser && version === browserObject.browser_version) {
return true;
} else {
return false;
}
});
if (browserObject) {
return Object.assign({
name: browserWithVersion.replace(/\//g, '-').replace(/\./g, '_'),
base: 'BrowserStack'
}, browserObject);
} else {
throw new Error(`Browser: ${browser} with version ${version} was not found on BrowserStack.`);
}
}
const browsersWeSupportInBrowserStack = {};
for (const browser of browsersWeSupportForThisFeature.map((element) => useragentToBrowserObject(element))) {
browsersWeSupportInBrowserStack[browser.name] = browser;
}
return browsersWeSupportInBrowserStack;
}
function generateKarmaConfigTestFiles(config) {
let features = config.features;
features = features.split(',');
features = features.map(feature => feature.trim());
features = features.map(feature => feature.replace(/\./g, path.sep));
features = features.map(feature => path.join(__dirname, './polyfills', feature, 'tests.js'));
const featureTests = globby.sync(features);
return featureTests.map((element) => createKarmaFileObject(element));
}
function createKarmaFileObject(filePattern) {
return {
pattern: filePattern,
included: true,
served: true,
watched: false
};
}
module.exports = async function (config) {
if (!config.features) {
console.error('Missing the `--features` flag. `--features` needs to be set to the names of the features being tested. E.G. `npm run test-feature -- --features=Array.from,Array.prototype.forEach`');
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
}
config.set({
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
browserDisconnectTimeout: 60 * 1000, // default 2000
browserDisconnectTolerance: 5, // default 0
browserNoActivityTimeout: 60 * 1000, // default 10000
browserSocketTimeout: 60 * 1000, // default 20000
// enable / disable colors in the output (reporters and logs)
colors: true,
// list of files / patterns to load in the browser
files: [
proclaim,
...generateKarmaConfigTestFiles(config)
],
beforeMiddleware: ['polyfill-library'],
// We need to add mocha after polyfill-library to ensure that the scripts loaded in the browser are in the correct order.
// TODO: This is really a bug in the Symbol polyfill that we should fix, which is that it adds enumerable properties onto Object.prototype which gets exposed in `for (var o in {})`.
frameworks: [
"polyfill-library",
"mocha"
],
reporters: ['mocha'],
mochaReporter: {
output: 'minimal'
},
// web server port
port: 9876,
plugins: [
'karma-mocha',
'karma-mocha-reporter',
karmaPolyfillLibraryPlugin,
'karma-summary-optional-console-reporter'
],
logLevel: config.LOG_WARN,
client: {
mocha: {
opts: 'mocha.opts',
// change Karma's debug.html to the mocha web reporter so we can see the test results in page instead of in the console
reporter: 'html'
}
},
// Run the tests inside a new window instead of in an iFrame
useIframe: false
});
const features = config.features.split(',').map(feature => feature.trim());
const feature = features[0];
// eslint-disable-next-line unicorn/consistent-function-scoping
const featureToFolder = feature => feature.replace(/\./g, path.sep);
if (config.browserstack) {
const browsers = getBrowsersFor(featureToFolder(feature));
if (Object.keys(browsers).length === 0) {
console.log('No browsers we support require this polyfill, not running the tests');
// eslint-disable-next-line unicorn/no-process-exit
process.exit(0);
}
config.set(Object.assign(config,{
// if true, Karma captures browsers, runs the tests and exits
singleRun: true,
plugins: [...config.plugins,
'karma-browserstack-launcher'
],
browserStack: {
startTunnel: true,
name: feature,
project: 'polyfill-library',
retryLimit: 10
},
reporters: [...config.reporters, 'summary-optional-console', 'BrowserStack'],
summaryOptionalConsoleReporter: {
// 'failed', 'skipped' or 'all'
show: 'failed',
// Limit the spec label to this length
specLength: 50,
// Show an 'all' column as a summary
overviewColumn: false,
// Print console log/error messages
consoleLogs: true,
// Print a summary line for each browser
browserSummary: true
},
concurrency: 5,
customLaunchers: browsers,
browsers: Object.keys(browsers)
}));
}
};