无法在NestJS中模拟Elasticsearch服务

时间:2018-11-28 07:02:16

标签: javascript node.js elasticsearch jestjs nestjs

当前行为

Nest can't resolve dependencies of the ElasticsearchService (?). Please make sure that the argument at index [0] is available in the ElasticsearchModule context.

预期行为

在测试模块中创建ElasticSearchService

通过说明将问题最小化

import { Test, TestingModule } from '@nestjs/testing';
import { RepliesController } from './replies.controller';
import { ElasticsearchService, ElasticsearchModule } from '@nestjs/elasticsearch';
import { Client } from 'elasticsearch';

describe('Replies Controller', () => {
  let module: TestingModule;
  let elasticsearch: ElasticsearchService;

  beforeAll(async () => {
    module = await Test.createTestingModule({
      imports: [ElasticsearchModule],
      controllers: [RepliesController],
      components: [ElasticsearchService, {provide: Client, useValue: {}}],
    }).compile();

    elasticsearch = module.get<ElasticsearchService>(ElasticsearchService);
  });
  it('should be defined', () => {
    const controller: RepliesController = module.get<RepliesController>(RepliesController);
    expect(controller).toBeDefined();
  });
});

环境

   [Nest Information]
elasticsearch version : 0.1.2
common version        : 5.4.0
core version          : 5.4.0

1 个答案:

答案 0 :(得分:1)

您需要使用测试模块公开的方法覆盖提供程序,而不是在components数组中进行设置:

Interval(HH:mi:ss)
0:4:23
696:1:36
696:4:51
8760:1:18

There are examples of this in the NestJs docs in the testing section.