Skip to content
This repository was archived by the owner on Jan 31, 2023. It is now read-only.

Commit d64122c

Browse files
authored
fix: generate sourcemaps on .ts and .tsx files (#51)
1 parent e4c5236 commit d64122c

File tree

5 files changed

+104
-16
lines changed

5 files changed

+104
-16
lines changed

lib/simple_tsify.js

+22-10
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,41 @@ const isJson = (code) => {
1515
// It means it should check types whenever spec file is changed
1616
// and it slows down the test speed a lot.
1717
// We skip this slow type-checking process by using transpileModule() api.
18-
module.exports = function (filePath, opts) {
18+
module.exports = function (fileName, opts) {
19+
const ts = opts.typescript
1920
const chunks = []
20-
const ext = path.extname(filePath)
21+
const ext = path.extname(fileName)
2122

2223
return through(
2324
(buf, enc, next) => {
2425
chunks.push(buf.toString())
2526
next()
2627
},
2728
function (next) {
28-
const ts = opts.typescript
2929
const text = chunks.join('')
3030

3131
if (isJson(text)) {
3232
this.push(text)
3333
} else {
34-
this.push(ts.transpileModule(text, {
35-
compilerOptions: {
36-
esModuleInterop: true,
37-
jsx: ext === '.tsx' || ext === '.jsx' || ext === '.js' ? 'react' : undefined,
38-
downlevelIteration: true,
39-
},
40-
}).outputText)
34+
this.push(
35+
ts.transpileModule(text, {
36+
// explicitly name the file here
37+
// for sourcemaps
38+
fileName,
39+
compilerOptions: {
40+
esModuleInterop: true,
41+
// inline the source maps into the file
42+
// https://github.com/cypress-io/cypress-browserify-preprocessor/issues/48
43+
inlineSourceMap: true,
44+
inlineSources: true,
45+
jsx:
46+
ext === '.tsx' || ext === '.jsx' || ext === '.js'
47+
? 'react'
48+
: undefined,
49+
downlevelIteration: true,
50+
},
51+
}).outputText,
52+
)
4153
}
4254

4355
next()

test/e2e/e2e_spec.js

+60-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
const _ = require('lodash')
12
const chai = require('chai')
23
const path = require('path')
34
const snapshot = require('snap-shot-it')
5+
const Bluebird = require('bluebird')
46

57
process.env.__TESTING__ = true
68

@@ -10,6 +12,8 @@ const preprocessor = require('../../index')
1012
/* eslint-disable-next-line no-unused-vars */
1113
const expect = chai.expect
1214

15+
const typescript = require.resolve('typescript')
16+
1317
beforeEach(() => {
1418
fs.removeSync(path.join(__dirname, '_test-output'))
1519
preprocessor.reset()
@@ -21,13 +25,37 @@ const DEFAULT_OPTIONS = { browserifyOptions: { debug: false } }
2125
const 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+
3159
describe('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)

test/fixtures/typescript/math.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
export const multiply = (a: number, b: number): number => {
2+
return a * b
3+
}
4+
15
export default {
26
add: (a: number, b: number) => {
37
return a + b
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { multiply } from './math'
2+
3+
describe('simple .tsx spec', () => {
4+
it('can import another module and add', () => {
5+
const EXPECTED = 6
6+
const result = multiply(2, 3)
7+
8+
if (result !== EXPECTED) {
9+
throw new Error(`multiplying 2*3 did not equal ${EXPECTED}. received: ${result}`)
10+
}
11+
})
12+
})

test/unit/index_spec.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ mockery.enable({
1919
mockery.registerMock('browserify', browserify)
2020

2121
const streamApi = {
22-
pipe () { return streamApi },
22+
pipe () {
23+
return streamApi
24+
},
2325
}
2426

2527
streamApi.on = sandbox.stub().returns(streamApi)
@@ -35,7 +37,9 @@ describe('browserify preprocessor', function () {
3537

3638
const bundlerApi = this.bundlerApi = {
3739
bundle: sandbox.stub().returns(streamApi),
38-
external () { return bundlerApi },
40+
external () {
41+
return bundlerApi
42+
},
3943
close: sandbox.spy(),
4044
plugin: sandbox.stub(),
4145
}

0 commit comments

Comments
 (0)