Skip to content

Commit b701e74

Browse files
committed
Refactor unit tests for AwsTextractService to improve mock configuration
- Renamed configService to mockConfigService for clarity in backend/src/services/aws-textract.service.spec.ts. - Simplified the setup of mock dependencies by directly creating the mockConfigService instance. - Enhanced readability by removing unnecessary async/await in the beforeEach setup.
1 parent 3ce6dce commit b701e74

File tree

1 file changed

+16
-29
lines changed

1 file changed

+16
-29
lines changed

backend/src/services/aws-textract.service.spec.ts

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Test, TestingModule } from '@nestjs/testing';
21
import { ConfigService } from '@nestjs/config';
32
import { AwsTextractService } from './aws-textract.service';
43
import { BadRequestException } from '@nestjs/common';
@@ -14,7 +13,7 @@ vi.mock('../utils/security.utils', () => ({
1413

1514
describe('AwsTextractService', () => {
1615
let service: AwsTextractService;
17-
let configService: ConfigService;
16+
let mockConfigService: ConfigService;
1817

1918
// Create mocks
2019
const mockTextractSend = vi.fn();
@@ -138,43 +137,31 @@ describe('AwsTextractService', () => {
138137
};
139138

140139
// Setup mock dependencies
141-
beforeEach(async () => {
140+
beforeEach(() => {
142141
vi.clearAllMocks();
143142

144143
// Set up mock response
145144
mockTextractSend.mockResolvedValue(mockTextractResponse);
146145

147-
// Create mock config with a type that allows any string key
148-
const mockConfig: Record<string, any> = {
149-
'aws.region': 'us-east-1',
150-
'aws.aws.accessKeyId': 'test-access-key',
151-
'aws.aws.secretAccessKey': 'test-secret-key',
152-
'aws.aws.sessionToken': 'test-session-token',
153-
'aws.textract.maxBatchSize': 10,
154-
'aws.textract.documentRequestsPerMinute': 10,
155-
};
156-
157146
// Create mock ConfigService
158-
configService = {
159-
get: vi.fn((key: string) => mockConfig[key]),
147+
mockConfigService = {
148+
get: vi.fn().mockImplementation((key: string) => {
149+
const config: Record<string, any> = {
150+
'aws.region': 'us-east-1',
151+
'aws.aws.accessKeyId': 'test-access-key',
152+
'aws.aws.secretAccessKey': 'test-secret-key',
153+
'aws.aws.sessionToken': 'test-session-token',
154+
'aws.textract.maxBatchSize': 10,
155+
'aws.textract.documentRequestsPerMinute': 10,
156+
};
157+
return config[key];
158+
}),
160159
} as unknown as ConfigService;
161160

162-
// Create mock module with mocked dependencies
163-
const module: TestingModule = await Test.createTestingModule({
164-
providers: [
165-
{
166-
provide: ConfigService,
167-
useValue: configService,
168-
},
169-
AwsTextractService,
170-
],
171-
}).compile();
172-
173-
// Get service instance
174-
service = module.get<AwsTextractService>(AwsTextractService);
161+
// Create service instance
162+
service = new AwsTextractService(mockConfigService);
175163

176164
// Replace dependencies with mocks
177-
// This is a hacky but effective way to inject mocks
178165
(service as any).client = {
179166
send: mockTextractSend,
180167
};

0 commit comments

Comments
 (0)