-
Notifications
You must be signed in to change notification settings - Fork 214
@W-18670301 - added prettier and improved tests for trim-extensions script #2688
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
Merged
kzheng-sfdc
merged 4 commits into
feature/extensibility-v2
from
u/kzheng/W-18670301/prettier
Jun 30, 2025
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| */ | ||
| /* eslint-disable @typescript-eslint/no-var-requires */ | ||
| const fs = require('fs') | ||
| const {execSync} = require('child_process') | ||
|
|
||
| jest.mock('../assets/plugin-config', () => ({ | ||
| plugins: { | ||
|
|
@@ -19,6 +20,41 @@ jest.mock('../assets/plugin-config', () => ({ | |
| })) | ||
|
|
||
| jest.mock('fs') | ||
| jest.mock('child_process') | ||
|
|
||
| // custom matcher to compare strings line by line with trimming | ||
| expect.extend({ | ||
| toEqualTrimmedLines(received, expected) { | ||
| const clean = (str) => | ||
| str | ||
| .split('\n') | ||
| .map((line) => line.trim()) // Trim each line | ||
| .filter((line) => line.length > 0) // Optional: remove empty lines | ||
|
|
||
| const receivedLines = clean(received) | ||
| const expectedLines = clean(expected) | ||
|
|
||
| const pass = this.equals(receivedLines, expectedLines) | ||
|
|
||
| if (pass) { | ||
| return { | ||
| pass: true, | ||
| message: () => | ||
| `✅ Expected strings not to match line by line (but they did).\n\nExpected: ${this.utils.printExpected( | ||
| expectedLines | ||
| )}\nReceived: ${this.utils.printReceived(receivedLines)}` | ||
| } | ||
| } else { | ||
| return { | ||
| pass: false, | ||
| message: () => | ||
| `❌ Expected strings to match line by line (with trimming).\n\nExpected: ${this.utils.printExpected( | ||
| expectedLines | ||
| )}\nReceived: ${this.utils.printReceived(receivedLines)}` | ||
| } | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| const trimExtensions = require('./trim-extensions') | ||
|
|
||
|
|
@@ -47,6 +83,7 @@ describe('trim-extensions', () => { | |
| } | ||
| }) | ||
| fs.unlinkSync.mockReturnValue(true) | ||
| execSync.mockReturnValue(true) | ||
| }) | ||
|
|
||
| it('handles OR operator correctly', () => { | ||
|
|
@@ -62,7 +99,10 @@ describe('trim-extensions', () => { | |
| trimExtensions('/mock/dir', {SFDC_EXT_featureA: true, SFDC_EXT_featureB: false}) | ||
| expect(fs.writeFileSync).toHaveBeenCalledWith( | ||
| expect.any(String), | ||
| "const feature = 'Feature Enabled';" | ||
| expect.toEqualTrimmedLines("const feature = 'Feature Enabled';") | ||
| ) | ||
| expect(execSync).toHaveBeenCalledWith( | ||
| 'npx prettier --write /mock/dir/src/components/featureComponent.jsx' | ||
| ) | ||
| }) | ||
|
|
||
|
|
@@ -82,12 +122,15 @@ describe('trim-extensions', () => { | |
|
|
||
| expect(fs.writeFileSync).toHaveBeenCalledWith( | ||
| expect.any(String), | ||
| "const featureAFunc = () => 'Feature A';" | ||
| expect.toEqualTrimmedLines("const featureAFunc = () => 'Feature A';") | ||
| ) | ||
| expect(fs.writeFileSync).toHaveBeenCalledWith( | ||
| expect.any(String), | ||
| expect.not.stringContaining("const featureBFunc = () => 'Feature B';") | ||
| ) | ||
| expect(execSync).toHaveBeenCalledWith( | ||
| 'npx prettier --write /mock/dir/src/components/featureComponent.jsx' | ||
| ) | ||
| }) | ||
|
|
||
| it('handles variable with ternary expressions correctly', () => { | ||
|
|
@@ -104,12 +147,15 @@ describe('trim-extensions', () => { | |
|
|
||
| expect(fs.writeFileSync).toHaveBeenCalledWith( | ||
| expect.any(String), | ||
| 'const showFeature = Feature_A;' | ||
| expect.toEqualTrimmedLines('const showFeature = Feature_A;') | ||
| ) | ||
| expect(fs.writeFileSync).toHaveBeenCalledWith( | ||
| expect.any(String), | ||
| expect.not.stringContaining('const showFeature = Feature_B') | ||
| ) | ||
| expect(execSync).toHaveBeenCalledWith( | ||
| 'npx prettier --write /mock/dir/src/components/featureComponent.jsx' | ||
| ) | ||
| }) | ||
|
|
||
| it('handles return with ternary expressions correctly', () => { | ||
|
|
@@ -122,9 +168,59 @@ describe('trim-extensions', () => { | |
|
|
||
| trimExtensions('/mock/dir', {SFDC_EXT_featureA: true}) | ||
|
|
||
| const expected = ` | ||
| function test() { | ||
| return Feature_A; | ||
| } | ||
| ` | ||
| expect(fs.writeFileSync).toHaveBeenCalledWith( | ||
| expect.any(String), | ||
| expect.toEqualTrimmedLines(expected) | ||
| ) | ||
| expect(execSync).toHaveBeenCalledWith( | ||
| 'npx prettier --write /mock/dir/src/components/featureComponent.jsx' | ||
| ) | ||
| }) | ||
|
|
||
| it('handles PropTypes declarations correctly', () => { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a new test case to demonstrate how to extend PropType declaration |
||
| const code = ` | ||
| MyClass.PropTypes = { | ||
| name: PropTypes.string, | ||
| description: PropTypes.string | ||
| }; | ||
| SFDC_EXT_featureA && (MyClass.PropType = { | ||
| ...MyClass.PropType, | ||
| featureAProp: PropTypes.string | ||
| }); | ||
| SFDC_EXT_featureB && (MyClass.PropType = { | ||
| ...MyClass.PropType, | ||
| featureBProp: PropTypes.string | ||
| }); | ||
| ` | ||
| fs.readFileSync.mockReturnValue(code) | ||
|
|
||
| trimExtensions('/mock/dir', {SFDC_EXT_featureA: true}) | ||
|
|
||
| const expected = ` | ||
| MyClass.PropTypes = { | ||
| name: PropTypes.string, | ||
| description: PropTypes.string | ||
| }; | ||
| MyClass.PropType = { | ||
| ...MyClass.PropType, | ||
| featureAProp: PropTypes.string | ||
| }; | ||
| ` | ||
| expect(fs.writeFileSync).toHaveBeenCalledWith( | ||
| expect.any(String), | ||
| expect.stringContaining('return Feature_A') | ||
| expect.toEqualTrimmedLines(expected) | ||
| ) | ||
| expect(fs.writeFileSync).not.toHaveBeenCalledWith( | ||
| expect.any(String), | ||
| expect.stringContaining('featureBProp: PropTypes.string') | ||
| ) | ||
| expect(execSync).toHaveBeenCalledWith( | ||
| 'npx prettier --write /mock/dir/src/components/featureComponent.jsx' | ||
| ) | ||
| }) | ||
|
|
||
|
|
@@ -143,14 +239,25 @@ describe('trim-extensions', () => { | |
|
|
||
| trimExtensions('/mock/dir', {SFDC_EXT_featureA: true, SFDC_EXT_featureB: false}) | ||
|
|
||
| const expected = ` | ||
| function test() { | ||
| return ( | ||
| <div> | ||
| <ComponentA /> | ||
| </div>); | ||
| } | ||
| ` | ||
| expect(fs.writeFileSync).toHaveBeenCalledWith( | ||
| expect.any(String), | ||
| expect.stringContaining('<ComponentA />') | ||
| expect.toEqualTrimmedLines(expected) | ||
| ) | ||
| expect(fs.writeFileSync).not.toHaveBeenCalledWith( | ||
| expect.any(String), | ||
| expect.stringContaining('<ComponentB />') | ||
| ) | ||
| expect(execSync).toHaveBeenCalledWith( | ||
| 'npx prettier --write /mock/dir/src/components/featureComponent.jsx' | ||
| ) | ||
| }) | ||
|
|
||
| it('does not remove referenced imports', () => { | ||
|
|
||
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.
Call prettier in a child process because it requires ES module, which this package doesn't support.