Skip to content

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

@dixy-xavier

Description

@dixy-xavier

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions