|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { RequireInfo, analyzeKarmaConfig } from './karma-config-analyzer'; |
| 10 | +import { generateDefaultKarmaConfig } from './karma-config-comparer'; |
| 11 | + |
| 12 | +describe('Karma Config Analyzer', () => { |
| 13 | + it('should parse a basic karma config file', () => { |
| 14 | + const karmaConf = ` |
| 15 | + module.exports = function (config) { |
| 16 | + config.set({ |
| 17 | + basePath: '', |
| 18 | + frameworks: ['jasmine', '@angular-devkit/build-angular'], |
| 19 | + plugins: [ |
| 20 | + require('karma-jasmine'), |
| 21 | + require('karma-chrome-launcher'), |
| 22 | + require('karma-jasmine-html-reporter'), |
| 23 | + require('karma-coverage'), |
| 24 | + require('@angular-devkit/build-angular/plugins/karma'), |
| 25 | + ], |
| 26 | + client: { |
| 27 | + clearContext: false, // leave Jasmine Spec Runner output visible in browser |
| 28 | + }, |
| 29 | + jasmineHtmlReporter: { |
| 30 | + suppressAll: true, // removes the duplicated traces |
| 31 | + }, |
| 32 | + coverageReporter: { |
| 33 | + dir: require('path').join(__dirname, './coverage/test-project'), |
| 34 | + subdir: '.', |
| 35 | + reporters: [{ type: 'html' }, { type: 'text-summary' }], |
| 36 | + }, |
| 37 | + reporters: ['progress', 'kjhtml'], |
| 38 | + port: 9876, |
| 39 | + colors: true, |
| 40 | + logLevel: config.LOG_INFO, |
| 41 | + autoWatch: true, |
| 42 | + browsers: ['Chrome'], |
| 43 | + singleRun: false, |
| 44 | + restartOnFileChange: true, |
| 45 | + }); |
| 46 | + }; |
| 47 | + `; |
| 48 | + |
| 49 | + const { settings, hasUnsupportedValues } = analyzeKarmaConfig(karmaConf); |
| 50 | + |
| 51 | + expect(settings.get('basePath') as unknown).toBe(''); |
| 52 | + expect(settings.get('frameworks') as unknown).toEqual([ |
| 53 | + 'jasmine', |
| 54 | + '@angular-devkit/build-angular', |
| 55 | + ]); |
| 56 | + expect(settings.get('port') as unknown).toBe(9876); |
| 57 | + expect(settings.get('autoWatch') as boolean).toBe(true); |
| 58 | + expect(settings.get('singleRun') as boolean).toBe(false); |
| 59 | + expect(settings.get('reporters') as unknown).toEqual(['progress', 'kjhtml']); |
| 60 | + |
| 61 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 62 | + const plugins = settings.get('plugins') as any[]; |
| 63 | + expect(plugins).toBeInstanceOf(Array); |
| 64 | + expect(plugins.length).toBe(5); |
| 65 | + expect(plugins[0]).toEqual({ module: 'karma-jasmine' }); |
| 66 | + expect(plugins[4]).toEqual({ module: '@angular-devkit/build-angular/plugins/karma' }); |
| 67 | + |
| 68 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 69 | + const coverageReporter = settings.get('coverageReporter') as any; |
| 70 | + const dirInfo = coverageReporter.dir as RequireInfo; |
| 71 | + expect(dirInfo.module).toBe('path'); |
| 72 | + expect(dirInfo.export).toBe('join'); |
| 73 | + expect(dirInfo.isCall).toBe(true); |
| 74 | + expect(dirInfo.arguments as unknown).toEqual(['__dirname', './coverage/test-project']); |
| 75 | + |
| 76 | + // config.LOG_INFO is a variable, so it should be flagged as unsupported |
| 77 | + expect(hasUnsupportedValues).toBe(true); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should return an empty map for an empty config file', () => { |
| 81 | + const { settings, hasUnsupportedValues } = analyzeKarmaConfig(''); |
| 82 | + |
| 83 | + expect(settings.size).toBe(0); |
| 84 | + expect(hasUnsupportedValues).toBe(false); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should handle a config file with no config.set call', () => { |
| 88 | + const karmaConf = ` |
| 89 | + module.exports = function (config) { |
| 90 | + // No config.set call |
| 91 | + }; |
| 92 | + `; |
| 93 | + const { settings, hasUnsupportedValues } = analyzeKarmaConfig(karmaConf); |
| 94 | + |
| 95 | + expect(settings.size).toBe(0); |
| 96 | + expect(hasUnsupportedValues).toBe(false); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should detect unsupported values like variables', () => { |
| 100 | + const karmaConf = ` |
| 101 | + const myBrowsers = ['Chrome', 'Firefox']; |
| 102 | + module.exports = function (config) { |
| 103 | + config.set({ |
| 104 | + browsers: myBrowsers, |
| 105 | + }); |
| 106 | + }; |
| 107 | + `; |
| 108 | + const { settings, hasUnsupportedValues } = analyzeKarmaConfig(karmaConf); |
| 109 | + |
| 110 | + expect(settings.get('browsers')).toBeUndefined(); |
| 111 | + expect(hasUnsupportedValues).toBe(true); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should correctly parse require with nested exports', () => { |
| 115 | + const karmaConf = ` |
| 116 | + module.exports = function (config) { |
| 117 | + config.set({ |
| 118 | + reporter: require('some-plugin').reporter.type, |
| 119 | + }); |
| 120 | + }; |
| 121 | + `; |
| 122 | + const { settings, hasUnsupportedValues } = analyzeKarmaConfig(karmaConf); |
| 123 | + |
| 124 | + const reporter = settings.get('reporter') as RequireInfo; |
| 125 | + expect(reporter.module).toBe('some-plugin'); |
| 126 | + expect(reporter.export).toBe('reporter.type'); |
| 127 | + expect(hasUnsupportedValues).toBe(false); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should handle an array with mixed values', () => { |
| 131 | + const karmaConf = ` |
| 132 | + module.exports = function (config) { |
| 133 | + config.set({ |
| 134 | + plugins: [ |
| 135 | + 'karma-jasmine', |
| 136 | + require('karma-chrome-launcher'), |
| 137 | + true, |
| 138 | + ], |
| 139 | + }); |
| 140 | + }; |
| 141 | + `; |
| 142 | + const { settings, hasUnsupportedValues } = analyzeKarmaConfig(karmaConf); |
| 143 | + |
| 144 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 145 | + const plugins = settings.get('plugins') as any[]; |
| 146 | + expect(plugins).toEqual(['karma-jasmine', { module: 'karma-chrome-launcher' }, true]); |
| 147 | + expect(hasUnsupportedValues).toBe(false); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should not report unsupported values when all values are literals or requires', () => { |
| 151 | + const karmaConf = ` |
| 152 | + module.exports = function (config) { |
| 153 | + config.set({ |
| 154 | + autoWatch: true, |
| 155 | + browsers: ['Chrome'], |
| 156 | + plugins: [require('karma-jasmine')], |
| 157 | + }); |
| 158 | + }; |
| 159 | + `; |
| 160 | + const { hasUnsupportedValues } = analyzeKarmaConfig(karmaConf); |
| 161 | + |
| 162 | + expect(hasUnsupportedValues).toBe(false); |
| 163 | + }); |
| 164 | + |
| 165 | + it('should handle path.join with variables and flag as unsupported', () => { |
| 166 | + const karmaConf = ` |
| 167 | + const myPath = './coverage/test-project'; |
| 168 | + module.exports = function (config) { |
| 169 | + config.set({ |
| 170 | + coverageReporter: { |
| 171 | + dir: require('path').join(__dirname, myPath), |
| 172 | + }, |
| 173 | + }); |
| 174 | + }; |
| 175 | + `; |
| 176 | + const { settings, hasUnsupportedValues } = analyzeKarmaConfig(karmaConf); |
| 177 | + |
| 178 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 179 | + const coverageReporter = settings.get('coverageReporter') as any; |
| 180 | + const dirInfo = coverageReporter.dir as RequireInfo; |
| 181 | + expect(dirInfo.module).toBe('path'); |
| 182 | + expect(dirInfo.export).toBe('join'); |
| 183 | + expect(dirInfo.isCall).toBe(true); |
| 184 | + expect(dirInfo.arguments as unknown).toEqual(['__dirname', undefined]); // myPath is a variable |
| 185 | + expect(hasUnsupportedValues).toBe(true); |
| 186 | + }); |
| 187 | + |
| 188 | + it('should correctly parse the default karma config template', async () => { |
| 189 | + const defaultConfig = await generateDefaultKarmaConfig('..', 'test-project', true); |
| 190 | + const { settings, hasUnsupportedValues } = analyzeKarmaConfig(defaultConfig); |
| 191 | + |
| 192 | + expect(hasUnsupportedValues).toBe(false); |
| 193 | + expect(settings.get('basePath') as unknown).toBe(''); |
| 194 | + expect(settings.get('frameworks') as unknown).toEqual([ |
| 195 | + 'jasmine', |
| 196 | + '@angular-devkit/build-angular', |
| 197 | + ]); |
| 198 | + expect(settings.get('plugins') as unknown).toEqual([ |
| 199 | + { module: 'karma-jasmine' }, |
| 200 | + { module: 'karma-chrome-launcher' }, |
| 201 | + { module: 'karma-jasmine-html-reporter' }, |
| 202 | + { module: 'karma-coverage' }, |
| 203 | + { module: '@angular-devkit/build-angular/plugins/karma' }, |
| 204 | + ]); |
| 205 | + expect(settings.get('client') as unknown).toEqual({ |
| 206 | + jasmine: {}, |
| 207 | + }); |
| 208 | + expect(settings.get('jasmineHtmlReporter') as unknown).toEqual({ |
| 209 | + suppressAll: true, |
| 210 | + }); |
| 211 | + const coverageReporter = settings.get('coverageReporter') as { |
| 212 | + dir: RequireInfo; |
| 213 | + subdir: string; |
| 214 | + reporters: { type: string }[]; |
| 215 | + }; |
| 216 | + expect(coverageReporter.dir.module).toBe('path'); |
| 217 | + expect(coverageReporter.dir.export).toBe('join'); |
| 218 | + expect(coverageReporter.dir.isCall).toBe(true); |
| 219 | + expect(coverageReporter.dir.arguments as unknown).toEqual([ |
| 220 | + '__dirname', |
| 221 | + '../coverage/test-project', |
| 222 | + ]); |
| 223 | + expect(coverageReporter.subdir).toBe('.'); |
| 224 | + expect(coverageReporter.reporters).toEqual([{ type: 'html' }, { type: 'text-summary' }]); |
| 225 | + expect(settings.get('reporters') as unknown).toEqual(['progress', 'kjhtml']); |
| 226 | + expect(settings.get('browsers') as unknown).toEqual(['Chrome']); |
| 227 | + expect(settings.get('restartOnFileChange') as unknown).toBe(true); |
| 228 | + }); |
| 229 | +}); |
0 commit comments