1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
2// See the LICENCE file in the repository root for full licence text.
3
4// karma-webpack doesn't exit on compile error.
5// This plugin makes it exit on compile error.
6class ExitOnErrorWebpackPlugin {
7 apply(compiler) {
8 compiler.hooks.done.tap('ExitOnErrorWebpackPlugin', (stats) => {
9 if (stats && stats.hasErrors()) {
10 stats.toJson().errors.forEach((error) => {
11 console.error(error);
12 });
13 process.exit(1);
14 }
15 });
16 }
17}
18
19/**
20 * Blocks until the webpack config is read.
21 */
22function readWebpackConfig() {
23 const { argv } = require('yargs');
24
25 if (!argv.singleRun) {
26 argv.watch = true;
27 }
28
29 const configFn = require('./webpack.config.js');
30
31 return configFn(null, argv);
32}
33
34process.env.SKIP_MANIFEST = true;
35const webpackConfig = readWebpackConfig();
36webpackConfig.plugins.push(new ExitOnErrorWebpackPlugin());
37webpackConfig.mode = 'development';
38webpackConfig.devtool = 'inline-source-map';
39delete webpackConfig.optimization; // karma doesn't work with splitChunks...or runtimeChunk
40delete webpackConfig.entry; // test runner doesn't use the entry points
41
42const testIndex = './tests/karma/index.ts';
43
44const files = [
45 './tests/karma/globals.js', // shims for tests
46 testIndex,
47];
48
49const preprocessors = {};
50preprocessors[testIndex] = ['webpack', 'sourcemap'];
51
52module.exports = function(config) {
53 config.set({
54 autoWatch: true,
55 basePath: '.',
56 browsers: ['ChromeHeadless'],
57 colors: true,
58 concurrency: Infinity,
59 exclude: [],
60 files,
61 frameworks: ['jasmine', 'webpack'],
62 logLevel: config.LOG_INFO,
63 mime: { 'text/x-typescript': ['ts', 'tsx'] },
64 port: 9876,
65 preprocessors,
66 reporters: ['mocha'],
67 singleRun: false, // set to true for the process to exit after completing.
68 webpack: webpackConfig,
69 webpackMiddleware: {
70 noInfo: true,
71 stats: 'errors-only',
72 },
73 });
74};