-
Notifications
You must be signed in to change notification settings - Fork 183
Add a single snapshot test to diffs #182
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
Merged
Changes from all commits
Commits
Show all changes
5 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
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
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 |
|---|---|---|
| @@ -1,3 +1,26 @@ | ||
| # @pierre/precision-diffs | ||
|
|
||
| Docs at [https://pierrejs-docs.vercel.app/](https://pierrejs-docs.vercel.app/) | ||
|
|
||
| ## Development | ||
|
|
||
| ### Building | ||
|
|
||
| ```bash | ||
| bun run build | ||
| ``` | ||
|
|
||
| ### Testing | ||
|
|
||
| ```bash | ||
| # Run tests | ||
| bun test | ||
|
|
||
| # Update snapshots | ||
| bun test --update-snapshots | ||
|
|
||
| # Type checking | ||
| bun run tsc | ||
| ``` | ||
|
|
||
| Tests are located in the `test/` folder and use Bun's native testing framework with snapshot support. |
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 |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| import { describe, expect, test } from 'bun:test'; | ||
| import type { Element, ElementContent } from 'hast'; | ||
|
|
||
| import { FileRenderer } from '../src/FileRenderer'; | ||
| import { mockFiles } from './mocks'; | ||
|
|
||
| describe('FileRenderer AST Structure', () => { | ||
| test('should generate correct AST structure for JavaScript file', async () => { | ||
| const instance = new FileRenderer(); | ||
| const result = await instance.render(mockFiles.file2); | ||
|
|
||
| expect(result).toBeDefined(); | ||
| const { codeAST, totalLines } = result!; | ||
|
|
||
| // Verify line count matches | ||
| const inputLines = mockFiles.file2.contents.split('\n').length; | ||
| expect(totalLines).toBe(inputLines); | ||
| expect(codeAST.length).toBe(inputLines); | ||
|
|
||
| // Verify each line has the expected structure | ||
| for (let i = 0; i < codeAST.length; i++) { | ||
| const lineElement = codeAST[i] as Element; | ||
|
|
||
| // Each line should be a div element | ||
| expect(lineElement.type).toBe('element'); | ||
| expect(lineElement.tagName).toBe('div'); | ||
|
|
||
| // Each line should have the correct properties | ||
| expect(lineElement.properties).toBeDefined(); | ||
| expect(lineElement.properties['data-line']).toBe(i + 1); | ||
| expect(lineElement.properties['data-line-type']).toBe('context'); | ||
| expect(lineElement.properties['data-line-index']).toBe(i); | ||
|
|
||
| // Each line should have two child spans: column-number and column-content | ||
| expect(lineElement.children).toBeDefined(); | ||
| expect(lineElement.children.length).toBe(2); | ||
|
|
||
| const [numberColumn, contentColumn] = lineElement.children as Element[]; | ||
|
|
||
| // Verify line number column | ||
| expect(numberColumn.type).toBe('element'); | ||
| expect(numberColumn.tagName).toBe('span'); | ||
| expect(numberColumn.properties['data-column-number']).toBe(''); | ||
|
|
||
| // Verify content column | ||
| expect(contentColumn.type).toBe('element'); | ||
| expect(contentColumn.tagName).toBe('span'); | ||
| expect(contentColumn.properties['data-column-content']).toBe(''); | ||
| } | ||
| }); | ||
|
|
||
| test('should apply syntax highlighting with CSS variables', async () => { | ||
| const instance = new FileRenderer(); | ||
| const result = await instance.render(mockFiles.file2); | ||
|
|
||
| expect(result).toBeDefined(); | ||
| const { codeAST } = result!; | ||
|
|
||
| // Helper to recursively find all text nodes with their parent styles | ||
| const findTextNodesWithStyles = ( | ||
| nodes: ElementContent[] | ||
| ): Array<{ text: string; style?: string }> => { | ||
| const results: Array<{ text: string; style?: string }> = []; | ||
|
|
||
| const traverse = (node: ElementContent, parentStyle?: string) => { | ||
| if (node.type === 'text') { | ||
| results.push({ text: node.value, style: parentStyle }); | ||
| } else if (node.type === 'element') { | ||
| const style = | ||
| typeof node.properties?.style === 'string' | ||
| ? node.properties.style | ||
| : undefined; | ||
| node.children?.forEach((child) => | ||
| traverse(child, style ?? parentStyle) | ||
| ); | ||
| } | ||
| }; | ||
|
|
||
| nodes.forEach((node) => traverse(node)); | ||
| return results; | ||
| }; | ||
|
|
||
| const textNodes = findTextNodesWithStyles(codeAST); | ||
|
|
||
| // Verify that at least some tokens have syntax highlighting with CSS variables | ||
| const styledTokens = textNodes.filter((node) => node.style !== undefined); | ||
| expect(styledTokens.length).toBeGreaterThan(0); | ||
|
|
||
| // Verify that styled tokens have the expected CSS variable format | ||
| const tokensWithCSSVars = styledTokens.filter( | ||
| (node) => | ||
| node.style?.match( | ||
| /--pjs-dark:#[A-F0-9]{6};--pjs-light:#[A-F0-9]{6}/ | ||
| ) !== null | ||
| ); | ||
| expect(tokensWithCSSVars.length).toBeGreaterThan(0); | ||
|
|
||
| // Verify specific keyword exists and is highlighted | ||
| const functionToken = textNodes.find((node) => node.text === 'function'); | ||
| expect(functionToken).toBeDefined(); | ||
| expect(functionToken!.style).toBeDefined(); | ||
| expect(functionToken!.style).toMatch( | ||
| /--pjs-dark:#[A-F0-9]{6};--pjs-light:#[A-F0-9]{6}/ | ||
| ); | ||
| }); | ||
|
|
||
| test('should generate correct totalLines count', async () => { | ||
| const instance = new FileRenderer(); | ||
|
|
||
| // Test file1 (TypeScript) | ||
| const result1 = await instance.render(mockFiles.file1); | ||
| expect(result1).toBeDefined(); | ||
| const file1Lines = mockFiles.file1.contents.split('\n').length; | ||
| expect(result1!.totalLines).toBe(file1Lines); | ||
|
|
||
| // Test file2 (JavaScript) | ||
| const result2 = await instance.render(mockFiles.file2); | ||
| expect(result2).toBeDefined(); | ||
| const file2Lines = mockFiles.file2.contents.split('\n').length; | ||
| expect(result2!.totalLines).toBe(file2Lines); | ||
| }); | ||
|
|
||
| test('should include CSS property in result', async () => { | ||
| const instance = new FileRenderer(); | ||
| const result = await instance.render(mockFiles.file2); | ||
|
|
||
| expect(result).toBeDefined(); | ||
| expect(result!.css).toBeDefined(); | ||
| expect(typeof result!.css).toBe('string'); | ||
| // CSS may be empty string depending on theme configuration | ||
| }); | ||
|
|
||
| test('should create preNode with correct properties', async () => { | ||
| const instance = new FileRenderer(); | ||
| const result = await instance.render(mockFiles.file2); | ||
|
|
||
| expect(result).toBeDefined(); | ||
| const { preNode } = result!; | ||
|
|
||
| expect(preNode.type).toBe('element'); | ||
| expect(preNode.tagName).toBe('pre'); | ||
| expect(preNode.properties).toBeDefined(); | ||
| }); | ||
| }); |
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 |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { describe, expect, test } from 'bun:test'; | ||
|
|
||
| import { FileRenderer } from '../src/FileRenderer'; | ||
| import { mockFiles } from './mocks'; | ||
|
|
||
| describe('FileRenderer', () => { | ||
| test('should render TypeScript code to AST matching snapshot', async () => { | ||
| const instance = new FileRenderer(); | ||
| const result = await instance.render(mockFiles.file1); | ||
|
|
||
| expect(result).toBeDefined(); | ||
| expect(result?.codeAST).toMatchSnapshot(); | ||
| }); | ||
| }); |
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.
Should we let the AI know about this? Could that result in them seeing a failing test and then going
oh, well, i'll just update the snapshotstofixthe problem?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.
these are like an industry standard, so AI should know when to do it and when to not.