Skip to content

Commit

Permalink
Fix breaking change in sinon fake timers
Browse files Browse the repository at this point in the history
  • Loading branch information
hdiniz committed Dec 12, 2024
1 parent d066667 commit 1e49881
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 26 deletions.
4 changes: 2 additions & 2 deletions test/cron/monthly/host-settlement.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ describe('cron/monthly/host-settlement', () => {
);

// Refund contributions that must be
let clock = sinon.useFakeTimers(moment(lastMonth).add(1, 'day').toDate());
let clock = sinon.useFakeTimers({ shouldAdvanceTime: true, now: moment(lastMonth).add(1, 'day').toDate() });
await createRefundTransaction(unsettledRefundedContribution, 0, null, user, fakeUUID('00000008'));
await createRefundTransaction(settledRefundedContribution, 0, null, user, fakeUUID('00000009'));
clock.restore();
Expand All @@ -225,7 +225,7 @@ describe('cron/monthly/host-settlement', () => {
});

// Create Contributions
clock = useFakeTimers(lastMonth.toDate()); // Manually setting today's date
clock = useFakeTimers({ now: lastMonth.toDate(), shouldAdvanceTime: true }); // Manually setting today's date
const order = await fakeOrder({
description: 'EUR Contribution with tip + host fee',
CollectiveId: eurCollective.id,
Expand Down
2 changes: 1 addition & 1 deletion test/server/graphql/common/transactions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('server/graphql/common/transactions', () => {
kind: TransactionKind.CONTRIBUTION,
OrderId: order.id,
});
timer = useFakeTimers(new Date('2020-07-23 0:0').getTime());
timer = useFakeTimers({ now: new Date('2020-07-23 0:0').getTime(), shouldAdvanceTime: true });
oldTransaction = await fakeTransaction({
CollectiveId: collective.id,
FromCollectiveId: contributor.CollectiveId,
Expand Down
2 changes: 1 addition & 1 deletion test/server/graphql/v1/invoices.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import * as utils from '../../../utils';
* The payment method is always stripe for now.
*/
async function donate(user, currency, amount, createdAt, collective) {
const timer = useFakeTimers(new Date(createdAt).getTime());
const timer = useFakeTimers({ shouldAdvanceTime: true, now: new Date(createdAt).getTime() });
try {
await store.stripeConnectedAccount(collective.HostCollectiveId);
await store.stripeOneTimeDonation({
Expand Down
14 changes: 7 additions & 7 deletions test/server/graphql/v2/mutation/OrderMutations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2840,7 +2840,7 @@ describe('server/graphql/v2/mutation/OrderMutations', () => {
describe('when charge is due today', () => {
it('from monthly to yearly', async () => {
const today = moment(new Date(2022, 0, 1)); // 1st of January 2022
clock = useFakeTimers(today.toDate()); // Manually setting today's date
clock = useFakeTimers({ now: today.toDate(), shouldAdvanceTime: true }); // Manually setting today's date
const { result, order } = await updateFakeOrder(today, 'month', 'year');
expect(result.errors).to.not.exist;
expect(result.data.updateOrder.tier.name).to.eq(fixedYearlyTier.name);
Expand All @@ -2852,7 +2852,7 @@ describe('server/graphql/v2/mutation/OrderMutations', () => {

it('from yearly to monthly', async () => {
const today = moment(new Date(2022, 0, 1)); // 1st of January 2022
clock = useFakeTimers(today.toDate()); // Manually setting today's date
clock = useFakeTimers({ now: today.toDate(), shouldAdvanceTime: true }); // Manually setting today's date
const { result, order } = await updateFakeOrder(today, 'year', 'month');
expect(result.errors).to.not.exist;
expect(result.data.updateOrder.tier.name).to.eq(fixedMonthlyTier.name);
Expand All @@ -2866,7 +2866,7 @@ describe('server/graphql/v2/mutation/OrderMutations', () => {
describe('with a recent past charge (keep the existing date)', () => {
it('from monthly to yearly', async () => {
const today = moment(new Date(2022, 0, 12)); // 12th of January 2022
clock = useFakeTimers(today.toDate()); // Manually setting today's date
clock = useFakeTimers({ now: today.toDate(), shouldAdvanceTime: true }); // Manually setting today's date
const firstOfJanuary = moment(new Date(2022, 0, 1)); // 1st of January 2022
const { result, order } = await updateFakeOrder(firstOfJanuary, 'month', 'year');
expect(result.errors).to.not.exist;
Expand All @@ -2879,7 +2879,7 @@ describe('server/graphql/v2/mutation/OrderMutations', () => {

it('from yearly to monthly', async () => {
const today = moment(new Date(2022, 0, 12)); // 12th of January 2022
clock = useFakeTimers(today.toDate()); // Manually setting today's date
clock = useFakeTimers({ now: today.toDate(), shouldAdvanceTime: true }); // Manually setting today's date
const firstOfJanuary = moment(new Date(2022, 0, 1)); // 1st of January 2022
const { result, order } = await updateFakeOrder(firstOfJanuary, 'year', 'month');
expect(result.errors).to.not.exist;
Expand All @@ -2894,7 +2894,7 @@ describe('server/graphql/v2/mutation/OrderMutations', () => {
describe('with an old dur charge', () => {
it('from monthly to yearly', async () => {
const today = moment(new Date(2022, 6, 1)); // 1st of July 2022
clock = useFakeTimers(today.toDate()); // Manually setting today's date
clock = useFakeTimers({ now: today.toDate(), shouldAdvanceTime: true }); // Manually setting today's date
const firstOfJanuary = moment(new Date(2022, 0, 1)); // 1st of January 2022
const { result, order } = await updateFakeOrder(firstOfJanuary, 'month', 'year');
expect(result.errors).to.not.exist;
Expand All @@ -2907,7 +2907,7 @@ describe('server/graphql/v2/mutation/OrderMutations', () => {

it('from yearly to monthly', async () => {
const today = moment(new Date(2022, 6, 12)); // 12th of July 2022
clock = useFakeTimers(today.toDate()); // Manually setting today's date
clock = useFakeTimers({ now: today.toDate(), shouldAdvanceTime: true }); // Manually setting today's date
const firstOfJanuary = moment(new Date(2022, 0, 1)); // 1st of January 2022
const { result, order } = await updateFakeOrder(firstOfJanuary, 'year', 'month');
expect(result.errors).to.not.exist;
Expand All @@ -2920,7 +2920,7 @@ describe('server/graphql/v2/mutation/OrderMutations', () => {

it('from yearly to monthly (on the 1st of the month)', async () => {
const today = moment(new Date(2022, 6, 1)); // 1st of July 2022
clock = useFakeTimers(today.toDate()); // Manually setting today's date
clock = useFakeTimers({ now: today.toDate(), shouldAdvanceTime: true }); // Manually setting today's date
const firstOfJanuary = moment(new Date(2022, 0, 1)); // 1st of January 2022
const { result, order } = await updateFakeOrder(firstOfJanuary, 'year', 'month');
expect(result.errors).to.not.exist;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ describe('server/graphql/v2/mutation/UploadedFileMutations', () => {
const hourlyLimit = config.limits.klippa.perUser.hour;

// Mock the clock to make sure we don't trigger the hourly limit
clock = sinon.useFakeTimers(moment().subtract(20, 'hours').toDate());
clock = sinon.useFakeTimers({ now: moment().subtract(20, 'hours').toDate(), shouldAdvanceTime: true });
const validRequests = [];
for (let i = 0; i < dailyLimit; i++) {
const result = await graphqlQueryV2(uploadFileMutation, args, user);
Expand Down
4 changes: 2 additions & 2 deletions test/server/graphql/v2/query/HostMetricsQuery.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('server/graphql/v2/query/HostMetricsQuery', () => {
collective1 = await fakeCollective({ admin: collectiveAdminUser, HostCollectiveId: host.id, hostFeePercent: 30 });
collective2 = await fakeCollective({ admin: collectiveAdminUser, HostCollectiveId: host.id, hostFeePercent: 30 });

let clock = useFakeTimers(new Date('2021-02-01 0:0').getTime());
let clock = useFakeTimers({ now: new Date('2021-02-01 0:0').getTime(), shouldAdvanceTime: true });
try {
const order1 = await fakeOrder({
CollectiveId: collective1.id,
Expand All @@ -72,7 +72,7 @@ describe('server/graphql/v2/query/HostMetricsQuery', () => {
clock.restore();
}

clock = useFakeTimers(new Date('2021-06-01 0:0').getTime());
clock = useFakeTimers({ now: new Date('2021-06-01 0:0').getTime(), shouldAdvanceTime: true });
try {
const order2 = await fakeOrder({
CollectiveId: collective2.id,
Expand Down
5 changes: 4 additions & 1 deletion test/server/lib/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import * as auth from '../../../server/lib/auth';
describe('server/lib/auth', () => {
it('should generate valid tokens', () => {
// Given that time `Date.now` returns zero (0)
const clock = useFakeTimers();
const clock = useFakeTimers({
now: 0,
shouldAdvanceTime: true,
});

// When the token is generated
const token = auth.createJwt('subject', { foo: 'bar' }, 5);
Expand Down
2 changes: 1 addition & 1 deletion test/server/lib/recurring-contributions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('server/lib/recurring-contributions', () => {
};

// And given that we freeze time
const clock = useFakeTimers(new Date('2018-01-28 0:0').getTime());
const clock = useFakeTimers({ now: new Date('2018-01-28 0:0').getTime(), shouldAdvanceTime: true });

// When dates are updated with failure
const updatedDates = getNextChargeAndPeriodStartDates('failure', order);
Expand Down
2 changes: 1 addition & 1 deletion test/server/lib/spam.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('server/lib/spam', () => {
let clock;

before(() => {
clock = useFakeTimers(new Date('2020-01-01T00:00:00.000Z'));
clock = useFakeTimers({ now: new Date('2020-01-01T00:00:00.000Z'), shouldAdvanceTime: true });
});

after(() => {
Expand Down
6 changes: 3 additions & 3 deletions test/server/lib/subscriptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('server/lib/subscriptions', () => {
describe('with an order that has a pending payment', () => {
it('to what it was before, keep the past due date', async () => {
const today = moment(new Date(2022, 0, 1)); // 1st of January 2022
clock = sinon.useFakeTimers(today.toDate()); // Manually setting today's date
clock = sinon.useFakeTimers({ now: today.toDate(), shouldAdvanceTime: true }); // Manually setting today's date
const paypalPm = await fakePaymentMethod({
service: PAYMENT_METHOD_SERVICE.PAYPAL,
type: PAYMENT_METHOD_TYPE.SUBSCRIPTION,
Expand Down Expand Up @@ -71,7 +71,7 @@ describe('server/lib/subscriptions', () => {
describe('with an order that has a future payment', () => {
it('before the 15th of the month => 1st of next month', async () => {
const today = moment(new Date(2022, 0, 1)); // 1st of January 2022
clock = sinon.useFakeTimers(today.toDate()); // Manually setting today's date
clock = sinon.useFakeTimers({ now: today.toDate(), shouldAdvanceTime: true }); // Manually setting today's date
const paypalPm = await fakePaymentMethod({
service: PAYMENT_METHOD_SERVICE.PAYPAL,
type: PAYMENT_METHOD_TYPE.SUBSCRIPTION,
Expand Down Expand Up @@ -103,7 +103,7 @@ describe('server/lib/subscriptions', () => {

it('after the 15th of the month => skip next month', async () => {
const today = moment(new Date(2022, 0, 18)); // 18th of January 2022
clock = sinon.useFakeTimers(today.toDate()); // Manually setting today's date
clock = sinon.useFakeTimers({ now: today.toDate(), shouldAdvanceTime: true }); // Manually setting today's date
const paypalPm = await fakePaymentMethod({
service: PAYMENT_METHOD_SERVICE.PAYPAL,
type: PAYMENT_METHOD_TYPE.SUBSCRIPTION,
Expand Down
2 changes: 1 addition & 1 deletion test/server/models/PaymentMethod.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('server/models/PaymentMethod', () => {
.query({ access_key: config.fixer.accessKey, base: 'EUR', symbols: 'USD' }) // eslint-disable-line camelcase
.reply(200, { base: 'EUR', date: '2017-09-01', rates: { USD: 1.192 } });
await utils.resetTestDB();
timer = useFakeTimers(new Date('2017-09-01 00:00:00').getTime());
timer = useFakeTimers({ now: new Date('2017-09-01 00:00:00').getTime(), shouldAdvanceTime: true });
});
after(() => {
timer.restore();
Expand Down
2 changes: 1 addition & 1 deletion test/server/routes/oauth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('server/routes/oauth', () => {

it('goes through the entire OAuth flow', async () => {
const fakeNow = new Date(2022, 0, 1);
clock = useFakeTimers(fakeNow);
clock = useFakeTimers({ now: fakeNow, shouldAdvanceTime: true });
const application = await fakeApplication();

// Get authorization code
Expand Down
2 changes: 1 addition & 1 deletion test/stores/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ export async function stripeOneTimeDonation(opt) {
// requested creation date. It will be reset right after the
// execution of the order.
if (createdAt) {
sandbox.useFakeTimers(new Date(createdAt).getTime());
sandbox.useFakeTimers({ now: new Date(createdAt).getTime(), shouldAdvanceTime: true });
}

// Stub the stripe calls before executing the order.
Expand Down
6 changes: 3 additions & 3 deletions test/stories/platform-migration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ describe('platform constants', () => {
});

it('platform collective id is same before migration cutoff', () => {
sandbox.useFakeTimers(moment('2024-10-01T00:00:00Z').toDate());
sandbox.useFakeTimers({ now: moment('2024-10-01T00:00:00Z').toDate(), shouldAdvanceTime: true });
expect(PlatformConstants.PlatformCollectiveId).to.equal(8686);
});

it('platform collective id is migrated after migration cutoff', () => {
sandbox.useFakeTimers(moment('2024-10-01T00:00:01Z').toDate());
sandbox.useFakeTimers({ now: moment('2024-10-01T00:00:01Z').toDate(), shouldAdvanceTime: true });
expect(PlatformConstants.PlatformCollectiveId).to.equal(845576);
});
});
Expand Down Expand Up @@ -253,7 +253,7 @@ describe('platform transactions', () => {
},
].map(tc =>
('only' in tc && tc.only ? it.only : it)(tc.title, async () => {
sandbox.useFakeTimers(tc.when);
sandbox.useFakeTimers({ now: tc.when, shouldAdvanceTime: true });

const result = await makeContribution(
{
Expand Down

0 comments on commit 1e49881

Please sign in to comment.