All files / src/modules/search elasticsearch.module.ts

57.89% Statements 11/19
50% Branches 2/4
33.33% Functions 2/6
56.25% Lines 9/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34  17x 17x 17x   17x               11x 11x 11x 6x                                 17x
// src/modules/search/elasticsearch.module.ts
import { Module } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Client as ESClient } from '@elastic/elasticsearch';
 
export const ELASTICSEARCH_CLIENT = 'ELASTICSEARCH_CLIENT';
 
@Module({
  imports: [require('@nestjs/config').ConfigModule],
  providers: [
    {
      provide: ELASTICSEARCH_CLIENT,
      useFactory: (configService: ConfigService) => {
        const isTest = process.env.NODE_ENV === 'test' || process.env.TEST_MODE === '1';
        if (isTest) {
          return {
            indices: { exists: async () => true, create: async () => ({}) },
            search: async () => ({ hits: { hits: [] } }),
            update: async () => ({}),
            bulk: async () => ({}),
          } as unknown as ESClient;
        }
        const node = configService.get<string>('ELASTICSEARCH_NODE');
        Iif (!node) {
          throw new Error('ELASTICSEARCH_NODE environment variable is not set.');
        }
        return new ESClient({ node });
      },
      inject: [ConfigService],
    },
  ],
  exports: [ELASTICSEARCH_CLIENT],
})
export class ElasticsearchModule {}