javascript - 如何在 Node repl(无浏览器)中测试 javascript?

标签 javascript node.js testing karma-runner read-eval-print-loop

我有 2 个文件 mycode.jsmycode.test.js。我想在 Node 控制台 (repl) -(无浏览器)中以 karma 风格测试它。

mycode.test.js

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

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

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

最佳答案

我不知道如何在 Node 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"

关于javascript - 如何在 Node repl(无浏览器)中测试 javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43694178/

相关文章:

javascript - D3 map 缩放后如何重新缩放

javascript - appendTo - 旁边而不是下面

javascript - 启动 Node 服务器时出现错误 : `thread_id_key != 0x7777' failed. 中止(核心转储)

testing - 如何在本地测试 Google 跟踪代码管理器

javascript - 监控特定的 js 事件以及作为自动化测试的一部分执行它们所花费的时间?

javascript - 可以将事件添加到 d3 调度程序吗?

javascript - JQuery UI Datepicker 改变一天的样式

node.js - Puppeteer - 新窗口关于 :blank

javascript - 将 excel 转换为 json 数组时无法解析日期

django - 如何测试自定义 django-admin 命令