fix(mermaid): error if lazyLoadedDiagrams fails

Throw an error if lazyLoadedDiagrams fails to load properly.

Rendering is still performed, even on a lazyLoadedDiagrams failure.
This commit is contained in:
Alois Klink
2022-10-20 11:10:58 +01:00
parent 895a5eb78a
commit 3a2669e634
2 changed files with 31 additions and 3 deletions

View File

@@ -48,11 +48,32 @@ describe('when using mermaid and ', function () {
const node = document.createElement('div'); const node = document.createElement('div');
node.appendChild(document.createTextNode('graph TD;\na;')); node.appendChild(document.createTextNode('graph TD;\na;'));
mermaid.initThrowsErrors(undefined, node); await mermaid.initThrowsErrors(undefined, node);
// mermaidAPI.render function has been mocked, since it doesn't yet work // mermaidAPI.render function has been mocked, since it doesn't yet work
// in Node.JS (only works in browser) // in Node.JS (only works in browser)
expect(mermaidAPI.render).toHaveBeenCalled(); expect(mermaidAPI.render).toHaveBeenCalled();
}); });
it('should throw error (but still render) if lazyLoadedDiagram fails', async () => {
const node = document.createElement('div');
node.appendChild(document.createTextNode('graph TD;\na;'));
mermaidAPI.setConfig({
lazyLoadedDiagrams: ['this-file-does-not-exist.mjs'],
});
await expect(mermaid.initThrowsErrors(undefined, node)).rejects.toThrowError(
// this error message is probably different on every platform
// this one is just for vite-note (node/jest/browser may be different)
'Failed to load this-file-does-not-exist.mjs'
);
// should still render, even if lazyLoadedDiagrams fails
expect(mermaidAPI.render).toHaveBeenCalled();
});
afterEach(() => {
// we modify mermaid config in some tests, so we need to make sure to reset them
mermaidAPI.reset();
});
}); });
describe('checking validity of input ', function () { describe('checking validity of input ', function () {

View File

@@ -84,14 +84,22 @@ const initThrowsErrors = async function (
mermaid.sequenceConfig = config; mermaid.sequenceConfig = config;
} }
const errors = [];
if (conf?.lazyLoadedDiagrams && conf.lazyLoadedDiagrams.length > 0) { if (conf?.lazyLoadedDiagrams && conf.lazyLoadedDiagrams.length > 0) {
// Load all lazy loaded diagrams in parallel // Load all lazy loaded diagrams in parallel
await Promise.allSettled( const results = await Promise.allSettled(
conf.lazyLoadedDiagrams.map(async (diagram: string) => { conf.lazyLoadedDiagrams.map(async (diagram: string) => {
const { id, detector, loadDiagram } = await import(diagram); const { id, detector, loadDiagram } = await import(diagram);
addDetector(id, detector, loadDiagram); addDetector(id, detector, loadDiagram);
}) })
); );
for (const result of results) {
if (result.status == 'rejected') {
log.warn(`Failed to lazyLoadedDiagram due to `, result.reason);
errors.push(result.reason);
}
}
} }
// if last argument is a function this is the callback function // if last argument is a function this is the callback function
@@ -119,7 +127,6 @@ const initThrowsErrors = async function (
const idGenerator = new utils.initIdGenerator(conf.deterministicIds, conf.deterministicIDSeed); const idGenerator = new utils.initIdGenerator(conf.deterministicIds, conf.deterministicIDSeed);
let txt: string; let txt: string;
const errors = [];
// element is the current div with mermaid class // element is the current div with mermaid class
for (const element of Array.from(nodesToProcess)) { for (const element of Array.from(nodesToProcess)) {