-
Notifications
You must be signed in to change notification settings - Fork 46
/
karma.conf.js
102 lines (97 loc) · 2.86 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
/* eslint-disable @typescript-eslint/no-require-imports */
const _ = require('lodash');
const getKarmaConfig = require('balena-config-karma');
const packageJSON = require('./package.json');
const BROWSER_BUNDLE = 'es2017/balena-browser.min.js';
module.exports = function (config) {
require('dotenv').config();
const envVars = [
'TEST_API_URL',
'TEST_EMAIL',
'TEST_PASSWORD',
'TEST_USERNAME',
'TEST_MEMBER_EMAIL',
'TEST_MEMBER_PASSWORD',
'TEST_MEMBER_USERNAME',
'TEST_2FA_EMAIL',
'TEST_2FA_PASSWORD',
'TEST_2FA_SECRET',
'TEST_REGISTER_EMAIL',
'TEST_REGISTER_PASSWORD',
'TEST_REGISTER_USERNAME',
];
const karmaConfig = getKarmaConfig(packageJSON);
karmaConfig.webpack.resolve.fallback = {
// required by: temp in tests
constants: false,
// required by: mockttp in tests
crypto: require.resolve('crypto-browserify'),
// required by: mocha.parallel in tests
domain: require.resolve('domain-browser'),
// used conditionally in the tests
fs: false,
// required by: ndjson
os: require.resolve('os-browserify'),
// used conditionally in the tests
path: false,
// required by: mockttp in tests
querystring: require.resolve('querystring-es3'),
// required by: balena-request
stream: require.resolve('stream-browserify'),
// required by tmp that we use in tests, but not when running on a browser
vm: false,
// required by mockttp -> http-encoding
zlib: false,
};
karmaConfig.webpack.plugins = [
new getKarmaConfig.webpack.ProvidePlugin({
// Polyfills or mocks for various node stuff
process: 'process/browser',
Buffer: ['buffer', 'Buffer'],
}),
new getKarmaConfig.webpack.DefinePlugin({
// The key needs to match how @balena/env-parsing references the env, since webpack replaces
// those references on build time.
'process.env': Object.fromEntries(
envVars.map((v) => [v, JSON.stringify(process.env[v])]),
),
}),
];
karmaConfig.webpack.module.rules.push({
test: /\.m?js/,
resolve: {
fullySpecified: false,
},
});
karmaConfig.webpack.experiments = {
asyncWebAssembly: true,
};
// do not pre-process the browser build
karmaConfig.preprocessors = _.omitBy(
karmaConfig.preprocessors,
(_value, key) => key.startsWith('es2017/') || key.startsWith('es2018/'),
);
karmaConfig.client = {
mocha: {
timeout: 5 * 60 * 1000,
retries: 2,
slow: 10 * 1000,
},
};
karmaConfig.files = [
BROWSER_BUNDLE,
'tests/**/*.spec.js',
'tests/**/*.spec.ts',
];
const { optionalVar } = require('@balena/env-parsing');
const TEST_ONLY_ON_ENVIRONMENT = optionalVar('TEST_ONLY_ON_ENVIRONMENT');
if (TEST_ONLY_ON_ENVIRONMENT && TEST_ONLY_ON_ENVIRONMENT !== 'browser') {
console.log(
`TEST_ONLY_ON_ENVIRONMENT is set to ${TEST_ONLY_ON_ENVIRONMENT}`,
);
console.log('Skipping browser tests');
karmaConfig.files = [];
karmaConfig.failOnEmptyTestSuite = false;
}
return config.set(karmaConfig);
};