Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

348
Vistas
How to schedule bi-weekly cron jobs using @nestjs/schedule module

By bi-weekly I mean every two weeks.

I have a job I'd like to run every two weeks on Saturdays at 4:30pm and I'm using NestJs's @Cron decorator to specify when it runs.

Is there a simple way to specify intervals of two weeks from a specific start date?

import { Injectable, Logger } from '@nestjs/common';
import { Cron} from '@nestjs/schedule';

@Injectable()
export class TasksService {
  private readonly logger = new Logger(TasksService.name);

  _called = 0;

  @Cron('* * * * * sat')
  handleBiWeekly(): void {
    this.scrapeContestData();
  }

  scrapeContestData(): void {
    this._called += 1;
  }
}

These two test cases need to pass:

import { INestApplication } from '@nestjs/common';
import { ScheduleModule } from '@nestjs/schedule';
import { Test } from '@nestjs/testing';
import { AppModule } from '../app.module';
import { TasksModule } from './tasks.module';
import { TasksService } from './tasks.service';
import * as sinon from 'sinon';

describe('TasksService', () => {
  let app: INestApplication;
  let clock: sinon.SinonFakeTimers;

  beforeEach(async () => {
    const module = await Test.createTestingModule({
      imports: [
        {
          module: AppModule,
          imports: [ScheduleModule.forRoot(), TasksModule],
          providers: [TasksService],
        },
      ],
    }).compile();

    app = module.createNestApplication();
  });

describe('biweekly', () => {
    test('Feb 19 at 4:30 PM, scrapeContestData TO BE called', async () => {
      const service = app.get(TasksService);
      clock = sinon.useFakeTimers({
        now: new Date('2022-02-19T16:30Z').valueOf(),
      });
      await app.init();
      clock.tick(3000);
      expect(new Date().getDay()).toBe(6);
      expect(service._called).toBe(3);
    });

     test('a week after Feb 19 at 4:30PM, scrapeContestData to NOT BE called', async () => {
      const service = app.get(TasksService);
      clock = sinon.useFakeTimers({
        now: new Date('2022-02-26T16:30Z').valueOf(),
      });
      await app.init();
      clock.tick(3000);
      expect(new Date().getDay()).toBe(6);
      expect(service._called).toBe(0);
    });
});
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda