簡體   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