Просмотр исходного кода

refs 114144: fix questionnaire cron test

Futa Arai 3 лет назад
Родитель
Сommit
f1b7d6dabc
1 измененных файлов с 3 добавлено и 31 удалено
  1. 3 31
      packages/app/test/integration/service/questionnaire-cron.test.ts

+ 3 - 31
packages/app/test/integration/service/questionnaire-cron.test.ts

@@ -3,24 +3,14 @@ import { getInstance } from '../setup-crowi';
 
 const axios = require('axios').default;
 
-const rand = require('../../../src/utils/rand');
-
 const spyAxiosGet = jest.spyOn<typeof axios, 'get'>(
   axios,
   'get',
 );
 
-const spyGetRandomIntInRange = jest.spyOn<typeof rand, 'getRandomIntInRange'>(
-  rand,
-  'getRandomIntInRange',
-);
-
 describe('QuestionnaireCronService', () => {
   let crowi;
 
-  const maxSecondsUntilRequest = 4 * 60 * 60 * 1000;
-  const secondsUntilRequest = rand.getRandomIntInRange(0, maxSecondsUntilRequest);
-
   const mockResponse = {
     data: {
       questionnaireOrders: [
@@ -134,9 +124,6 @@ describe('QuestionnaireCronService', () => {
   };
 
   beforeAll(async() => {
-    process.env.QUESTIONNAIRE_CRON_SCHEDULE = '0 22 * * *';
-    process.env.QUESTIONNAIRE_CRON_MAX_HOURS_UNTIL_REQUEST = '4';
-
     crowi = await getInstance();
     // reload
     await crowi.setupConfigManager();
@@ -242,33 +229,18 @@ describe('QuestionnaireCronService', () => {
       },
     ]);
 
-    // mock the date to 5 seconds before cronjob execution
-    const mockDate = new Date(2022, 0, 1, 21, 59, 55);
-    jest.useFakeTimers();
-    jest.setSystemTime(mockDate);
-
-    // must be after useFakeTimers for mockDate to be in effect
     crowi.setupCron();
 
     spyAxiosGet.mockResolvedValue(mockResponse);
-    spyGetRandomIntInRange.mockReturnValue(secondsUntilRequest); // static sleep time until request
   });
 
   afterAll(() => {
-    jest.useRealTimers();
     crowi.questionnaireCronService.stopCron();
   });
 
-  test('Should save quesionnaire orders and delete outdated ones', async() => {
-    jest.advanceTimersByTime(5 * 1000); // advance unitl cronjob execution
-    jest.advanceTimersByTime(secondsUntilRequest); // advance until request execution
-    jest.useRealTimers(); // after cronjob starts, undo timer mocks so mongoose can work properly
-
-    await new Promise((resolve) => {
-      // wait until cronjob execution finishes
-      // refs: https://github.com/node-cron/node-cron/blob/a0be3f4a7a5419af109cecf4a41071ea559b9b3d/src/task.js#L24
-      crowi.questionnaireCronService.cronJob._task.once('task-finished', resolve);
-    });
+  test('Job execution should save quesionnaire orders and delete outdated ones', async() => {
+    // testing the cronjob from schedule has untrivial overhead, so test job execution in place
+    await crowi.questionnaireCronService.executeJob();
 
     const savedOrders = await QuestionnaireOrder.find()
       .select('-condition._id -questions._id')