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

Commit 00e9be9

Browse files
authored
fix: Only enable TypeScript jsx compiling for .js, .jsx, and .tsx files (#45)
1 parent f549896 commit 00e9be9

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/simple_tsify.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
let through = require('through2')
1+
const through = require('through2')
2+
const path = require('path')
23

34
const isJson = (code) => {
45
try {
@@ -14,8 +15,9 @@ const isJson = (code) => {
1415
// It means it should check types whenever spec file is changed
1516
// and it slows down the test speed a lot.
1617
// We skip this slow type-checking process by using transpileModule() api.
17-
module.exports = function (b, opts) {
18+
module.exports = function (filePath, opts) {
1819
const chunks = []
20+
const ext = path.extname(filePath)
1921

2022
return through(
2123
(buf, enc, next) => {
@@ -32,7 +34,7 @@ module.exports = function (b, opts) {
3234
this.push(ts.transpileModule(text, {
3335
compilerOptions: {
3436
esModuleInterop: true,
35-
jsx: 'react',
37+
jsx: ext === '.tsx' || ext === '.jsx' || ext === '.js' ? 'react' : undefined,
3638
downlevelIteration: true,
3739
},
3840
}).outputText)

test/fixtures/typescript/math_spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ const { add } = math
55

66
const x: number = 3
77

8+
// ensures that generics can be properly compiled and not treated
9+
// as react components in `.ts` files.
10+
// https://github.com/cypress-io/cypress-browserify-preprocessor/issues/44
11+
const isKeyOf = <T>(obj: T, key: any): key is keyof T => {
12+
return typeof key === 'string' && key in obj;
13+
}
14+
815
context('math.ts', function () {
916
it('imports function', () => {
1017
expect(add, 'add').to.be.a('function')
@@ -20,4 +27,11 @@ context('math.ts', function () {
2027

2128
expect(arr[0] + arr[1]).to.eq(1)
2229
})
30+
it('Test generic', () => {
31+
const x = {
32+
key: 'value'
33+
}
34+
35+
expect(isKeyOf(x, 'key')).to.eq(true)
36+
})
2337
})

0 commit comments

Comments
 (0)