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

Commit

Permalink
fix: Only enable TypeScript jsx compiling for .js, .jsx, and `.ts…
Browse files Browse the repository at this point in the history
…x` files (#45)
  • Loading branch information
sainthkh authored May 8, 2020
1 parent f549896 commit 00e9be9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/simple_tsify.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
let through = require('through2')
const through = require('through2')
const path = require('path')

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

return through(
(buf, enc, next) => {
Expand All @@ -32,7 +34,7 @@ module.exports = function (b, opts) {
this.push(ts.transpileModule(text, {
compilerOptions: {
esModuleInterop: true,
jsx: 'react',
jsx: ext === '.tsx' || ext === '.jsx' || ext === '.js' ? 'react' : undefined,
downlevelIteration: true,
},
}).outputText)
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/typescript/math_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ const { add } = math

const x: number = 3

// ensures that generics can be properly compiled and not treated
// as react components in `.ts` files.
// https://github.com/cypress-io/cypress-browserify-preprocessor/issues/44
const isKeyOf = <T>(obj: T, key: any): key is keyof T => {
return typeof key === 'string' && key in obj;
}

context('math.ts', function () {
it('imports function', () => {
expect(add, 'add').to.be.a('function')
Expand All @@ -20,4 +27,11 @@ context('math.ts', function () {

expect(arr[0] + arr[1]).to.eq(1)
})
it('Test generic', () => {
const x = {
key: 'value'
}

expect(isKeyOf(x, 'key')).to.eq(true)
})
})

0 comments on commit 00e9be9

Please sign in to comment.