首页 > 解决方案 > Using Jest to test scripts affecting the DOM

问题描述

I have a script that translated markdown to html on a document and I'm trying to test the html rending. Here's what I have so far.

<html>
  <head>
    <script
      type="text/javascript"
      src="https://rawcdn.githack.com/oscarmorrison/md-page/master/md-page.js"
    ></script>
  </head>
  <body>
    <noscript># hello</noscript>
  </body>
</html>

Here's my Jest file.

describe("HTML Rendering Tests: Ensures that syntax is converted successfully.", () => {
  beforeAll(() => {
    const s = document.createElement("script");
    s.type = "text/javascript";
    s.src = "https://rawcdn.githack.com/oscarmorrison/md-page/master/md-page.js";
    document.head.appendChild(s);
  });

  test('it should load scripts', () => {
    document.body.innerHTML = '<noscript># hello';
    expect(document.body).toContain('Hello');
  });
});

I've printed everything inside it doesn't seem to be translating the text to markdown. Any thoughts?

<head><script type="text/javascript" src="https://rawcdn.githack.com/oscarmorrison/md-page/master/md-page.js"></script></head><body><noscript># hello</noscript></body>

标签: javascripthtmltestingdomjestjs

解决方案


推荐阅读