我的lib / gcd.js
function gcd(a, b) {
if (!a) return b;
if (!b) return a;
while (1) {
a%= b;
if (!a) return b;
b%= a;
if (!b) return a;
}
}
module.exports = gcd;
我的测试/ gcd.test.js
const { gcd } = require("../lib");
describe("gcd", () => {
test("Basic: gcd of 54 and 24 is 6", () => {
expect(gcd(54, 24)).toBe(6);
});
test("Large Numbers: gcd of 123456 and 9876 is 12", () => {
expect(gcd(123456, 9876)).toBe(12);
});
test("1 Negative: gcd of 54 and -24 is 6", () => {
expect(gcd(54, -24)).toBe(6);
});
test("2 Negative: gcd of -54 and -24 is 6", () => {
expect(gcd(-54, -24)).toBe(6);
});
test("gcd of 0 and 1 should throw error", () => {
expect(() => {
gcd(0, 1);
}).toThrow();
});
});
当我使用jest运行测试时出现此错误说: TypeError:gcd不是函数
我能知道出了什么问题,是否与gcd功能有关?
答案 0 :(得分:0)
应该是这样的:
const gcd = require("../lib/gcd.js");
describe("gcd", () => {
test("Basic: gcd of 54 and 24 is 6", () => {
expect(gcd(54, 24)).toBe(6);
});
test("Large Numbers: gcd of 123456 and 9876 is 12", () => {
expect(gcd(123456, 9876)).toBe(12);
});
test("1 Negative: gcd of 54 and -24 is 6", () => {
expect(gcd(54, -24)).toBe(6);
});
test("2 Negative: gcd of -54 and -24 is 6", () => {
expect(gcd(-54, -24)).toBe(6);
});
test("gcd of 0 and 1 should throw error", () => {
expect(() => {
gcd(0, 1);
}).toThrow();
});
});