import { Redis } from 'ioredis' ; import { enqueueTask } from '../../actions/enqueue-task' ; import { takeTaskAndMarkAsProcessing } from '../../actions/take-task-and-mark-as-processing' ; import { markTaskFailed } from '../../actions/mark-task-failed' ; import { flushAll , quit , createClientAndLoadLuaScripts , lrange , } from '../../utils/redis' ; import { createUuid } from '../../utils/general' ; import { redisConfig } from '../config' ; import { Task } from '../../domain/tasks/task' ; import { TaskStatus } from '../../domain/tasks/task-status' ; import { getTaskById } from '../../actions/get-task-by-id' ; import { getFailedListKey } from '../../utils/keys' ; describe ( 'markTaskFailed' , ( ) => { const queue = createUuid ( ) ; let client ; beforeAll ( ( ) => { client = createClientAndLoadLuaScripts ( redisConfig ) ; } ) ; beforeEach ( async ( ) => { await flushAll ( { client } ) ; } ) ; afterAll ( async ( ) => { await quit ( { client } ) ; } ) ; it ( 'markTaskFailed marks task failed' , async ( ) => { const task = { id : 'i' , data : 'j' } ; await enqueueTask ( { queue , client , task } ) ; const acquiredTask = ( await takeTaskAndMarkAsProcessing ( { queue , client , } ) ) as Task ; const failedTask = await markTaskFailed ( { task : acquiredTask , queue , client , error : 'aww :(' , } ) ; expect ( failedTask ) . toHaveProperty ( 'status' , TaskStatus . Failed ) ; expect ( failedTask ) . toHaveProperty ( 'error' , 'aww :(' ) ; const fetchedFailedTask = await getTaskById ( { taskId : task . id , queue , client , } ) ; expect ( fetchedFailedTask ) . toHaveProperty ( 'status' , TaskStatus . Failed ) ; expect ( fetchedFailedTask ) . toHaveProperty ( 'error' , 'aww :(' ) ; const failedTaskIds = await lrange ( { key : getFailedListKey ( { queue } ) , start : 0 , stop : - 1 , client , } ) ; expect ( failedTaskIds . length ) . toBe ( 1 ) ; expect ( failedTaskIds [ 0 ] ) . toBe ( task . id ) ; } ) ; } ) ;