代码之家  ›  专栏  ›  技术社区  ›  Steve

如何使用mocha/chai测试引用html文件

  •  0
  • Steve  · 技术社区  · 6 年前

    我有一个index.html文件:

    <!doctype html>
    <html>
        <head>
            <style>
                #container {width: 600px; max-width: 100%; margin: 1em auto;}
            </style>
        </head>
        <body>
            <div id="container">
                <form>
                    <input type="text" id="text" placeholder="enter some text" required maxlength="10" pattern="^[a-z,A-Z]{1,10}$"><br />
                    <input type="text" id="number" placeholder="enter a number" required maxlength="10" pattern="\d{10}"><br />
                    <input type="submit">
                </form>
            </div>
        </body>
    </html>
    

    我想用表格上的chai断言做一个摩卡测试。

    当我测试外部 .js 文件,我可以用 require 声明拉进来 JS 文件。

    如何引用/导入 .html 在窗体上运行测试的文件?

    我用谷歌搜索找不到这样的信息。

    感谢帮助。

    编辑 :我是否在 <script> 里面 html 文件?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Steve    6 年前

    我想我 找到了答案。

    我可以在html文件中使用以下内容。

    <div id="mocha"></div>
    <script src="node_modules/mocha/mocha.js"></script>
    <script src="node_modules/chai/chai.js"></script>
    <script>mocha.setup('bdd')</script>
    
    <!-- load code you want to test here -->
    
    <!-- load your test files here -->
    
    <script>
      mocha.run();
    </script>
    

    this page .