Skip to content

Commit b119600

Browse files
committed
Add tests for buildSourceMapTree behavior
1 parent b9eb7a7 commit b119600

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

test/unit/build-source-map-tree.ts

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ describe('buildSourceMapTree', () => {
1818
const loader = jest.fn(() => null);
1919
buildSourceMapTree(decodedMap, loader);
2020

21+
expect(loader).toHaveBeenCalledTimes(1);
2122
expect(loader).toHaveBeenCalledWith('helloworld.js');
22-
expect(loader.mock.calls.length).toBe(1);
2323
});
2424

2525
test('loader cannot be async', () => {
@@ -100,9 +100,9 @@ describe('buildSourceMapTree', () => {
100100
},
101101
]);
102102

103+
expect(loader).toHaveBeenCalledTimes(2);
103104
expect(loader).toHaveBeenCalledWith('helloworld.js');
104105
expect(loader).toHaveBeenCalledWith('two.js');
105-
expect(loader.mock.calls.length).toBe(2);
106106
});
107107

108108
test('calls loader with sourceRoot joined to source file', () => {
@@ -115,8 +115,8 @@ describe('buildSourceMapTree', () => {
115115
loader
116116
);
117117

118+
expect(loader).toHaveBeenCalledTimes(1);
118119
expect(loader).toHaveBeenCalledWith('https://foo.com/helloworld.js');
119-
expect(loader.mock.calls.length).toBe(1);
120120
});
121121

122122
test('original sources are relative to the tree path', () => {
@@ -154,10 +154,10 @@ describe('buildSourceMapTree', () => {
154154
},
155155
]);
156156

157+
expect(loader).toHaveBeenCalledTimes(3);
157158
expect(loader).toHaveBeenCalledWith('helloworld.js');
158159
expect(loader).toHaveBeenCalledWith('https://foo.com/assets/two.js');
159160
expect(loader).toHaveBeenCalledWith('https://foo.com/assets/deep/three.js');
160-
expect(loader.mock.calls.length).toBe(3);
161161
});
162162

163163
test('original sources are relative to the tree path, edge cases', () => {
@@ -202,10 +202,10 @@ describe('buildSourceMapTree', () => {
202202
},
203203
]);
204204

205+
expect(loader).toHaveBeenCalledTimes(3);
205206
expect(loader).toHaveBeenCalledWith('https://foo.com/deep/helloworld.js');
206207
expect(loader).toHaveBeenCalledWith('https://foo.com/two.js');
207208
expect(loader).toHaveBeenCalledWith('https://foo.com/assets/three.js');
208-
expect(loader.mock.calls.length).toBe(3);
209209
});
210210

211211
test('transformation maps of a sourcemap may be passed before the sourcemap', () => {
@@ -227,6 +227,28 @@ describe('buildSourceMapTree', () => {
227227
]);
228228
});
229229

230+
test('transformation map does not influence map url', () => {
231+
const maps = [
232+
{
233+
...decodedMap,
234+
sourceRoot: 'https://example.com/',
235+
}, // "transformation map"
236+
decodedMap,
237+
];
238+
const tree = buildSourceMapTree(maps, () => null);
239+
240+
expect(tree.sources).toMatchObject([
241+
{
242+
// helloworld.js's map
243+
sources: [
244+
{
245+
source: 'helloworld.js',
246+
},
247+
],
248+
},
249+
]);
250+
});
251+
230252
test('throws when transformation map has more than one source', () => {
231253
const maps = [
232254
{
@@ -241,6 +263,25 @@ describe('buildSourceMapTree', () => {
241263
}).toThrow();
242264
});
243265

266+
test('handles when transformation map has 0 sources', () => {
267+
const maps = [
268+
{
269+
...decodedMap,
270+
mappings: [],
271+
sources: [],
272+
}, // "transformation map"
273+
decodedMap,
274+
];
275+
const loader = jest.fn();
276+
277+
const tree = buildSourceMapTree(maps, loader);
278+
expect(tree.map).toMatchObject({
279+
sources: [],
280+
});
281+
expect(loader).toHaveBeenCalledTimes(1);
282+
expect(loader).toHaveBeenCalledWith('helloworld.js');
283+
});
284+
244285
test('parses map with null source', () => {
245286
const loader = jest.fn();
246287
loader

0 commit comments

Comments
 (0)