🤖 Backend
NestJS
Nestjs Testing

Assert to reject an error

await expect(
  (processor as any).processChunk(
  ),
).rejects.toThrowError();

Assert to not reject an error

await expect(
  service.bulkRemoveTags(orgId, payload),
).resolves.not.toThrowError();

Fixed date

beforeEach(async () => {
  jest.useFakeTimers('modern');
  jest.setSystemTime(Date.parse('1999-01-23T00:00:00Z'));
});
 
afterEach(async () => {
  jest.useRealTimers();
});

Mock Super Class

import { CubeJs } from 'src/utils/cubejs';
import { AnalyticsService } from './analytics-service';
 
jest.mock('src/utils/cubejs');
 
describe('AnalyticsService', () => {
  let analyticsService: AnalyticsService;
 
  beforeEach(() => {
    analyticsService = new AnalyticsService();
  });
 
  describe('getIndividualIssues', () => {
    it('should call load', async () => {
      // Arrange
      const spyLoad = jest
        .spyOn(CubeJs.prototype as any, 'load')
        .mockResolvedValue({
          rawData: jest.fn(),
        }); 
      const issueIds = ['issueId1', 'issueId2'];
 
      // Act
      await analyticsService.getIndividualIssues(issueIds);
 
      // Assert
      
    });
  });
});
 

Expecting nested object/array

expect(spyLoad).toBeCalledWith(
  expect.objectContaining({
    filters: expect.arrayContaining([
      expect.objectContaining({
        values: issueIds,
      }),
    ]),
  }),
);