Skip to content

Commit

Permalink
test: fix initThrowsErrors test with esbuild
Browse files Browse the repository at this point in the history
Jest mocks don't work well with ESM (especially since esbuild
is stricter with ESM code than jest is).

We can convert this test to instead import the mermaid modules
using `require('./mermaid')`, so that jest can easily mock it.
  • Loading branch information
aloisklink committed Sep 6, 2022
1 parent a0fa8df commit 6aeef3d
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/mermaid.spec.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import mermaid from './mermaid';
import { mermaidAPI } from './mermaidAPI';
import flowDb from './diagrams/flowchart/flowDb';
// mocks the mermaidAPI.render function (see `./__mocks__/mermaidAPI`)
jest.mock('./mermaidAPI');
// jest.mock only works well with CJS, see https://github.com/facebook/jest/issues/9430
const { default: mermaid } = require('./mermaid');
const { mermaidAPI } = require('./mermaidAPI');
const { default: flowDb } = require('./diagrams/flowchart/flowDb');

import flowParser from './diagrams/flowchart/parser/flow';
import flowRenderer from './diagrams/flowchart/flowRenderer';
import Diagram from './Diagram';

const spyOn = jest.spyOn;

// mocks the mermaidAPI.render function (see `./__mocks__/mermaidAPI`)
jest.mock('./mermaidAPI');

afterEach(() => {
jest.restoreAllMocks();
});
Expand Down Expand Up @@ -54,6 +55,8 @@ describe('when using mermaid and ', function () {
node.appendChild(document.createTextNode('graph TD;\na;'));

mermaid.initThrowsErrors(undefined, node);
// mermaidAPI.render function has been mocked, since it doesn't yet work
// in Node.JS (only works in browser)
expect(mermaidAPI.render).toHaveBeenCalled();
});
});
Expand Down

0 comments on commit 6aeef3d

Please sign in to comment.