Jest 和 TypeScript 的不完整存根
Incomplete stubs with Jest & TypeScript
我正在尝试使用 Jest 存根测试函数。由于我使用的是 TypeScript,因此编译器一直困扰着我,以确保我的存根 return 包含所有对象字段。我不需要所有的对象字段,只需要其中两个。更糟糕的是,它要我 return 的这个对象相当复杂。
我有什么
import * as sinon from "sinon";
import * as stripeHelpers from "../../src/lib/stripe";
describe("lib/authorization", () => {
let sandbox: sinon.SinonSandbox;
beforeAll(() => {
sandbox = sinon.createSandbox();
});
afterEach(() => {
sandbox.restore();
});
describe("getAuthorizationDateRange", () => {
test("returns an authorization date range", async () => {
const getUserSubscriptionStub = sandbox.stub(stripeHelpers, "getUserSubscription").resolves({
current_period_end: 123123,
current_period_start: 123123,
object: "subscription",
application_fee_percent: undefined,
billing: "charge_automatically",
collection_method: "charge_automatically",
billing_cycle_anchor: 0,
billing_thresholds: undefined,
cancel_at: undefined,
cancel_at_period_end: true,
canceled_at: undefined,
created: 0,
customer: "test",
days_until_due: undefined,
default_payment_method: "test",
default_source: "test",
default_tax_rates: [],
discount: undefined,
ended_at: undefined,
items,
latest_invoice: "test",
livemode: true,
metadata: {},
start: 0,
start_date: 0,
status: "active",
tax_percent: undefined,
trial_end: undefined,
trial_start: undefined,
});
});
});
});
我想做什么
import * as sinon from "sinon";
import * as stripeHelpers from "../../src/lib/stripe";
describe("lib/authorization", () => {
let sandbox: sinon.SinonSandbox;
beforeAll(() => {
sandbox = sinon.createSandbox();
});
afterEach(() => {
sandbox.restore();
});
describe("getAuthorizationDateRange", () => {
test("returns an authorization date range", async () => {
const getUserSubscriptionStub = sandbox.stub(stripeHelpers, "getUserSubscription").resolves({
current_period_end: 123123,
current_period_start: 123123,
});
});
});
});
正在测试的功能
export async function getAuthorizationDateRange(user: User): Promise<DateRange> {
const subscription: Stripe.subscriptions.ISubscription = await stripeHelpers.getUserSubscription(user);
return {
start: moment.unix(subscription.current_period_start).toDate(),
end: moment.unix(subscription.current_period_end).toDate()
};
}
该函数仅使用前两个属性,因此尝试重新创建 整个 对象感觉是在浪费精力。 Stripe.subscriptions.ISubscription
界面有很多嵌套和复杂性,我真的很想在我的测试中避免这些。
事实证明,您只需要将对象转换为显式 any
。
const getUserSubscriptionStub = sandbox.stub(stripeHelpers, "getUserSubscription").resolves({
current_period_end: 123123,
current_period_start: 123123,
} as any);
我正在尝试使用 Jest 存根测试函数。由于我使用的是 TypeScript,因此编译器一直困扰着我,以确保我的存根 return 包含所有对象字段。我不需要所有的对象字段,只需要其中两个。更糟糕的是,它要我 return 的这个对象相当复杂。
我有什么
import * as sinon from "sinon";
import * as stripeHelpers from "../../src/lib/stripe";
describe("lib/authorization", () => {
let sandbox: sinon.SinonSandbox;
beforeAll(() => {
sandbox = sinon.createSandbox();
});
afterEach(() => {
sandbox.restore();
});
describe("getAuthorizationDateRange", () => {
test("returns an authorization date range", async () => {
const getUserSubscriptionStub = sandbox.stub(stripeHelpers, "getUserSubscription").resolves({
current_period_end: 123123,
current_period_start: 123123,
object: "subscription",
application_fee_percent: undefined,
billing: "charge_automatically",
collection_method: "charge_automatically",
billing_cycle_anchor: 0,
billing_thresholds: undefined,
cancel_at: undefined,
cancel_at_period_end: true,
canceled_at: undefined,
created: 0,
customer: "test",
days_until_due: undefined,
default_payment_method: "test",
default_source: "test",
default_tax_rates: [],
discount: undefined,
ended_at: undefined,
items,
latest_invoice: "test",
livemode: true,
metadata: {},
start: 0,
start_date: 0,
status: "active",
tax_percent: undefined,
trial_end: undefined,
trial_start: undefined,
});
});
});
});
我想做什么
import * as sinon from "sinon";
import * as stripeHelpers from "../../src/lib/stripe";
describe("lib/authorization", () => {
let sandbox: sinon.SinonSandbox;
beforeAll(() => {
sandbox = sinon.createSandbox();
});
afterEach(() => {
sandbox.restore();
});
describe("getAuthorizationDateRange", () => {
test("returns an authorization date range", async () => {
const getUserSubscriptionStub = sandbox.stub(stripeHelpers, "getUserSubscription").resolves({
current_period_end: 123123,
current_period_start: 123123,
});
});
});
});
正在测试的功能
export async function getAuthorizationDateRange(user: User): Promise<DateRange> {
const subscription: Stripe.subscriptions.ISubscription = await stripeHelpers.getUserSubscription(user);
return {
start: moment.unix(subscription.current_period_start).toDate(),
end: moment.unix(subscription.current_period_end).toDate()
};
}
该函数仅使用前两个属性,因此尝试重新创建 整个 对象感觉是在浪费精力。 Stripe.subscriptions.ISubscription
界面有很多嵌套和复杂性,我真的很想在我的测试中避免这些。
事实证明,您只需要将对象转换为显式 any
。
const getUserSubscriptionStub = sandbox.stub(stripeHelpers, "getUserSubscription").resolves({
current_period_end: 123123,
current_period_start: 123123,
} as any);