microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
test/debugger/sourceMapsCombinator.test.ts
71lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import { SourceMapsCombinator } from "../../src/debugger/sourceMapsCombinator"; |
| 5 | import { RawSourceMap } from "source-map"; |
| 6 | |
| 7 | import * as assert from "assert"; |
| 8 | import * as sinon from "sinon"; |
| 9 | import * as fs from "fs"; |
| 10 | import * as path from "path"; |
| 11 | |
| 12 | suite("sourceMapsCombinator", function () { |
| 13 | let sandbox: Sinon.SinonSandbox; |
| 14 | |
| 15 | setup(() => { |
| 16 | sandbox = sinon.sandbox.create(); |
| 17 | }); |
| 18 | |
| 19 | suiteTeardown(() => { |
| 20 | sandbox.restore(); |
| 21 | }); |
| 22 | |
| 23 | suite("#convert", function () { |
| 24 | test("convert sourcemap", function () { |
| 25 | const pathToJS = "d:/hello.js"; |
| 26 | const pathToTS = "d:/hello.ts"; |
| 27 | const sourcemapPath = "d:/hello.js.map"; |
| 28 | const codeJS = fs.readFileSync(path.resolve(__dirname, "assets/hello.js")); |
| 29 | const codeTS = fs.readFileSync(path.resolve(__dirname, "assets/hello.ts")); |
| 30 | const sourcemap: RawSourceMap = { |
| 31 | "version": 3, |
| 32 | "sources": [ |
| 33 | "d:/hello.ts", |
| 34 | ], |
| 35 | "names": [], |
| 36 | "mappings": "AAAA,MAAM,MAAM;IACR,YAAY,CAAC,OAAO,GAAG,EAAE,MAAM,CAAC;IAChC;IACA,OAAO,QAAQ,CAAC,EAAE;QACd,OAAO,OAAO,EAAE,IAAI,CAAC,IAAI,EAAE,OAAO;IACtC;AACJ;;AAEA,MAAM,MAAM,EAAE,IAAI,KAAK,CAAC,gDAAgD,CAAC;;AAEzE,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC", "file": "hello.js", "sourceRoot": "", |
| 37 | }; |
| 38 | |
| 39 | const expected = { |
| 40 | "version": 3, |
| 41 | "sources": [ |
| 42 | "d:/hello.ts", |
| 43 | ], |
| 44 | "names": <string[]>[], |
| 45 | "mappings": "AAAA,IAAA,MAAM,EAAM,CAAA,SAAA,CAAA,EAAA;IACR,SAAA,KAAa,CAAA,GAAA,EAAO;QACpB,IAAA,CAAA,IAAA,EAAA,GAAA;IACA;SACI,CAAA,SAAO,CAAA,SAAc,EAAA,SAAM,CAAA,EAAO;QACtC,OAAA,OAAA,EAAA,IAAA,CAAA,IAAA,EAAA,OAAA;IACJ,CAAA;;AAEA,CAAA,CAAA,CAAA,CAAA;;AAEA,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC", |
| 46 | }; |
| 47 | |
| 48 | let rawBundleSourcemap: RawSourceMap = { |
| 49 | "version": 3, |
| 50 | "sources": [ |
| 51 | "d:/hello.js", |
| 52 | ], |
| 53 | "names": <string[]>[], |
| 54 | "mappings": "AAAA,IAAI,MAAM,EAAE,CAAC,SAAS,CAAC,EAAE;IACrB,SAAS,KAAK,CAAC,GAAG,EAAE;QAChB,IAAI,CAAC,IAAI,EAAE,GAAG;IAClB;IACA,KAAK,CAAC,SAAS,CAAC,SAAS,EAAE,SAAS,CAAC,EAAE;QACnC,OAAO,OAAO,EAAE,IAAI,CAAC,IAAI,EAAE,OAAO;IACtC,CAAC;IACD,OAAO,KAAK;AAChB,CAAC,CAAC,CAAC,CAAC;AACJ,IAAI,MAAM,EAAE,IAAI,KAAK,CAAC,gDAAgD,CAAC;AACvE,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC", "file": "hello.js", "sourceRoot": "", |
| 55 | }; |
| 56 | |
| 57 | const fsReadFileStub = sandbox.stub(fs, "readFileSync"); |
| 58 | |
| 59 | fsReadFileStub.withArgs(pathToJS).returns(codeJS); |
| 60 | fsReadFileStub.withArgs(pathToTS).returns(codeTS); |
| 61 | fsReadFileStub.withArgs(sourcemapPath).returns(JSON.stringify(sourcemap)); |
| 62 | |
| 63 | let sourceMapsCombinator = new SourceMapsCombinator(); |
| 64 | let result = sourceMapsCombinator.convert(rawBundleSourcemap); |
| 65 | result.sources = result.sources.map(p => { |
| 66 | return p.replace(/\\/g, "/"); |
| 67 | }); |
| 68 | assert.deepEqual(expected, result); |
| 69 | }); |
| 70 | }); |
| 71 | }); |