#ConfigurationError: Missing node(s) option

6 messages · Page 1 of 1 (latest)

azure saddle
#

I am creating a test module that has only BatteryService as a provider. I have an ElasticService which has nothing to do with BatteryService, but for some reason I get the error: "ConfigurationError: Missing node(s) option" which refers to the ElasticsearchService class. Is there any solution to this problem?

battery.service.ts

@Injectable()
export class BatteryService {
  constructor(
    @Inject(WINSTON_MODULE_NEST_PROVIDER) private readonly logger: Logger,
    @InjectModel(Device.name) private deviceModel: Model<DeviceSchemaDocument>,
  ) {}

  async addBatteryDevice(batteryInput: BatteryInput) {
    try {
      const device = new this.deviceModel({
        ...batteryInput,
      });

      return await device.save();
    } catch (e) {
      this.logger.error(e);
    }
  }

  async updateBatteryDevice(id: string, input: BatteryUpdateInput) {
    try {
      return await this.deviceModel.findByIdAndUpdate(id, input, {
        new: true,
        useFindAndModify: false,
      });
    } catch (e) {
      this.logger.error(e);
    }
  }
}
#

elastic.service.ts

@Injectable()
export class ElasticService {
  private readonly elasticClient: Client;

  constructor(private readonly configService: ConfigService) {
    this.elasticClient = new ElasticsearchService({
      node: configService.get('ELASTICSEARCH_NODE'),
      auth: {
        username: configService.get('ELASTICSEARCH_USERNAME'),
        password: configService.get('ELASTICSEARCH_PASSWORD'),
      },
      headers: {
        'Content-Type': 'application/json',
      },
    });
  }

  getClient(): Client {
    return this.elasticClient;
  }

  async addIndex(input: AddIndexInterface) {
    try {
      return await this.elasticClient.index({
        index: input.index,
        id: input.id,
        body: {
          name: input.name,
        },
      });
    } catch (e) {
      throw new Error(e);
    }
  }

  async updateIndex(input: AddIndexInterface) {
    try {
      return await this.elasticClient.update({
        index: input.index,
        id: input.id,
        body: {
          doc: {
            name: input.name,
          },
        },
      });
    } catch (e) {
      throw new Error(e);
    }
  }

  async searchFuzzyIndices(index: string, query: string) {
    try {
      const { body } = await this.elasticClient.search({
        index: index,
        body: {
          query: {
            multi_match: {
              fields: ['name'],
              query: query,
              fuzziness: 'AUTO',
            },
          },
        },
      });

      return body.hits.hits;
    } catch (e) {
      throw new Error(e);
    }
  }
}
#

mongoose.helper.ts

import { MongooseModule, MongooseModuleOptions } from '@nestjs/mongoose';
import { MongoMemoryServer } from 'mongodb-memory-server';

let mongo: MongoMemoryServer;

export const rootMongooseTestModule = (options: MongooseModuleOptions = {}) =>
    MongooseModule.forRootAsync({
        useFactory: async () => {
            mongo = await MongoMemoryServer.create();
            const mongoUri = mongo.getUri();
            return {
                uri: mongoUri,
                ...options,
            };
        },
    });

export const closeInMongodConnection = async () => {
    if (mongo) await mongo.stop();
};
#

battery.service.spec.ts

let service: BatteryService;
let testingModule: TestingModule;

beforeAll(async () => {
    testingModule = await Test.createTestingModule({
        imports: [
            rootMongooseTestModule(),
            ConfigModule.forRoot(),
            MongooseModule.forFeature([
                {
                    name: Device.name,
                    schema: DeviceSchema,
                },
            ]),
        ],
        providers: [BatteryService],
    }).compile();

    service = testingModule.get<BatteryService>(BatteryService);
});

afterAll(async () => {
    if (testingModule) {
        await testingModule.close();
        await closeInMongodConnection();
    }
});
#

battery.service.spec.ts

it('should create a Battery device', async () => {
    const battery = await service.addBatteryDevice(createBatteryInput) as BatteryOutput;
    expect(battery._id).toBeDefined();
    expect(battery.name).toBe(createBatteryInput.name);
    expect(battery.manufacturer).toBe(createBatteryInput.name);
    batteryId = '' + battery._id;
});

it('should update a Battery device', async () => {
    const updatedBattery = await service.updateBatteryDevice(batteryId, updateUserInput) as BatteryOutput
    expect(updatedBattery._id).toBe(batteryId)
    expect(updatedBattery.name).not.toBe(createBatteryInput.name)
    expect(updatedBattery.props.cell_count).toBe(updatedBattery.props.cell_count)
});
azure saddle