|
1 | | -import { describe, expect, test } from 'vitest' |
| 1 | +import { describe, expect, test, vi } from 'vitest' |
2 | 2 | import { isWindows } from '../../../shared/utils' |
3 | | -import { getNodeModulesPackageRoot } from '../sourcemap' |
| 3 | +import { createLogger } from '../../logger' |
| 4 | +import { |
| 5 | + getNodeModulesPackageRoot, |
| 6 | + injectSourcesContent, |
| 7 | + type SourceMapLike, |
| 8 | +} from '../sourcemap' |
4 | 9 |
|
5 | 10 | describe('getNodeModulesPackageRoot', () => { |
6 | 11 | const cases = [ |
@@ -69,3 +74,62 @@ describe('getNodeModulesPackageRoot', () => { |
69 | 74 | }) |
70 | 75 | } |
71 | 76 | }) |
| 77 | + |
| 78 | +describe('injectSourcesContent', () => { |
| 79 | + function createMockLogger() { |
| 80 | + const logger = createLogger() |
| 81 | + logger.warnOnce = vi.fn() |
| 82 | + return logger |
| 83 | + } |
| 84 | + |
| 85 | + test('leaves maps with a remote sourceRoot alone', async () => { |
| 86 | + const map: SourceMapLike = { |
| 87 | + sources: ['index.ts'], |
| 88 | + sourceRoot: 'https://raw.githubusercontent.com/fb55/domutils/abc123/src/', |
| 89 | + } |
| 90 | + const logger = createMockLogger() |
| 91 | + |
| 92 | + await injectSourcesContent( |
| 93 | + map, |
| 94 | + '/project/node_modules/domutils/lib/esm/index.js', |
| 95 | + logger, |
| 96 | + ) |
| 97 | + |
| 98 | + expect(logger.warnOnce).not.toHaveBeenCalled() |
| 99 | + expect(map.sourcesContent).toBeUndefined() |
| 100 | + }) |
| 101 | + |
| 102 | + test('does not inject content for remote sources', async () => { |
| 103 | + const map: SourceMapLike = { |
| 104 | + sources: [ |
| 105 | + 'https://raw.githubusercontent.com/fb55/domutils/abc123/src/index.ts', |
| 106 | + ], |
| 107 | + } |
| 108 | + const logger = createMockLogger() |
| 109 | + |
| 110 | + await injectSourcesContent( |
| 111 | + map, |
| 112 | + '/project/node_modules/domutils/lib/esm/index.js', |
| 113 | + logger, |
| 114 | + ) |
| 115 | + |
| 116 | + expect(logger.warnOnce).not.toHaveBeenCalled() |
| 117 | + expect(map.sourcesContent).toStrictEqual([]) |
| 118 | + }) |
| 119 | + |
| 120 | + test('warns for sources that resolve outside the package', async () => { |
| 121 | + const map: SourceMapLike = { |
| 122 | + sources: ['/outside/project/index.ts'], |
| 123 | + } |
| 124 | + const logger = createMockLogger() |
| 125 | + |
| 126 | + await injectSourcesContent( |
| 127 | + map, |
| 128 | + '/project/node_modules/foo/dist/index.js', |
| 129 | + logger, |
| 130 | + ) |
| 131 | + |
| 132 | + expect(logger.warnOnce).toHaveBeenCalledOnce() |
| 133 | + expect(map.sourcesContent).toStrictEqual([null]) |
| 134 | + }) |
| 135 | +}) |
0 commit comments