-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathcreateKarmaConfig.ts
More file actions
106 lines (98 loc) · 3.55 KB
/
createKarmaConfig.ts
File metadata and controls
106 lines (98 loc) · 3.55 KB
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
import fs from 'fs'
import { fileURLToPath } from 'url'
import type { Configuration, ExternalItem } from 'webpack'
const DEBUG_MODE = process.env.BROWSER_TEST_DEBUG_MODE ?? false
export interface KarmaConfigOptions {
// File patterns to serve but not include in the test bundle (e.g. worker files)
servedFiles?: string[]
}
export const createKarmaConfig = (
testPaths: string[],
webpackConfig: () => Configuration,
localDirectory?: string,
options: KarmaConfigOptions = {}
): (config: any) => any => {
const setupFiles = [fileURLToPath(new URL('./karma-setup.js', import.meta.url))]
if (localDirectory !== undefined) {
const karmaSetupCandidates = [
`${localDirectory}/karma-setup.js`,
`${localDirectory}/karma-setup.ts`
]
for (const candidate of karmaSetupCandidates) {
if (fs.existsSync(candidate)) {
setupFiles.push(candidate)
break
}
}
}
const preprocessors: Record<string, string[]> = {}
setupFiles.forEach((f) => preprocessors[f] = ['webpack'])
testPaths.forEach((f) => preprocessors[f] = ['webpack', 'sourcemap'])
const baseWebpack = webpackConfig()
const mergedExternals: ExternalItem[] = []
if (baseWebpack.externals !== undefined) {
if (Array.isArray(baseWebpack.externals)) {
mergedExternals.push(...baseWebpack.externals)
} else {
mergedExternals.push(baseWebpack.externals)
}
}
mergedExternals.push({
'expect': 'commonjs2 expect',
'@jest/expect-utils': 'commonjs2 @jest/expect-utils',
'pretty-format': 'commonjs2 pretty-format',
'jest-diff': 'commonjs2 jest-diff',
})
return (config: any) => {
config.set({
plugins: [
'karma-electron',
'karma-webpack',
'karma-jasmine',
'karma-spec-reporter',
'karma-sourcemap-loader'
],
basePath: '.',
frameworks: ['webpack', 'jasmine'],
reporters: ['spec'],
files: [
...setupFiles,
...testPaths,
...(options.servedFiles ?? []).map((pattern) => ({
pattern,
included: false,
served: true,
watched: false
}))
],
preprocessors,
customLaunchers: {
CustomElectron: {
base: 'Electron',
browserWindowOptions: {
webPreferences: {
contextIsolation: false,
preload: fileURLToPath(new URL('./preload.cjs', import.meta.url)),
webSecurity: false,
sandbox: false,
nodeIntegration: true
},
show: DEBUG_MODE // set to true to show the electron window
}
}
},
browserDisconnectTimeout: 60000,
browserNoActivityTimeout: 400000,
browsers: ['CustomElectron'],
client: {
clearContext: false, // leave Jasmine Spec Runner output visible in browser
useIframe: false,
},
singleRun: !DEBUG_MODE, //set to false to leave electron window open
webpack: {
...baseWebpack,
externals: mergedExternals,
}
})
}
}