11import sinon , { type SinonStub } from "sinon" ;
2- import { SourceMapConsumer , type BasicSourceMapConsumer , type NullableMappedPosition } from "source-map" ;
2+ import { MappedPosition , SourceMapConsumer } from "source-map-js " ;
33import { extractSourceMaps , resolveLocationWithSourceMap } from "./../../../src/error-snippets/source-maps" ;
44import type { SufficientStackFrame , ResolvedFrame } from "../../../src/error-snippets/types" ;
55
@@ -17,13 +17,28 @@ describe("error-snippets/source-maps", () => {
1717 describe ( "extractSourceMaps" , ( ) => {
1818 it ( "should return null if source maps comment is not present in file content" , async ( ) => {
1919 const fileContents = 'console.log("Hello, World!");' ;
20- const fileName = "test.js" ;
20+ const fileName = "file:// test.js" ;
2121
2222 const result = await extractSourceMaps ( fileContents , fileName ) ;
2323
2424 assert . isNull ( result ) ;
2525 } ) ;
2626
27+ it ( "should return null if file has esm path" , async ( ) => {
28+ const inlineSourceMap =
29+ "data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IiJ9" ;
30+ const fileContents = `console.log("Hello, World!");\n//# sourceMappingURL=${ inlineSourceMap } ` ;
31+ const fileName = "file:///some/path/test.js" ;
32+ fetchStub . withArgs ( inlineSourceMap ) . resolves ( {
33+ text : ( ) => Promise . resolve ( '{"version":3,"sources":[],"names":[],"mappings":""}' ) ,
34+ headers : new Map ( ) ,
35+ } ) ;
36+
37+ const result = await extractSourceMaps ( fileContents , fileName ) ;
38+
39+ assert . instanceOf ( result , SourceMapConsumer ) ;
40+ } ) ;
41+
2742 it ( "should return a SourceMapConsumer instance if source maps comment is present in file content" , async ( ) => {
2843 const inlineSourceMap =
2944 "data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IiJ9" ;
@@ -42,14 +57,12 @@ describe("error-snippets/source-maps", () => {
4257
4358 describe ( "resolveLocationWithSourceMap" , ( ) => {
4459 it ( "should throw an error when source is null" , async ( ) => {
45- const sourceMaps = ( await new SourceMapConsumer (
46- JSON . stringify ( {
47- version : 3 ,
48- sources : [ ] ,
49- names : [ ] ,
50- mappings : "" ,
51- } ) ,
52- ) ) as BasicSourceMapConsumer ;
60+ const sourceMaps = new SourceMapConsumer ( {
61+ version : 3 as unknown as string ,
62+ sources : [ ] ,
63+ names : [ ] ,
64+ mappings : "" ,
65+ } ) ;
5366 const stackFrame = { lineNumber : 5 , columnNumber : 10 } as SufficientStackFrame ;
5467
5568 const fn = ( ) : ResolvedFrame => resolveLocationWithSourceMap ( stackFrame , sourceMaps ) ;
@@ -58,16 +71,15 @@ describe("error-snippets/source-maps", () => {
5871 } ) ;
5972
6073 it ( "should throw an error when line or column is null" , async ( ) => {
61- const sourceMaps = ( await new SourceMapConsumer (
62- JSON . stringify ( {
63- version : 3 ,
64- sources : [ "file1" ] ,
65- names : [ ] ,
66- mappings : "" ,
67- sourcesContent : [ "content" ] ,
68- } ) ,
69- ) ) as BasicSourceMapConsumer ;
70- sandbox . stub ( sourceMaps , "originalPositionFor" ) . returns ( { source : "file1" } as NullableMappedPosition ) ;
74+ const sourceMaps = new SourceMapConsumer ( {
75+ // Specification says it should be number
76+ version : 3 as unknown as string ,
77+ sources : [ "file1" ] ,
78+ names : [ ] ,
79+ mappings : "" ,
80+ sourcesContent : [ "content" ] ,
81+ } ) ;
82+ sandbox . stub ( sourceMaps , "originalPositionFor" ) . returns ( { source : "file1" } as MappedPosition ) ;
7183 const stackFrame = { lineNumber : 5 , columnNumber : 10 } as SufficientStackFrame ;
7284
7385 const fn = ( ) : ResolvedFrame => resolveLocationWithSourceMap ( stackFrame , sourceMaps ) ;
@@ -76,19 +88,17 @@ describe("error-snippets/source-maps", () => {
7688 } ) ;
7789
7890 it ( "should return ResolvedFrame" , async ( ) => {
79- const sourceMaps = ( await new SourceMapConsumer (
80- JSON . stringify ( {
81- version : 3 ,
82- sources : [ "file1" ] ,
83- names : [ ] ,
84- mappings : "AAAA;AACA" ,
85- sourcesContent : [ "content" ] ,
86- } ) ,
87- ) ) as BasicSourceMapConsumer ;
88- sourceMaps . file = "file:///file1" ;
91+ const sourceMaps = new SourceMapConsumer ( {
92+ version : 3 as unknown as string ,
93+ sources : [ "file1" ] ,
94+ names : [ ] ,
95+ mappings : "AAAA;AACA" ,
96+ sourcesContent : [ "content" ] ,
97+ file : "file:///file1" ,
98+ } ) ;
8999 sandbox
90100 . stub ( sourceMaps , "originalPositionFor" )
91- . returns ( { source : "file1" , line : 100 , column : 500 } as NullableMappedPosition ) ;
101+ . returns ( { source : "file1" , line : 100 , column : 500 } as MappedPosition ) ;
92102 const stackFrame = { lineNumber : 1 , columnNumber : 1 } as SufficientStackFrame ;
93103
94104 const result = resolveLocationWithSourceMap ( stackFrame , sourceMaps ) ;
0 commit comments