Skip to content

Commit

Permalink
stub only Date.now
Browse files Browse the repository at this point in the history
  • Loading branch information
hdiniz committed Dec 12, 2024
1 parent 50183ad commit c1af332
Show file tree
Hide file tree
Showing 16 changed files with 32 additions and 32 deletions.
8 changes: 4 additions & 4 deletions test/cron/hourly/71-send-resume-subscription-emails.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('cron/hourly/71-send-resume-subscription-emails', () => {
);

// 1st reminder
clock = sandbox.useFakeTimers({ now: moment(nextReminderDate).add(1, 'day').toDate(), shouldAdvanceTime: true });
clock = sandbox.useFakeTimers({ now: moment(nextReminderDate).add(1, 'day').toDate(), toFake: ['Date'] });
updatedOrders = await runCronJob();
expect(updatedOrders).to.have.length(1);
expect(updatedOrders[0].status).to.equal(OrderStatuses.PAUSED);
Expand All @@ -125,7 +125,7 @@ describe('cron/hourly/71-send-resume-subscription-emails', () => {

// 2nd reminder
clock.restore();
clock = sandbox.useFakeTimers({ now: moment(nextReminderDate).add(1, 'day').toDate(), shouldAdvanceTime: true });
clock = sandbox.useFakeTimers({ now: moment(nextReminderDate).add(1, 'day').toDate(), toFake: ['Date'] });
updatedOrders = await runCronJob();
expect(updatedOrders).to.have.length(1);
expect(updatedOrders[0].status).to.equal(OrderStatuses.PAUSED);
Expand All @@ -148,7 +148,7 @@ describe('cron/hourly/71-send-resume-subscription-emails', () => {

// 3rd and final reminder
clock.restore();
clock = sandbox.useFakeTimers({ now: moment(nextReminderDate).add(1, 'day').toDate(), shouldAdvanceTime: true });
clock = sandbox.useFakeTimers({ now: moment(nextReminderDate).add(1, 'day').toDate(), toFake: ['Date'] });
updatedOrders = await runCronJob();
expect(updatedOrders).to.have.length(1);
expect(updatedOrders[0].data.resumeContribution.reminder).to.equal(4);
Expand All @@ -165,7 +165,7 @@ describe('cron/hourly/71-send-resume-subscription-emails', () => {

// No more reminders
clock.restore();
clock = sandbox.useFakeTimers({ now: moment(nextReminderDate).add(60, 'day').toDate(), shouldAdvanceTime: true });
clock = sandbox.useFakeTimers({ now: moment(nextReminderDate).add(60, 'day').toDate(), toFake: ['Date'] });
updatedOrders = await runCronJob();
expect(updatedOrders).to.have.length(0);
});
Expand Down
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({ shouldAdvanceTime: true, now: moment(lastMonth).add(1, 'day').toDate() });
let clock = sinon.useFakeTimers({ toFake: ['Date'], 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({ now: lastMonth.toDate(), shouldAdvanceTime: true }); // Manually setting today's date
clock = useFakeTimers({ now: lastMonth.toDate(), toFake: ['Date'] }); // 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({ now: new Date('2020-07-23 0:0').getTime(), shouldAdvanceTime: true });
timer = useFakeTimers({ now: new Date('2020-07-23 0:0').getTime(), toFake: ['Date'] });
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({ shouldAdvanceTime: true, now: new Date(createdAt).getTime() });
const timer = useFakeTimers({ toFake: ['Date'], 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({ now: today.toDate(), shouldAdvanceTime: true }); // Manually setting today's date
clock = useFakeTimers({ now: today.toDate(), toFake: ['Date'] }); // 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({ now: today.toDate(), shouldAdvanceTime: true }); // Manually setting today's date
clock = useFakeTimers({ now: today.toDate(), toFake: ['Date'] }); // 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({ now: today.toDate(), shouldAdvanceTime: true }); // Manually setting today's date
clock = useFakeTimers({ now: today.toDate(), toFake: ['Date'] }); // 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({ now: today.toDate(), shouldAdvanceTime: true }); // Manually setting today's date
clock = useFakeTimers({ now: today.toDate(), toFake: ['Date'] }); // 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({ now: today.toDate(), shouldAdvanceTime: true }); // Manually setting today's date
clock = useFakeTimers({ now: today.toDate(), toFake: ['Date'] }); // 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({ now: today.toDate(), shouldAdvanceTime: true }); // Manually setting today's date
clock = useFakeTimers({ now: today.toDate(), toFake: ['Date'] }); // 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({ now: today.toDate(), shouldAdvanceTime: true }); // Manually setting today's date
clock = useFakeTimers({ now: today.toDate(), toFake: ['Date'] }); // 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({ now: moment().subtract(20, 'hours').toDate(), shouldAdvanceTime: true });
clock = sinon.useFakeTimers({ now: moment().subtract(20, 'hours').toDate(), toFake: ['Date'] });
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({ now: new Date('2021-02-01 0:0').getTime(), shouldAdvanceTime: true });
let clock = useFakeTimers({ now: new Date('2021-02-01 0:0').getTime(), toFake: ['Date'] });
try {
const order1 = await fakeOrder({
CollectiveId: collective1.id,
Expand All @@ -72,7 +72,7 @@ describe('server/graphql/v2/query/HostMetricsQuery', () => {
clock.restore();
}

clock = useFakeTimers({ now: new Date('2021-06-01 0:0').getTime(), shouldAdvanceTime: true });
clock = useFakeTimers({ now: new Date('2021-06-01 0:0').getTime(), toFake: ['Date'] });
try {
const order2 = await fakeOrder({
CollectiveId: collective2.id,
Expand Down
2 changes: 1 addition & 1 deletion test/server/lib/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('server/lib/auth', () => {
// Given that time `Date.now` returns zero (0)
const clock = useFakeTimers({
now: 0,
shouldAdvanceTime: true,
toFake: ['Date'],
});

// When the token is generated
Expand Down
4 changes: 2 additions & 2 deletions 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({ now: new Date('2018-01-28 0:0').getTime(), shouldAdvanceTime: true });
const clock = useFakeTimers({ now: new Date('2018-01-28 0:0').getTime(), toFake: ['Date'] });

// When dates are updated with failure
const updatedDates = getNextChargeAndPeriodStartDates('failure', order);
Expand Down Expand Up @@ -185,7 +185,7 @@ describe('server/lib/recurring-contributions', () => {
};

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

// when dates are updated with 'updated' status
const updatedDates = getNextChargeAndPeriodStartDates('updated', 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({ now: new Date('2020-01-01T00:00:00.000Z'), shouldAdvanceTime: true });
clock = useFakeTimers({ now: new Date('2020-01-01T00:00:00.000Z'), toFake: ['Date'] });
});

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({ now: today.toDate(), shouldAdvanceTime: true }); // Manually setting today's date
clock = sinon.useFakeTimers({ now: today.toDate(), toFake: ['Date'] }); // 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({ now: today.toDate(), shouldAdvanceTime: true }); // Manually setting today's date
clock = sinon.useFakeTimers({ now: today.toDate(), toFake: ['Date'] }); // 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({ now: today.toDate(), shouldAdvanceTime: true }); // Manually setting today's date
clock = sinon.useFakeTimers({ now: today.toDate(), toFake: ['Date'] }); // 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({ now: new Date('2017-09-01 00:00:00').getTime(), shouldAdvanceTime: true });
timer = useFakeTimers({ now: new Date('2017-09-01 00:00:00').getTime(), toFake: ['Date'] });
});
after(() => {
timer.restore();
Expand Down
2 changes: 1 addition & 1 deletion test/server/models/User.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('server/models/User', () => {
() =>
(clock = useFakeTimers({
now: 0,
shouldAdvanceTime: true,
toFake: ['Date'],
})),
);
afterEach(() => clock.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({ now: fakeNow, shouldAdvanceTime: true });
clock = useFakeTimers({ now: fakeNow, toFake: ['Date'] });
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({ now: new Date(createdAt).getTime(), shouldAdvanceTime: true });
sandbox.useFakeTimers({ now: new Date(createdAt).getTime(), toFake: ['Date'] });
}

// 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({ now: moment('2024-10-01T00:00:00Z').toDate(), shouldAdvanceTime: true });
sandbox.useFakeTimers({ now: moment('2024-10-01T00:00:00Z').toDate(), toFake: ['Date'] });
expect(PlatformConstants.PlatformCollectiveId).to.equal(8686);
});

it('platform collective id is migrated after migration cutoff', () => {
sandbox.useFakeTimers({ now: moment('2024-10-01T00:00:01Z').toDate(), shouldAdvanceTime: true });
sandbox.useFakeTimers({ now: moment('2024-10-01T00:00:01Z').toDate(), toFake: ['Date'] });
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({ now: tc.when, shouldAdvanceTime: true });
sandbox.useFakeTimers({ now: tc.when, toFake: ['Date'] });

const result = await makeContribution(
{
Expand Down

0 comments on commit c1af332

Please sign in to comment.