繁体   English   中英

如何在节点repl中测试javascript(没有浏览器)?

[英]How to test javascript in node repl (no browser)?

我有2个文件mycode.jsmycode.test.js 我想在节点控制台(repl)中测试它 - (没有浏览器),以业力风格。

mycode.test.js

var expect = require('expect');
var mycode = require("./mycode");

describe("mycode", () => {
    it("should properly run tests", () => {
        expect(1).toBe(1);
    });
});

实现这一目标的最简单的设置是什么?

我不知道如何在节点repl中完成这项工作,因为对require()的调用假定您在加载要测试的文件之前单独加载该函数。 我在repl中看到测试代码的唯一方法是让你用来测试的函数包含在加载的文件中。 例如,要测试函数覆盖是如何工作的,您可以创建function_override.js然后将其加载到repl中 -

$ .load path/to/file/function_override.js

- 并且有一个assert()函数,用于测试该文件中其他函数的输出。 例如:

            function assert(value, text) {
              if (value === true) {
                  console.log(text);
              } else {
                  console.log ("that is false");
              }
            }

            var fun = 3;

            function FuncType1(){
              assert(typeof fun === "function", "We access the function");
            }

            function FuncType2(){
              var fun = 3;
              assert(typeof fun === "number", "Now we access the number");
            }

            function FuncType3(){
              assert(typeof fun === "number", "Still a number");
            }

            function fun(){};

            FuncType1(); // returns "We access the function"
            FuncType2(); // returns "Now we access the number"
            FuncType3(); // returns  "that is false"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM