How testing `condition` blocks and signals (Manual time skipping?)

Hey there.

Working through some of the samples since i want to use temporal to solve a subscription use case. Im not sure how to test the following code in jest to show that when a cancel signal is received the subscription is terminated early (and the mock billing method is called < maxBillingPeriod).

async function BillingCycle(customer: Customer) {

  const status = useSubscriptionStore();
  const { maxBillingPeriods, billingPeriod} = customer;

  // Charge the customer at the end of each billing period
  while (status.value.billingPeriodsCharged < maxBillingPeriods) {

    if (await condition((() => status.value.isCanceled), billingPeriod)) {
      break;
    }    
    await chargeCustomerForBillingPeriod(customer);
    status.value.billingPeriodsCharged += 1;
  }
  await sendSubscriptionOverEmail(customer.email);
}

I get automatic time skippping from the Testing TypeScript Workflows | Legacy documentation for Temporal SDKs docs but manual time skipping doesnt seem to work. when i use start() instead of execute to start the test environment workflow my jest throws a timeout error meaning there is something wrong with the timeskipping sleep method. My test is written something like…

    it('cancels a subscription', async () => {
        const worker = await Worker.create({
            connection: testEnv.nativeConnection,
            taskQueue: 'test',
            activities: mockActivities,
            workflowsPath: require.resolve('./workflows'),
        });

        const handle = await testEnv.client.workflow.start(SubscriptionWorkflow, {
            args: [{
                email: 'ian-kaplan-2',
                billingPeriod: '30 seconds',
                trialPeriod: '30 seconds',
                maxBillingPeriods: 5,
                initialBillingPeriodCharge: 10,
                id: 'ian-kaplan-2',
            }],
            workflowId: 'workflow-1',
            taskQueue: 'test',
        });
        await testEnv.sleep(1000 * 50);
        await handle.signal(cancelSubscription);
        await handle.cancel()
        expect(mockActivities.chargeCustomerForBillingPeriod).toBeCalledTimes(1);
    });

any help would be appreciated. thanks!

It doesn’t look like you’re running the worker in the test.

@bergundy at what point would i run the worker? i treid calling worker.run() after creating the worker and i get the same timeout. How would i start the worker in this case given that im calling workflow.start and not workflow.execute?

I recommend running the worker with runUntil and putting the entire contents of the client code (including the sleep) in the async block.

See this example: Testing TypeScript Workflows | Legacy documentation for Temporal SDKs
And note that runUntil accepts an async function or promise.