开玩笑的错误与不是诺言的功能

时间:2020-05-14 10:05:57

标签: javascript jestjs

我正在尝试为诺言编写开玩笑测试。

calculateSquare()返回一个承诺。

使用console.log在脚本中对此进行测试,此功能可以按预期解决和拒绝操作。

square.js

const calculateSquare = (number) => {
    const promise = new Promise((resolve, reject) => {
        if (typeof number !== "number") {
            return reject(new Error("NaN"));
        }
        const square = number * number;
        resolve(square);
    });
    return promise;
};

// testing locally
calculateSquare(3).then((value) => { console.log(value) });
calculateSquare("3").catch((err) => { console.log(err.message) });

exports = { calculateSquare }

但是当我尝试运行下面的笑话测试时:

square.test.js

const { calculateSquare } = require("./square");

describe("test calculateSquare function", () => {
    test("2 squared is 4", () => {
        return expect(calculateSquare(2)).resolves.toEqual(4);
    });

    test("'2' throws exception", () => {
        return expect(calculateSquare("2")).rejects.toMatch("NaN");
    });
});

我的测试失败,并显示以下错误:

TypeError:calculateSquare不是函数

您能否帮助我理解为什么它不是“函数”?编写这些测试的正确方法是什么?

提前感谢您的帮助

1 个答案:

答案 0 :(得分:1)

看来您需要在Input: N = 6, M = 7, K = 3 the subset of K nodes: 3 4 6 the permutation: 2 3 4 6 5 the edges: (1, 2), (1, 5), (2, 3), (2, 4), (2, 6), (3, 4), (5, 6) Output: 4 If we "remove" nodes 2, 3, 4, 6, there is no path between 1 and 3, 4, 6

中执行以下操作
square.js