1 | const path = require('path')
|
2 | const { Server } = require('karma')
|
3 | const glob = require('fast-glob')
|
4 |
|
5 | module.exports = async (api, testFiles, options) => {
|
6 | const webpackConfig = api.createWebpackChain()
|
7 |
|
8 | const cwd = api.resolveCwd()
|
9 | const files = await glob(
|
10 | [
|
11 | '!**/node_modules/**',
|
12 | `!${path.join(path.relative(process.cwd(), api.config.output.dir), '**')}`
|
13 | ].concat(testFiles),
|
14 | {
|
15 | cwd
|
16 | }
|
17 | )
|
18 |
|
19 | const config = {
|
20 | basePath: cwd,
|
21 |
|
22 | browsers: [options.headless ? 'PoiChromeHeadless' : 'PoiChrome'],
|
23 |
|
24 | customLaunchers: {
|
25 | PoiChrome: {
|
26 | base: 'Chrome'
|
27 | },
|
28 | PoiChromeHeadless: {
|
29 | base: 'ChromeHeadless',
|
30 | flags: ['--no-sandbox']
|
31 | }
|
32 | },
|
33 |
|
34 | files: files.map(pattern => ({
|
35 | pattern,
|
36 | watched: false
|
37 | })),
|
38 |
|
39 | preprocessors: files.reduce((res, pattern) => {
|
40 | res[pattern] = ['webpack']
|
41 | return res
|
42 | }, {}),
|
43 |
|
44 | frameworks: ['jasmine'],
|
45 |
|
46 | reporters: ['mocha', options.coverage && 'coverage'].filter(Boolean),
|
47 |
|
48 |
|
49 | coverageReporter: {
|
50 | dir: 'coverage',
|
51 | reporters: [
|
52 | { type: 'text' },
|
53 | { type: 'html', subdir: 'report-html' },
|
54 | { type: 'lcov', subdir: 'report-lcov' }
|
55 | ]
|
56 | },
|
57 |
|
58 | webpack: webpackConfig.entryPoints
|
59 | .clear()
|
60 | .end()
|
61 | .toConfig(),
|
62 |
|
63 | webpackMiddleware: {
|
64 | logLevel: 'error',
|
65 | stats: 'errors-only',
|
66 | noInfo: true
|
67 | },
|
68 |
|
69 | colors: true,
|
70 |
|
71 | client: {
|
72 | captureConsole: true,
|
73 |
|
74 | jasmine: {
|
75 | random: false
|
76 | }
|
77 | },
|
78 |
|
79 | singleRun: !options.watch
|
80 | }
|
81 |
|
82 | const server = createServer(config, api.PoiError)
|
83 |
|
84 | server.start()
|
85 |
|
86 | await server.completion
|
87 | }
|
88 |
|
89 | function createServer(config, PoiError) {
|
90 | let resolve
|
91 | let reject
|
92 |
|
93 |
|
94 | const promise = new Promise((res, rej) => {
|
95 | resolve = res
|
96 | reject = rej
|
97 | })
|
98 |
|
99 | const callback = code => {
|
100 | if (code === 0) return resolve()
|
101 | const err = new PoiError({
|
102 | message: `Karma has exited with code ${code}`
|
103 | })
|
104 | err.code = code
|
105 | reject(err)
|
106 | }
|
107 |
|
108 | const server = new Server(config, callback)
|
109 |
|
110 | server.completion = promise
|
111 | return server
|
112 | }
|