|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { isModuleNotFoundError } from '../src/utils' |
| 3 | + |
| 4 | +describe('isModuleNotFoundError', () => { |
| 5 | + it('detects Chrome dynamic import error', () => { |
| 6 | + const error = new Error( |
| 7 | + 'Failed to fetch dynamically imported module: http://localhost:5173/src/routes/posts.index.tsx?tsr-split', |
| 8 | + ) |
| 9 | + expect(isModuleNotFoundError(error)).toBe(true) |
| 10 | + }) |
| 11 | + |
| 12 | + it('detects Firefox dynamic import error', () => { |
| 13 | + const error = new Error( |
| 14 | + 'error loading dynamically imported module: http://localhost:5173/src/routes/posts.index.tsx?tsr-split', |
| 15 | + ) |
| 16 | + expect(isModuleNotFoundError(error)).toBe(true) |
| 17 | + }) |
| 18 | + |
| 19 | + it('detects Safari dynamic import error', () => { |
| 20 | + const error = new Error('Importing a module script failed.') |
| 21 | + expect(isModuleNotFoundError(error)).toBe(true) |
| 22 | + }) |
| 23 | + |
| 24 | + it('detects Webpack ChunkLoadError by name', () => { |
| 25 | + // Webpack throws a ChunkLoadError with a distinct name property |
| 26 | + const error = new Error('Loading chunk 123 failed.\n(error: http://localhost:3333/js/123.abc123.chunk.js)') |
| 27 | + error.name = 'ChunkLoadError' |
| 28 | + expect(isModuleNotFoundError(error)).toBe(true) |
| 29 | + }) |
| 30 | + |
| 31 | + it('detects Webpack-style error by message prefix when name is generic', () => { |
| 32 | + // Fallback: even without ChunkLoadError name, the message prefix catches it |
| 33 | + const error = new Error('Loading chunk abc123 failed.\n(error: http://localhost:3333/js/main.abc123.js)') |
| 34 | + expect(isModuleNotFoundError(error)).toBe(true) |
| 35 | + }) |
| 36 | + |
| 37 | + it('detects Rspack ChunkLoadError', () => { |
| 38 | + const error = new Error('Loading chunk 456 failed.\n(error: http://localhost:3333/js/456.def456.chunk.js)') |
| 39 | + error.name = 'ChunkLoadError' |
| 40 | + expect(isModuleNotFoundError(error)).toBe(true) |
| 41 | + }) |
| 42 | + |
| 43 | + it('returns false for null input', () => { |
| 44 | + expect(isModuleNotFoundError(null)).toBe(false) |
| 45 | + }) |
| 46 | + |
| 47 | + it('returns false for undefined input', () => { |
| 48 | + expect(isModuleNotFoundError(undefined)).toBe(false) |
| 49 | + }) |
| 50 | + |
| 51 | + it('returns false for non-error objects', () => { |
| 52 | + expect(isModuleNotFoundError({})).toBe(false) |
| 53 | + expect(isModuleNotFoundError('string error')).toBe(false) |
| 54 | + expect(isModuleNotFoundError(42)).toBe(false) |
| 55 | + }) |
| 56 | + |
| 57 | + it('returns false for unrelated errors', () => { |
| 58 | + const error = new Error('Something went wrong') |
| 59 | + expect(isModuleNotFoundError(error)).toBe(false) |
| 60 | + }) |
| 61 | + |
| 62 | + it('returns false for errors without a message', () => { |
| 63 | + const error = {} |
| 64 | + expect(isModuleNotFoundError(error)).toBe(false) |
| 65 | + }) |
| 66 | +}) |
0 commit comments