Skip to content
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

Doesn’t read commonmark when included in unit testing #253

Open
dixy-xavier opened this issue Apr 1, 2022 · 1 comment
Open

Doesn’t read commonmark when included in unit testing #253

dixy-xavier opened this issue Apr 1, 2022 · 1 comment

Comments

@dixy-xavier
Copy link

I create a util function that uses commonmark to convert markdown to HTML. And then I tried to write a test case for that function, it throws error saying that commonmark is not defined.

Here is my code:

markdown.test.js

const {markdownToHtml} = require('../../services/util/markdown');
const assert = require('assert');

describe('Commonmark plugin', () => {
  it('normal text returns HTML string', () => {
    assert.strictEqual(markdownToHtml('Hello world').toBe('<p>Hello world</p>\n'));
  });
});

markdown.js

const markdownToHtml = (text) => {
  const reader = new commonmark.Parser();
  const writer = new commonmark.HtmlRenderer({safe: true});

  const parsed = reader.parse(text); // parsed is a 'Node' tree
  // transform parsed if you like...
  const result = writer.render(parsed); // result is a String
  return result;
};

module.exports = {markdownToHtml};

I get the following error when I run the test:
ReferenceError: commonmark is not defined

@jgm
Copy link
Member

jgm commented Apr 1, 2022

You'll need something like var commonmark = require('commonmark').

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants