如何运行mocha脚本通过浏览器测试url(不使用XMLHttpRequest)

时间:2017-04-27 06:12:39

标签: node.js mocha

您好:尽管对nodejs和mocha不熟悉并寻求您的帮助。提前谢谢。

我试图通过浏览器(而不是终端)运行一个简单的mocha测试来访问URL并获得响应状态。

使用XMLHttpRequest它有效。但是,我想使用节点库(比如request.js)。为了从浏览器测试mocha脚本,我无法找到如何正确加载request.js。请帮忙。

test.js(使用XMLHttpRequest)

describe('suite', function(){

    it('case using XMLHTTPREQUEST', function(done){        
        var url = "http://oss.sheetjs.com/js-xlsx/test_files/formula_stress_test_ajax.xlsx";
        var xhr = new XMLHttpRequest();
        xhr.open("GET", url, true);
        xhr.onload = function (e) {
            console.log("TEST RESPONSE STATUS IS: " + xhr.status);
        }
        xhr.send();
        done();
    });

    it('case using AXIOS', function(done){        
        axios.get('http://www.google.com').then(
            function(response){
                    console.log(response.status);
                    done();
            });
    });
});

testrunner.html

<!DOCTYPE html>
<html>
<head>
  <title>Mocha Tests</title>
  <link rel="stylesheet" href="../node_modules/mocha/mocha.css">
</head>

<body>
  <div id="mocha"></div>
  <script src="../node_modules/axios/dist/axios.js"></script>
  <script src="../node_modules/mocha/mocha.js"></script>
  <script>mocha.setup('bdd')</script>
  <script src="./test.js"></script>
  <script>
    mocha.run()
  </script>
</body>    
</html>

0 个答案:

没有答案
相关问题