|
1 | 1 | import { resolve, join } from 'path'; |
2 | 2 | import webpack from 'webpack'; |
3 | 3 | import HtmlPlugin from 'html-webpack-plugin'; |
| 4 | +import { logger, LogLevel } from '@app-config/logging'; |
4 | 5 | import AppConfigPlugin, { regex, loader, Options } from './index'; |
5 | 6 |
|
6 | 7 | const examplesDir = resolve(__dirname, '../../examples'); |
@@ -47,6 +48,41 @@ describe('frontend-webpack-project example', () => { |
47 | 48 | }); |
48 | 49 | }); |
49 | 50 |
|
| 51 | + it('should throw an error if html-webpack-plugin is not available and headerInjection is true', () => { |
| 52 | + process.env.APP_CONFIG = JSON.stringify({ externalApiUrl: 'https://localhost:3999' }); |
| 53 | + jest.isolateModules(async () => { |
| 54 | + jest.mock('html-webpack-plugin', () => { |
| 55 | + throw new Error('html-webpack-plugin not found'); |
| 56 | + }); |
| 57 | + |
| 58 | + const writeMsg = jest.fn(); |
| 59 | + logger.setWriter(writeMsg); |
| 60 | + logger.setLevel(LogLevel.Verbose); |
| 61 | + |
| 62 | + try { |
| 63 | + await new Promise<void>((done, reject) => { |
| 64 | + webpack([createOptions({ headerInjection: true })], (err, stats) => { |
| 65 | + if (err) return reject(err); |
| 66 | + if (!stats) return reject(new Error('no stats')); |
| 67 | + if (stats.hasErrors()) reject(stats.toString()); |
| 68 | + done(); |
| 69 | + }); |
| 70 | + }); |
| 71 | + } catch (err) { |
| 72 | + expect(writeMsg).toHaveBeenCalledTimes(3); |
| 73 | + expect(writeMsg).toHaveBeenCalledWith( |
| 74 | + '[app-config][ERROR] html-webpack-plugin not found\n', |
| 75 | + ); |
| 76 | + expect(writeMsg).toHaveBeenCalledWith( |
| 77 | + '[app-config][ERROR] Failed to resolve html-webpack-plugin\n', |
| 78 | + ); |
| 79 | + expect(writeMsg).toHaveBeenCalledWith( |
| 80 | + '[app-config][ERROR] Either include the module in your dependencies and enable the webpack plugin, or set headerInjection to false in your configuration.\n', |
| 81 | + ); |
| 82 | + } |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
50 | 86 | it('reads environment variable for app-config', async () => { |
51 | 87 | process.env.APP_CONFIG = JSON.stringify({ externalApiUrl: 'https://localhost:3999' }); |
52 | 88 |
|
|
0 commit comments