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

时间:2018-11-20 02:01:22

标签: mocha chai

我有一个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 asserts进行摩卡测试。

当我测试外部.js文件时,可以使用require语句提取该.js文件。

如何引用/导入.html文件以在表单上运行测试?

我无法使用Google搜索找到此类信息。

帮助表示赞赏。

编辑:我是否在<script>文件内的.html中运行测试?

1 个答案:

答案 0 :(得分:1)

我想我已经找到了答案。

我可以在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