1
+ const _ = require ( 'lodash' )
1
2
const chai = require ( 'chai' )
2
3
const path = require ( 'path' )
3
4
const snapshot = require ( 'snap-shot-it' )
5
+ const Bluebird = require ( 'bluebird' )
4
6
5
7
process . env . __TESTING__ = true
6
8
@@ -10,6 +12,8 @@ const preprocessor = require('../../index')
10
12
/* eslint-disable-next-line no-unused-vars */
11
13
const expect = chai . expect
12
14
15
+ const typescript = require . resolve ( 'typescript' )
16
+
13
17
beforeEach ( ( ) => {
14
18
fs . removeSync ( path . join ( __dirname , '_test-output' ) )
15
19
preprocessor . reset ( )
@@ -21,13 +25,37 @@ const DEFAULT_OPTIONS = { browserifyOptions: { debug: false } }
21
25
const bundle = ( fixtureName , options = DEFAULT_OPTIONS ) => {
22
26
const on = ( ) => { }
23
27
const filePath = path . join ( __dirname , '..' , 'fixtures' , fixtureName )
24
- const outputPath = path . join ( __dirname , '..' , '_test-output' , 'output.js' )
28
+ const outputPath = path . join ( __dirname , '..' , '_test-output' , fixtureName )
25
29
26
30
return preprocessor ( options ) ( { filePath, outputPath, on } ) . then ( ( ) => {
27
31
return fs . readFileSync ( outputPath ) . toString ( )
28
32
} )
29
33
}
30
34
35
+ const parseSourceMap = ( output ) => {
36
+ return _
37
+ . chain ( output )
38
+ . split ( '//# sourceMappingURL=data:application/json;charset=utf-8;base64,' )
39
+ . last ( )
40
+ . thru ( ( str ) => {
41
+ const base64 = Buffer . from ( str , 'base64' ) . toString ( )
42
+
43
+ return JSON . parse ( base64 )
44
+ } )
45
+ . value ( )
46
+ }
47
+
48
+ const verifySourceContents = ( { sources, sourcesContent } ) => {
49
+ const zippedArrays = _ . zip ( sources , sourcesContent )
50
+
51
+ return Bluebird . map ( zippedArrays , ( [ sourcePath , sourceContent ] ) => {
52
+ return fs . readFileAsync ( sourcePath , 'utf8' )
53
+ . then ( ( str ) => {
54
+ expect ( str ) . to . eq ( sourceContent )
55
+ } )
56
+ } )
57
+ }
58
+
31
59
describe ( 'browserify preprocessor - e2e' , ( ) => {
32
60
it ( 'correctly preprocesses the file' , ( ) => {
33
61
return bundle ( 'example_spec.js' ) . then ( ( output ) => {
@@ -50,7 +78,6 @@ describe('browserify preprocessor - e2e', () => {
50
78
} )
51
79
} )
52
80
53
-
54
81
it ( 'handles module.exports and import' , ( ) => {
55
82
return bundle ( 'sub_spec.js' ) . then ( ( output ) => {
56
83
// check that bundled tests work
@@ -88,16 +115,45 @@ describe('browserify preprocessor - e2e', () => {
88
115
describe ( 'typescript' , ( ) => {
89
116
it ( 'handles .ts file when the path is given' , ( ) => {
90
117
return bundle ( 'typescript/math_spec.ts' , {
91
- typescript : require . resolve ( 'typescript' ) ,
118
+ typescript,
92
119
} ) . then ( ( output ) => {
93
120
// check that bundled tests work
94
121
eval ( output )
122
+
123
+ const sourceMap = parseSourceMap ( output )
124
+
125
+ expect ( sourceMap . sources ) . to . deep . eq ( [
126
+ 'node_modules/browser-pack/_prelude.js' ,
127
+ 'test/fixtures/typescript/math.ts' ,
128
+ 'test/fixtures/typescript/math_spec.ts' ,
129
+ ] )
130
+
131
+ return verifySourceContents ( sourceMap )
132
+ } )
133
+ } )
134
+
135
+ it ( 'handles simple .tsx file with imports' , ( ) => {
136
+ return bundle ( 'typescript/simple.spec.tsx' , {
137
+ typescript,
138
+ } ) . then ( ( output ) => {
139
+ // check that bundled tests work
140
+ eval ( output )
141
+
142
+ const sourceMap = parseSourceMap ( output )
143
+
144
+ expect ( sourceMap . sources ) . to . deep . eq ( [
145
+ 'node_modules/browser-pack/_prelude.js' ,
146
+ 'test/fixtures/typescript/math.ts' ,
147
+ 'test/fixtures/typescript/simple.spec.tsx' ,
148
+ ] )
149
+
150
+ return verifySourceContents ( sourceMap )
95
151
} )
96
152
} )
97
153
98
154
it ( 'handles .tsx file when the path is given' , ( ) => {
99
155
return bundle ( 'typescript/react_spec.tsx' , {
100
- typescript : require . resolve ( 'typescript' ) ,
156
+ typescript,
101
157
} ) . then ( ( output ) => {
102
158
// check that bundled tests work
103
159
eval ( output )
0 commit comments