Skip to content

Commit 7e8678b

Browse files
committed
Adding new unit tests to the SourceMap and SourceMapFactory classes.
1 parent 3531f17 commit 7e8678b

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*---------------------------------------------------------
2+
* Copyright (C) Microsoft Corporation. All rights reserved.
3+
*--------------------------------------------------------*/
4+
import { expect } from 'chai';
5+
import {
6+
RawIndexMap, RawSourceMap, SourceMapConsumer
7+
} from 'source-map';
8+
import { SourceMap } from './sourceMap';
9+
10+
const sampleSource = 'console.log(123)';
11+
const basicSourceMap: RawSourceMap = {
12+
version: 3,
13+
sources: ['one.js'],
14+
sourcesContent: [sampleSource],
15+
names: [],
16+
file: '',
17+
mappings: '',
18+
};
19+
const indexedSourceMap: RawIndexMap = {
20+
version: 3,
21+
sections: [{
22+
offset: { line: 0, column: 100},
23+
map: basicSourceMap,
24+
}],
25+
};
26+
27+
describe('SourceMap', () => {
28+
it('loads basic source-maps', async () => {
29+
const map = new SourceMap(await new SourceMapConsumer(basicSourceMap), {
30+
sourceMapUrl: JSON.stringify(basicSourceMap),
31+
compiledPath: 'one.js',
32+
}, '', ['one.js'], false);
33+
34+
expect(map.sourceContentFor('one.js')).to.eq(sampleSource);
35+
});
36+
37+
it('loads indexed source-maps', async () => {
38+
const map = new SourceMap(await new SourceMapConsumer(indexedSourceMap), {
39+
sourceMapUrl: JSON.stringify(indexedSourceMap),
40+
compiledPath: 'one.js',
41+
}, '', ['one.js'], false);
42+
43+
expect(map.sourceContentFor('one.js')).to.eq(sampleSource);
44+
});
45+
});
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*---------------------------------------------------------
2+
* Copyright (C) Microsoft Corporation. All rights reserved.
3+
*--------------------------------------------------------*/
4+
5+
import { expect } from 'chai';
6+
import dataUriToBuffer from 'data-uri-to-buffer';
7+
import { RawIndexMap, RawSourceMap } from 'source-map';
8+
import Dap from '../../dap/api';
9+
import { stubbedDapApi, StubDapApi } from '../../dap/stubbedApi';
10+
import { Logger } from '../logging/logger';
11+
import { SourceMapFactory } from './sourceMapFactory';
12+
13+
const sampleSource = 'console.log(123)';
14+
const basicSourceMap: RawSourceMap = {
15+
version: 3,
16+
sources: ['one.js'],
17+
sourcesContent: [sampleSource],
18+
names: [],
19+
file: '',
20+
mappings: '',
21+
};
22+
const indexedSourceMap: RawIndexMap = {
23+
version: 3,
24+
sections: [{
25+
offset: { line: 0, column: 100},
26+
map: basicSourceMap,
27+
}],
28+
};
29+
30+
describe('SourceMapFactory', () => {
31+
let stubDap: StubDapApi;
32+
33+
beforeEach(() => {
34+
stubDap = stubbedDapApi();
35+
})
36+
37+
it('loads source-maps', async () => {
38+
const factory = new SourceMapFactory({
39+
rebaseRemoteToLocal() { return '/tmp/local'; },
40+
rebaseLocalToRemote() { return '/tmp/remote'; },
41+
shouldResolveSourceMap() { return true; },
42+
urlToAbsolutePath() { return Promise.resolve('/tmp/abs');},
43+
absolutePathToUrlRegexp() { return undefined; },
44+
}, {
45+
fetch(url) { return Promise.resolve({ ok: true, body: dataUriToBuffer(url).toString('utf8'), url: url, statusCode: 500 }); },
46+
fetchJson<T>() { return Promise.resolve({ ok: true, body: {} as T, url: '', statusCode: 200 }); },
47+
}, stubDap as unknown as Dap.Api, Logger.null);
48+
49+
const map = await factory.load({
50+
sourceMapUrl: 'data:application/json;base64,' + Buffer.from(JSON.stringify(indexedSourceMap)).toString('base64'),
51+
compiledPath: '/tmp/local/one.js',
52+
});
53+
54+
expect(map.sources).to.eql(['one.js']);
55+
});
56+
});

0 commit comments

Comments
 (0)