vendredi 6 décembre 2019

Incomplete stubs with Jest & TypeScript

I'm trying to stub out a function for testing using Jest. Since I'm using TypeScript the compiler is bothering me about making sure that my stub returns all the object fields. I don't need all the object fields, only two of them. To make matters worse, this object it wants me to return is fairly complicated.

What I have

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,
            });
        });
    });
});

What I want to do

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,
            });
        });
    });
});

The function being tests

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()
    };
}

The function only uses the first two properties so it feels like a waste of effort to try and re-create the entire object. The Stripe.subscriptions.ISubscription interface has a lot of nesting and complication in it that I'd really like to avoid in my tests.

Aucun commentaire:

Enregistrer un commentaire