5
5
* LICENSE file in the root directory of this source tree.
6
6
*/
7
7
8
+ import { originalPositionFor } from '@jridgewell/trace-mapping' ;
8
9
import * as fs from 'graceful-fs' ;
9
- import SourceMap from 'source-map' ;
10
10
import getCallsite from '../getCallsite' ;
11
11
12
12
jest . mock ( 'graceful-fs' ) ;
13
+ jest . mock ( '@jridgewell/trace-mapping' , ( ) => {
14
+ const actual = jest . requireActual ( '@jridgewell/trace-mapping' ) ;
15
+
16
+ return {
17
+ ...actual ,
18
+ originalPositionFor : jest . fn ( actual . originalPositionFor ) ,
19
+ } ;
20
+ } ) ;
13
21
14
22
describe ( 'getCallsite' , ( ) => {
15
23
test ( 'without source map' , ( ) => {
@@ -35,30 +43,35 @@ describe('getCallsite', () => {
35
43
} ) ;
36
44
37
45
test ( 'reads source map file to determine line and column' , ( ) => {
38
- ( fs . readFileSync as jest . Mock ) . mockImplementation ( ( ) => 'file data' ) ;
46
+ ( fs . readFileSync as jest . Mock ) . mockImplementation ( ( ) =>
47
+ JSON . stringify ( {
48
+ file : 'file.js' ,
49
+ mappings : 'AAAA,OAAO,MAAM,KAAK,GAAG,QAAd' ,
50
+ names : [ ] ,
51
+ sources : [ 'file.js' ] ,
52
+ sourcesContent : [ "export const hello = 'foobar';\\n" ] ,
53
+ version : 3 ,
54
+ } ) ,
55
+ ) ;
39
56
40
57
const sourceMapColumn = 1 ;
41
58
const sourceMapLine = 2 ;
42
59
43
- SourceMap . SourceMapConsumer = class {
44
- originalPositionFor ( params : Record < string , number > ) {
45
- expect ( params ) . toMatchObject ( {
46
- column : expect . any ( Number ) ,
47
- line : expect . any ( Number ) ,
48
- } ) ;
49
-
50
- return {
51
- column : sourceMapColumn ,
52
- line : sourceMapLine ,
53
- } ;
54
- }
55
- } ;
60
+ jest . mocked ( originalPositionFor ) . mockImplementation ( ( ) => ( {
61
+ column : sourceMapColumn ,
62
+ line : sourceMapLine ,
63
+ } ) ) ;
56
64
57
65
const site = getCallsite ( 0 , new Map ( [ [ __filename , 'mockedSourceMapFile' ] ] ) ) ;
58
66
59
67
expect ( site . getFileName ( ) ) . toEqual ( __filename ) ;
60
68
expect ( site . getColumnNumber ( ) ) . toEqual ( sourceMapColumn ) ;
61
69
expect ( site . getLineNumber ( ) ) . toEqual ( sourceMapLine ) ;
70
+ expect ( originalPositionFor ) . toHaveBeenCalledTimes ( 1 ) ;
71
+ expect ( originalPositionFor ) . toHaveBeenCalledWith ( expect . anything ( ) , {
72
+ column : expect . any ( Number ) ,
73
+ line : expect . any ( Number ) ,
74
+ } ) ;
62
75
expect ( fs . readFileSync ) . toHaveBeenCalledWith ( 'mockedSourceMapFile' , 'utf8' ) ;
63
76
} ) ;
64
77
} ) ;
0 commit comments