1+ const _ = require ( 'lodash' )
12const chai = require ( 'chai' )
23const path = require ( 'path' )
34const snapshot = require ( 'snap-shot-it' )
5+ const Bluebird = require ( 'bluebird' )
46
57process . env . __TESTING__ = true
68
@@ -10,6 +12,8 @@ const preprocessor = require('../../index')
1012/* eslint-disable-next-line no-unused-vars */
1113const expect = chai . expect
1214
15+ const typescript = require . resolve ( 'typescript' )
16+
1317beforeEach ( ( ) => {
1418 fs . removeSync ( path . join ( __dirname , '_test-output' ) )
1519 preprocessor . reset ( )
@@ -21,13 +25,37 @@ const DEFAULT_OPTIONS = { browserifyOptions: { debug: false } }
2125const bundle = ( fixtureName , options = DEFAULT_OPTIONS ) => {
2226 const on = ( ) => { }
2327 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 )
2529
2630 return preprocessor ( options ) ( { filePath, outputPath, on } ) . then ( ( ) => {
2731 return fs . readFileSync ( outputPath ) . toString ( )
2832 } )
2933}
3034
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+
3159describe ( 'browserify preprocessor - e2e' , ( ) => {
3260 it ( 'correctly preprocesses the file' , ( ) => {
3361 return bundle ( 'example_spec.js' ) . then ( ( output ) => {
@@ -50,7 +78,6 @@ describe('browserify preprocessor - e2e', () => {
5078 } )
5179 } )
5280
53-
5481 it ( 'handles module.exports and import' , ( ) => {
5582 return bundle ( 'sub_spec.js' ) . then ( ( output ) => {
5683 // check that bundled tests work
@@ -88,16 +115,45 @@ describe('browserify preprocessor - e2e', () => {
88115 describe ( 'typescript' , ( ) => {
89116 it ( 'handles .ts file when the path is given' , ( ) => {
90117 return bundle ( 'typescript/math_spec.ts' , {
91- typescript : require . resolve ( 'typescript' ) ,
118+ typescript,
92119 } ) . then ( ( output ) => {
93120 // check that bundled tests work
94121 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 )
95151 } )
96152 } )
97153
98154 it ( 'handles .tsx file when the path is given' , ( ) => {
99155 return bundle ( 'typescript/react_spec.tsx' , {
100- typescript : require . resolve ( 'typescript' ) ,
156+ typescript,
101157 } ) . then ( ( output ) => {
102158 // check that bundled tests work
103159 eval ( output )
0 commit comments