Repository navigation
fix(theme-mermaid): serialize initialize and render calls to prevent empty SVGs (#12757) #12923
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
Open
Aryan9891
wants to merge
1
commit into
meta:main
Choose a base branch
from
Aryan9891:fix/theme-mermaid-serialize-renders-12757
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+233
−37
Open
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
167 changes: 167 additions & 0 deletions
167
packages/docusaurus-theme-mermaid/src/__tests__/renderMermaid.test.ts
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,167 @@ | ||
| /** | ||
| * Copyright (c) Facebook, Inc. and its affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| import {describe, expect, it, vi, beforeEach} from 'vitest'; | ||
| import {renderMermaid, resetRenderQueueForTest} from '../client/index'; | ||
| import * as loadMermaidModule from '../client/loadMermaid'; | ||
| import type {MermaidConfig, RenderResult} from 'mermaid'; | ||
|
|
||
| describe('renderMermaid', () => { | ||
| beforeEach(() => { | ||
| resetRenderQueueForTest(); | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('serialize concurrent renders so initialize and render never overlap', async () => { | ||
| const executionTrace: string[] = []; | ||
| let activeRenders = 0; | ||
| let maxConcurrentRenders = 0; | ||
|
|
||
| const mockMermaid = { | ||
| initialize: vi.fn((config: MermaidConfig) => { | ||
| executionTrace.push(`initialize:${config.theme}`); | ||
| }), | ||
| render: vi.fn(async (id: string, text: string): Promise<RenderResult> => { | ||
| activeRenders++; | ||
| if (activeRenders > maxConcurrentRenders) { | ||
| maxConcurrentRenders = activeRenders; | ||
| } | ||
| executionTrace.push(`render:start:${id}`); | ||
|
|
||
| // Simulate async layout/rendering time | ||
| await new Promise((resolve) => setTimeout(resolve, 20)); | ||
|
|
||
| executionTrace.push(`render:end:${id}`); | ||
| activeRenders--; | ||
| return { | ||
| svg: `<svg id="${id}">${text}</svg>`, | ||
| diagramType: 'flowchart', | ||
| }; | ||
| }), | ||
| }; | ||
|
|
||
| vi.spyOn(loadMermaidModule, 'loadMermaid').mockResolvedValue( | ||
| mockMermaid as unknown as typeof loadMermaidModule.loadMermaid extends () => Promise< | ||
| infer M | ||
| > | ||
| ? M | ||
| : never, | ||
| ); | ||
|
|
||
| // Call 3 renders concurrently without awaiting | ||
| const p1 = renderMermaid({ | ||
| id: 'd1', | ||
| text: 'graph TD; A-->B', | ||
| config: {theme: 'default'}, | ||
| }); | ||
| const p2 = renderMermaid({ | ||
| id: 'd2', | ||
| text: 'graph TD; C-->D', | ||
| config: {theme: 'forest'}, | ||
| }); | ||
| const p3 = renderMermaid({ | ||
| id: 'd3', | ||
| text: 'graph TD; E-->F', | ||
| config: {theme: 'dark'}, | ||
| }); | ||
|
|
||
| const [r1, r2, r3] = await Promise.all([p1, p2, p3]); | ||
|
|
||
| expect(r1.svg).toBe('<svg id="d1">graph TD; A-->B</svg>'); | ||
| expect(r2.svg).toBe('<svg id="d2">graph TD; C-->D</svg>'); | ||
| expect(r3.svg).toBe('<svg id="d3">graph TD; E-->F</svg>'); | ||
|
|
||
| // Must never have more than 1 render in flight simultaneously | ||
| expect(maxConcurrentRenders).toBe(1); | ||
|
|
||
| // Strict sequential execution: | ||
| // initialize -> render start -> render end -> initialize next... | ||
| expect(executionTrace).toEqual([ | ||
| 'initialize:default', | ||
| 'render:start:d1', | ||
| 'render:end:d1', | ||
| 'initialize:forest', | ||
| 'render:start:d2', | ||
| 'render:end:d2', | ||
| 'initialize:dark', | ||
| 'render:start:d3', | ||
| 'render:end:d3', | ||
| ]); | ||
| }); | ||
|
|
||
| it('continue executing queued renders even if an earlier render throws', async () => { | ||
| const mockMermaid = { | ||
| initialize: vi.fn(), | ||
| render: vi.fn(async (id: string, text: string): Promise<RenderResult> => { | ||
| if (id === 'failing-diagram') { | ||
| throw new Error('Syntax error in diagram'); | ||
| } | ||
| return { | ||
| svg: `<svg id="${id}">${text}</svg>`, | ||
| diagramType: 'flowchart', | ||
| }; | ||
| }), | ||
| }; | ||
|
|
||
| vi.spyOn(loadMermaidModule, 'loadMermaid').mockResolvedValue( | ||
| mockMermaid as unknown as typeof loadMermaidModule.loadMermaid extends () => Promise< | ||
| infer M | ||
| > | ||
| ? M | ||
| : never, | ||
| ); | ||
|
|
||
| const p1 = renderMermaid({ | ||
| id: 'failing-diagram', | ||
| text: 'syntax error', | ||
| config: {theme: 'default'}, | ||
| }); | ||
| const p2 = renderMermaid({ | ||
| id: 'succeeding-diagram', | ||
| text: 'graph TD; A-->B', | ||
| config: {theme: 'default'}, | ||
| }); | ||
|
|
||
| await expect(p1).rejects.toThrow('Syntax error in diagram'); | ||
| const r2 = await p2; | ||
| expect(r2.svg).toBe('<svg id="succeeding-diagram">graph TD; A-->B</svg>'); | ||
| }); | ||
|
|
||
| it('clean up error container from document on error if present', async () => { | ||
| const removeFn = vi.fn(); | ||
| const querySelectorSpy = vi.fn().mockReturnValue({remove: removeFn}); | ||
| vi.stubGlobal('document', {querySelector: querySelectorSpy}); | ||
|
|
||
| const mockMermaid = { | ||
| initialize: vi.fn(), | ||
| render: vi.fn(async () => { | ||
| throw new Error('Render failed'); | ||
| }), | ||
| }; | ||
|
|
||
| vi.spyOn(loadMermaidModule, 'loadMermaid').mockResolvedValue( | ||
| mockMermaid as unknown as typeof loadMermaidModule.loadMermaid extends () => Promise< | ||
| infer M | ||
| > | ||
| ? M | ||
| : never, | ||
| ); | ||
|
|
||
| await expect( | ||
| renderMermaid({ | ||
| id: 'error-id', | ||
| text: 'invalid', | ||
| config: {theme: 'default'}, | ||
| }), | ||
| ).rejects.toThrow('Render failed'); | ||
|
|
||
| expect(querySelectorSpy).toHaveBeenCalledWith('#derror-id'); | ||
| expect(removeFn).toHaveBeenCalled(); | ||
|
|
||
| vi.unstubAllGlobals(); | ||
| }); | ||
| }); |
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
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.
Script executed:
Repository: meta/docusaurus
Length of output: 5972
Script executed:
Repository: meta/docusaurus
Length of output: 30836
Web query:
MDN document.querySelector SyntaxError invalid CSS selector pseudo class colon ID CSS.escapeResult:
Web query:
Mermaid 12.0.0 mermaidAPI render source removeExistingElements select id divId d3 mermaidAPI.tsResult:
Script executed:
Repository: meta/docusaurus
Length of output: 5775
Script executed:
Repository: meta/docusaurus
Length of output: 19057
Web query:
React 19.3 useId output client id format React 19.3.0 useId _r_ characters sourceResult:
Script executed:
Repository: meta/docusaurus
Length of output: 16437
Web query:
d3-selection selection.select source querySelector selector.js githubResult:
Web query:
MDN Document getElementById exact ID no CSS selector returns element documentResult:
Script executed:
Repository: meta/docusaurus
Length of output: 2464
Script executed:
Repository: meta/docusaurus
Length of output: 1602
Use an exact ID lookup for error cleanup.
If a caller supplies
:r0:-0, Mermaid can leave a temporary container after rejecting. The cleanup selector then throws anotherSyntaxErrorand leaves that container in the document. UsegetElementByIdfor cleanup and update the cleanup test.