-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(vite): vitest migration add reporters #20823
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
packages/vite/src/migrations/update-17-3-0/lib/fix-coverage-and-reporters.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import { ChangeType, applyChangesToString } from '@nx/devkit'; | ||
import { tsquery } from '@phenomnomnominal/tsquery'; | ||
import ts = require('typescript'); | ||
|
||
export function fixCoverageAndRerporters( | ||
configContents: string | ||
): string | undefined { | ||
const configNode = getConfigNode(configContents); | ||
if (!configNode) { | ||
return; | ||
} | ||
|
||
const testHasCoverage = tsquery.query( | ||
configNode, | ||
`PropertyAssignment:has(Identifier[name="test"]):has(PropertyAssignment:has(Identifier[name="coverage"]))` | ||
)?.[0]; | ||
let changes = []; | ||
|
||
if (testHasCoverage) { | ||
const testObjectLiteralExpressionNode = tsquery.query( | ||
testHasCoverage, | ||
`ObjectLiteralExpression:has(Identifier[name="coverage"])` | ||
)?.[0]; | ||
const coverageNode = findCoverageNode(testObjectLiteralExpressionNode); | ||
|
||
if (!coverageNode) { | ||
return; | ||
} | ||
|
||
const linesNode = tsquery.query( | ||
coverageNode, | ||
`PropertyAssignment:has(Identifier[name="lines"])` | ||
)?.[0]; | ||
|
||
const statementsNode = tsquery.query( | ||
coverageNode, | ||
`PropertyAssignment:has(Identifier[name="statements"])` | ||
)?.[0]; | ||
|
||
const functionsNode = tsquery.query( | ||
coverageNode, | ||
`PropertyAssignment:has(Identifier[name="functions"])` | ||
)?.[0]; | ||
|
||
const branchesNode = tsquery.query( | ||
coverageNode, | ||
`PropertyAssignment:has(Identifier[name="branches"])` | ||
)?.[0]; | ||
|
||
if (linesNode) { | ||
changes.push({ | ||
type: ChangeType.Delete, | ||
start: linesNode.getStart(), | ||
length: linesNode.getWidth() + 1, | ||
}); | ||
} | ||
if (statementsNode) { | ||
changes.push({ | ||
type: ChangeType.Delete, | ||
start: statementsNode.getStart(), | ||
length: statementsNode.getWidth() + 1, | ||
}); | ||
} | ||
|
||
if (functionsNode) { | ||
changes.push({ | ||
type: ChangeType.Delete, | ||
start: functionsNode.getStart(), | ||
length: functionsNode.getWidth() + 1, | ||
}); | ||
} | ||
|
||
if (branchesNode) { | ||
changes.push({ | ||
type: ChangeType.Delete, | ||
start: branchesNode.getStart(), | ||
length: branchesNode.getWidth() + 1, | ||
}); | ||
} | ||
|
||
if (branchesNode || functionsNode || statementsNode || linesNode) { | ||
changes.push({ | ||
type: ChangeType.Insert, | ||
index: coverageNode.getStart() + 1, | ||
text: `thresholds: { | ||
${linesNode ? linesNode.getText() + ',' : ''} | ||
${statementsNode ? statementsNode.getText() + ',' : ''} | ||
${functionsNode ? functionsNode.getText() + ',' : ''} | ||
${branchesNode ? branchesNode.getText() + ',' : ''} | ||
},`, | ||
}); | ||
} | ||
} | ||
|
||
const testHasReporters = tsquery.query( | ||
configNode, | ||
`PropertyAssignment:has(Identifier[name="test"]):has(PropertyAssignment:has(Identifier[name="reporters"]))` | ||
)?.[0]; | ||
|
||
if (!testHasReporters) { | ||
const testObject = tsquery.query( | ||
configNode, | ||
`PropertyAssignment:has(Identifier[name="test"])` | ||
)?.[0]; | ||
changes.push({ | ||
type: ChangeType.Insert, | ||
index: testObject.getStart() + `test: {`.length + 1, | ||
text: `reporters: ['default'],`, | ||
}); | ||
} | ||
|
||
if (changes.length > 0) { | ||
return applyChangesToString(configContents, changes); | ||
} else { | ||
return; | ||
} | ||
} | ||
|
||
export function getConfigNode(configFileContents: string): ts.Node | undefined { | ||
if (!configFileContents) { | ||
return; | ||
} | ||
let configNode = tsquery.query( | ||
configFileContents, | ||
`ObjectLiteralExpression` | ||
)?.[0]; | ||
|
||
const arrowFunctionReturnStatement = tsquery.query( | ||
configFileContents, | ||
`ArrowFunction Block ReturnStatement ObjectLiteralExpression` | ||
)?.[0]; | ||
|
||
if (arrowFunctionReturnStatement) { | ||
configNode = arrowFunctionReturnStatement; | ||
} | ||
|
||
return configNode; | ||
} | ||
|
||
function findCoverageNode(testNode: ts.Node) { | ||
let coverageNode: ts.Node | undefined; | ||
testNode.forEachChild((child) => { | ||
if (ts.isPropertyAssignment(child) && child.name.getText() === 'coverage') { | ||
coverageNode = child.initializer; | ||
} | ||
}); | ||
return coverageNode; | ||
} |
132 changes: 0 additions & 132 deletions
132
packages/vite/src/migrations/update-17-3-0/lib/fix-coverage.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So
buildTarget
options should override theserve
target options? 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, because ideally there would not be any overriding done, since there are no common options in serve and build. In any case, it's better to configure your project in
vite.config.ts
!if this surfaces any errors we can revisit, but we're trying to keep the executor logic as minimal as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see how this may seem confusing or wrong, however here's the thing:
When the options are loaded in the executor, the executor loads the main options from build, along with the options under the
development
configuration, for example:It's up to the user to make sure these are set the way the user prefers.
In this case, it would load from build the following:
And the options that would be loaded from serve would be:
All these will be simplified in the near future, when we will be moving away from executors, and the logic will be much more transparent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh! I see!
serve
andbuild
targets have different schemas.So that's why ideally there shouldn't be any overrides. It's the extra args that kinda allows the overriding. That got me confused. Although it's possible to setup
mode
in theserve
target options, it shouldn't be done. Is that correct?Thanks for the taking the time to explain! 🙇🏻♂️