|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { |
| 3 | + assertIsError, |
| 4 | + getJsonErrorLineColumn, |
| 5 | + getProxyConfig, |
| 6 | + normalizeProxyConfiguration, |
| 7 | +} from './dev-server-config-utils.ts'; |
| 8 | +import { MEMFS_VOLUME } from '@ng-rspack/testing-utils'; |
| 9 | +import * as loadEsm from './load-esm.ts'; |
| 10 | +import { vol } from 'memfs'; |
| 11 | + |
| 12 | +describe('getJsonErrorLineColumn', () => { |
| 13 | + it('should return line and column of the error', () => { |
| 14 | + const brokenJson = ` |
| 15 | + { |
| 16 | + "test": { |
| 17 | + "id": 1a, |
| 18 | + } |
| 19 | + }`; |
| 20 | + |
| 21 | + const errMsg = `Expected ',' or '}' after property value in JSON at position 38`; |
| 22 | + expect(() => JSON.parse(brokenJson)).toThrow(errMsg); |
| 23 | + |
| 24 | + const match = /position (\d+)/.exec(errMsg); // Extract the position from the error message (38) |
| 25 | + const offset = match ? Number(match[1]) : 0; |
| 26 | + const { line, column } = getJsonErrorLineColumn(offset, brokenJson); |
| 27 | + expect(`Parse error at line ${line}, column ${column}`).toBe( |
| 28 | + `Parse error at line 4, column 14` |
| 29 | + ); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should return line and column 0 if offset it 0', () => { |
| 33 | + expect(getJsonErrorLineColumn(0, '')).toStrictEqual({ |
| 34 | + column: 1, |
| 35 | + line: 1, |
| 36 | + }); |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | +describe('assertIsError', () => { |
| 41 | + it('does nothing for a genuine Error instance', () => { |
| 42 | + const e = new Error('oops'); |
| 43 | + expect(() => assertIsError(e)).not.toThrow(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('accepts a plain object with name and message (e.g. RxJS error)', () => { |
| 47 | + const rxError = { name: 'TypeError', message: 'bad type' }; |
| 48 | + expect(() => assertIsError(rxError)).not.toThrow(); |
| 49 | + // After narrowing, TS knows it’s Error-like: |
| 50 | + assertIsError(rxError); |
| 51 | + expect(rxError.name).toBe('TypeError'); |
| 52 | + expect(rxError.message).toBe('bad type'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('throws if value is not Error-like', () => { |
| 56 | + const badValues = [ |
| 57 | + null, |
| 58 | + undefined, |
| 59 | + 123, |
| 60 | + 'err', |
| 61 | + { foo: 'bar' }, |
| 62 | + { name: 'X' }, |
| 63 | + { message: 'Y' }, |
| 64 | + ]; |
| 65 | + for (const val of badValues) { |
| 66 | + expect(() => assertIsError(val as any)).toThrow( |
| 67 | + 'catch clause variable is not an Error instance' |
| 68 | + ); |
| 69 | + } |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +describe('normalizeProxyConfiguration', () => { |
| 74 | + it('returns the same array when given an array of proxy configs', () => { |
| 75 | + const input = [ |
| 76 | + { context: ['/api'], target: 'http://localhost:3000' }, |
| 77 | + { context: ['/auth'], changeOrigin: true }, |
| 78 | + ]; |
| 79 | + |
| 80 | + expect(normalizeProxyConfiguration(input)).toBe(input); |
| 81 | + }); |
| 82 | + |
| 83 | + it('converts multiple-object proxy configs to an array with matching entries', () => { |
| 84 | + const input = { |
| 85 | + '/api': { target: 'http://example.com' }, |
| 86 | + '/auth': { changeOrigin: true, secure: false }, |
| 87 | + }; |
| 88 | + expect(normalizeProxyConfiguration(input)).toEqual([ |
| 89 | + { context: ['/api'], target: 'http://example.com' }, |
| 90 | + { context: ['/auth'], changeOrigin: true, secure: false }, |
| 91 | + ]); |
| 92 | + }); |
| 93 | + |
| 94 | + it('returns an empty array when given an empty proxy object', () => { |
| 95 | + expect(normalizeProxyConfiguration({})).toEqual([]); |
| 96 | + }); |
| 97 | +}); |
| 98 | + |
| 99 | +describe('getProxyConfig integration', () => { |
| 100 | + const root = MEMFS_VOLUME; |
| 101 | + const loadEsmSpy = vi.spyOn(loadEsm, 'loadEsmModule'); |
| 102 | + |
| 103 | + beforeEach(() => { |
| 104 | + loadEsmSpy.mockResolvedValue({ |
| 105 | + default: [ |
| 106 | + { |
| 107 | + context: ['/mjs'], |
| 108 | + foo: 'bar', |
| 109 | + }, |
| 110 | + ], |
| 111 | + }); |
| 112 | + |
| 113 | + vol.reset(); |
| 114 | + vol.fromJSON( |
| 115 | + { |
| 116 | + 'good.json': `{ |
| 117 | + "/api": { "target": "http://example.com" }, |
| 118 | +}`, |
| 119 | + 'bad.json': `{ /api: target }`, |
| 120 | + 'config.js': `export default { "/js": { "foo": "baz" } }`, |
| 121 | + 'config.mjs': `just here to make existsSync happy`, |
| 122 | + 'config.cjs': `just here to make existsSync happy`, |
| 123 | + }, |
| 124 | + MEMFS_VOLUME |
| 125 | + ); |
| 126 | + }); |
| 127 | + |
| 128 | + it('returns undefined when proxyConfig is undefined', async () => { |
| 129 | + const result = await getProxyConfig(root, undefined); |
| 130 | + expect(result).toBeUndefined(); |
| 131 | + }); |
| 132 | + |
| 133 | + it('throws if the file does not exist', async () => { |
| 134 | + await expect(getProxyConfig(root, 'no-such-file.json')).rejects.toThrow( |
| 135 | + /Proxy configuration file .*no-such-file\.json does not exist\./ |
| 136 | + ); |
| 137 | + }); |
| 138 | + |
| 139 | + it('parses a valid JSON file and normalizes it', async () => { |
| 140 | + const cfg = await getProxyConfig(root, 'good.json'); |
| 141 | + expect(cfg).toEqual([{ context: ['/api'], target: 'http://example.com' }]); |
| 142 | + }); |
| 143 | + |
| 144 | + it('throws with detailed errors on invalid JSON', async () => { |
| 145 | + await expect(getProxyConfig(root, 'bad.json')).rejects.toThrow( |
| 146 | + /contains parse errors:/ |
| 147 | + ); |
| 148 | + }); |
| 149 | + |
| 150 | + it('loads an ESM (.mjs) config and normalizes it', async () => { |
| 151 | + const cfg = await getProxyConfig(root, 'config.mjs'); |
| 152 | + expect(cfg).toEqual([{ context: ['/mjs'], foo: 'bar' }]); |
| 153 | + }); |
| 154 | + |
| 155 | + it.todo('loads a CommonJS (.cjs) config and normalizes it'); |
| 156 | +}); |
0 commit comments