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