Skip to content

Commit

Permalink
fix: pass typescript plugin to parse source (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Oct 4, 2023
1 parent bf27b1d commit 50dc3b8
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 54 deletions.
102 changes: 51 additions & 51 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"dependencies": {
"@actions/core": "^1.10.1",
"@babel/core": "^7.22.17",
"@babel/core": "^7.23.0",
"@babel/parser": "^7.23.0",
"@babel/plugin-syntax-jsx": "^7.22.5",
"arg": "^5.0.2",
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ function findTestAttributes(source, options = {}) {

try {
ast = babel.parse(source, {
plugins: ['jsx'],
plugins: ['jsx', 'typescript'],
sourceType: 'script',
})
} catch (e) {
ast = babel.parse(source, {
plugins: ['jsx'],
plugins: ['jsx', 'typescript'],
sourceType: 'module',
})
}
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/address.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type Address = {
street?: string
}

export function Address() {
return <div data-cy="street">Main</div>
}
12 changes: 12 additions & 0 deletions tests/jsx-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,15 @@ test('finds data-testid attribute', (t) => {
const found = findTestAttributes(source)
t.deepEqual(found, ['greeting'])
})

test('handles optional ? operator', (t) => {
const source = stripIndent`
const person = {}
console.log(person?.name)
export const Greeting = () => {
return <div data-testid="greeting">Hello</div>
}
`
const found = findTestAttributes(source)
t.deepEqual(found, ['greeting'])
})
9 changes: 9 additions & 0 deletions tests/tsx-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const test = require('ava')
const path = require('path')
const { findTestAttributesInFile } = require('../src')

test('finds test ids in TSX file', (t) => {
const filename = path.join(__dirname, 'fixtures', 'address.tsx')
const found = findTestAttributesInFile(filename)
t.deepEqual(found, ['street'])
})

0 comments on commit 50dc3b8

Please sign in to comment.