Skip to content

fix: guard against undefined testFiles when findTests throws - #3473

Open
tsushanth wants to merge 1 commit into
avajs:mainfrom
tsushanth:fix/testfiles-undefined-on-glob-error
Open

fix: guard against undefined testFiles when findTests throws#3473
tsushanth wants to merge 1 commit into
avajs:mainfrom
tsushanth:fix/testfiles-undefined-on-glob-error

Conversation

@tsushanth

Copy link
Copy Markdown

What

When globs.findTests rejects (bad glob config, permission error, IO failure), the catch block in Api#run stores the error in setupOrGlobError but leaves testFiles as undefined. The very next statement builds selectionInsights:

const selectionInsights = {
  ...
  testFileCount: testFiles.length,   // ← TypeError: Cannot read properties of undefined
  ...
};

This crashes before setupOrGlobError is ever re-thrown, so the original error is swallowed and the user sees a confusing TypeError instead.

Fix

Use optional chaining so the property access is safe when testFiles is undefined:

testFileCount: testFiles?.length ?? 0,

Reproduction

Configure ava with a files glob that causes findTests to throw (e.g., pass an invalid pattern that triggers the internal validation error), or simulate an IO failure in globs.findTests. The run crashes with:

TypeError: Cannot read properties of undefined (reading 'length')
    at Api.run (lib/api.js:...)

instead of surfacing the underlying glob/config error.

When globs.findTests rejects (e.g. bad file patterns or IO errors), the
catch block sets setupOrGlobError but leaves testFiles undefined.
The subsequent selectionInsights object accesses testFiles.length, which
crashes with a TypeError before setupOrGlobError can be re-thrown.

Use optional chaining so a glob failure is surfaced via the existing
setupOrGlobError path rather than crashing unexpectedly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants