/
karma-tests.ts
217 lines (189 loc) · 6.63 KB
/
karma-tests.ts
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import karma = require('karma');
// karma.server is deprecated and will eventually be removed
karma.server.start({ port: 9876 }, (exitCode: number) => {
console.log('Karma has exited with ' + exitCode);
process.exit(exitCode);
});
karma.runner.run({ port: 9876 }, (exitCode: number) => {
console.log('Karma has exited with ' + exitCode);
process.exit(exitCode);
});
karma.stopper.stop({ port: 9876 }, exitCode => {
if (exitCode === 0) {
console.log('Server stop as initiated');
}
process.exit(exitCode);
});
const server = new karma.Server({ logLevel: 'debug', port: 9876 }, (exitCode: number) => {
console.log('Karma has exited with ' + exitCode);
process.exit(exitCode);
});
server.start();
server.refreshFiles();
server.on('browser_register', (browser: any) => {
console.log('A new browser was registered');
});
server.on('run_complete', (browsers, results) => {
results.disconnected = false;
results.error = false;
results.exitCode = 0;
results.failed = 9;
results.success = 10;
});
karma.runner.run({ port: 9876 }, (exitCode: number) => {
console.log('Karma has exited with ' + exitCode);
process.exit(exitCode);
});
const testLauncher = (launcher: karma.launcher.Launcher) => {
const captured: boolean = launcher.areAllCaptured();
};
// Example of configuration file karma.conf.ts, see http://karma-runner.github.io/latest/config/configuration-file.html
module.exports = (config: karma.Config) => {
config.set({
logLevel: config.LOG_DEBUG,
basePath: '..',
urlRoot: '/base/',
frameworks: ['jasmine'],
files: [
{ pattern: 'lib/angular.js', watched: false },
'test/unit/*.spec.js',
{ pattern: 'compiled/index.html', watched: false },
{ pattern: 'app/index.html', included: false, served: false },
{ pattern: 'compiled/app.js.map', included: false, served: true, watched: false, nocache: true },
{ pattern: 'test/images/*.jpg', watched: false, included: false, served: true, nocache: false },
],
loggers: {
custom: { type: 'file', filename: 'log.txt' },
},
reporters: ['progress', 'coverage'],
middleware: ['foo', 'bar'],
beforeMiddleware: ['foo', 'bar'],
mime: {
'text/x-typescript': ['ts', 'tsx'],
},
preprocessors: {
'app.js': ['coverage'],
},
port: 9876,
autoWatch: true,
browserDisconnectTimeout: 20_000,
browserDisconnectTolerance: 100,
browserNoActivityTimeout: 10_000,
browserSocketTimeout: 20_000,
browsers: ['Chrome', 'Firefox'],
customHeaders: [
{
match: '.*foo.html',
name: 'Service-Worker-Allowed',
value: '/',
},
],
customLaunchers: {
ChromiumHeadless_without_security: {
base: 'ChromiumHeadless',
flags: ['--no-sandbox', '--disable-setuid-sandbox'],
},
},
detached: true,
failOnEmptyTestSuite: false,
failOnSkippedTests: true,
failOnFailingTestSuite: false,
forceJSONP: true,
formatError: msg => `error: ${msg}`,
listenAddress: '0.0.0.0',
pingTimeout: 2000,
processKillTimeout: 3000,
proxies: {
'/static': 'http://gstatic.com',
'/web': 'http://localhost:9000',
'/img/': '/base/test/images/',
'/proxyfied': {
target: 'http://myserver.localhost',
changeOrigin: true,
},
},
proxyReq: (proxyReq, req, res, options) => {
proxyReq.setHeader('Referer', 'https://www.example.com/');
},
proxyRes: (proxyRes, req, res) => {
if (proxyRes.headers['set-cookie']) {
proxyRes.headers['set-cookie'] = proxyRes.headers['set-cookie'].map((cookie: string) => {
return cookie.replace(/\s*secure;?/i, '');
});
}
},
singleRun: true,
restartOnFileChange: true,
retryLimit: 5,
browserConsoleLogOptions: {
level: 'warn',
format: '%b %T: %m',
path: 'some/path/to.log',
terminal: false,
},
upstreamProxy: {
hostname: 'localhost',
path: '/',
port: 27001,
protocol: 'http',
},
});
};
// custom browser config
const customBrowser = (config: karma.Config) => {
config.set({
browsers: ['Safari', 'my-custom-browser', 'Firefox', '/usr/local/bin/custom-browser.sh'],
});
};
// plugins
function CustomMiddlewareFactory(config: karma.ConfigOptions) {
return (request: any, response: any /* next */) => {
response.writeHead(200);
return response.end('content!');
};
}
// plugin can be class or constructor function
const CustomPlugin = function CustomPlugin() {};
CustomPlugin.prototype = {
log: () => {},
};
class CustomPluginClass {
log: () => {};
}
const pluginsTests = (config: karma.Config) => {
config.set({
middleware: ['custom'],
plugins: [
'karma-jasmine',
'karma-chrome-launcher',
{ 'framework:xyz': ['factory', CustomMiddlewareFactory] },
{ 'framework:abc': ['type', CustomPlugin] },
{ 'framework:abc': ['type', CustomPluginClass] },
{ 'framework:xyz': ['value', 5] },
],
});
};
console.log(karma.constants.DEFAULT_HOSTNAME);
console.log(karma.VERSION);
karma.config.parseConfig('karma.conf.js', {
singleRun: true,
restartOnFileChange: true,
});
// constants
karma.VERSION; // $ExpectType string
karma.constants.VERSION; // $ExpectType string
karma.constants.DEFAULT_PORT; // $ExpectType string | number
karma.constants.DEFAULT_HOSTNAME; // $ExpectType string
karma.constants.DEFAULT_LISTEN_ADDR; // $ExpectType string
karma.constants.LOG_DISABLE; // $ExpectType "OFF"
karma.constants.LOG_ERROR; // $ExpectType "ERROR"
karma.constants.LOG_WARN; // $ExpectType "WARN"
karma.constants.LOG_INFO; // $ExpectType "INFO"
karma.constants.LOG_DEBUG; // $ExpectType "DEBUG"
karma.constants.LOG_LOG; // $ExpectType "LOG"
karma.constants.LOG_PRIORITIES; // $ExpectType ["OFF", "ERROR", "WARN", "LOG", "INFO", "DEBUG"]
karma.constants.COLOR_PATTERN; // $ExpectType string
karma.constants.NO_COLOR_PATTERN; // $ExpectType string
karma.constants.CONSOLE_APPENDER; // $ExpectType { type: string; layout: { type: string; pattern: string; }; }
karma.constants.EXIT_CODE; // $ExpectType string
karma.constants.LOG_PRIORITIES[5] === 'DEBUG';