fix(vite): Only attempt to amend test object if one exists (#26822)

Test object is assigned using optional chaining but previously did not
check that any value had actually been assigned which would cause the
`getStart` call to fail resulting in a failed migration.

## Current Behavior
Migration fails if no matching test object is found.

## Expected Behavior
Migration should not fail.

## Related Issue(s)
I haven't opened an issue, just the PR. I can open an issue if required
for tracking.
This commit is contained in:
Jasper McCulloch 2024-07-09 14:47:37 +01:00 committed by GitHub
parent 73858a094e
commit 8fd38cba9a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -102,11 +102,14 @@ export function fixCoverageAndRerporters(
configNode, configNode,
`PropertyAssignment:has(Identifier[name="test"])` `PropertyAssignment:has(Identifier[name="test"])`
)?.[0]; )?.[0];
changes.push({
type: ChangeType.Insert, if (testObject) {
index: testObject.getStart() + `test: {`.length + 1, changes.push({
text: `reporters: ['default'],`, type: ChangeType.Insert,
}); index: testObject.getStart() + `test: {`.length + 1,
text: `reporters: ['default'],`,
});
}
} }
if (changes.length > 0) { if (changes.length > 0) {