Skip to content

Commit

Permalink
fix: do not throw on parse errors (#34)
Browse files Browse the repository at this point in the history
* fix: do not throw when cannot parse source file

* do not crash when spec has bad syntax

* add readme note
  • Loading branch information
bahmutov authored Oct 4, 2023
1 parent 359c62b commit a207f8b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ $ npx find-ids --specs 'cypress/e2e/**/*.cy.{js,ts}' --command getBy --test-ids

This module uses [debug](https://github.com/debug-js/debug#readme) to output verbose logs. To see the logs, run with the following environment variable `DEBUG=changed-test-ids`

If a source file or a spec have syntax that cannot be parsed, they are ignored. You can see a debug message `changed-test-ids ⚠️ could not parse spec <filename> +0ms`

## Examples

- repo [bahmutov/taste-the-sauce-test-ids](https://github.com/bahmutov/taste-the-sauce-test-ids)
Expand Down
15 changes: 13 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,13 @@ function findTestQueries(source, options = {}) {
function findTestAttributesInFile(filename) {
debug('searching %s', filename)
const source = fs.readFileSync(filename, 'utf8')
return findTestAttributes(source)

try {
return findTestAttributes(source)
} catch (e) {
debug('⚠️ could not parse source code in file %s', filename)
return []
}
}

/**
Expand All @@ -191,7 +197,12 @@ function findTestAttributesInFiles(filenames) {
*/
function findTestQueriesInFile(filename, options = {}) {
const source = fs.readFileSync(filename, 'utf8')
return findTestQueries(source, options)
try {
return findTestQueries(source, options)
} catch (e) {
debug('⚠️ could not parse spec %s', filename)
return []
}
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/bad.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// this spec has bad syntax on purpose
// the parser should not crash
it('tests', () => {
cy.getTest('
})
3 changes: 3 additions & 0 deletions tests/fixtures/bad.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// this source file has bad syntax on purpose
// the parser should not crash, just move on
export default function something

0 comments on commit a207f8b

Please sign in to comment.