adam-cowley / nestjs-neo4j-starter

A starter kit for Nest.js and Neo4j
9 stars 2 forks source link

Jest test fails on initial test: EncryptionService › should be defined #1

Closed tristolliday closed 3 years ago

tristolliday commented 3 years ago

Upon cloning the project, the jest test immediately fails on encryption.service

This is after no amends to the project.

Any advice would be appreciated

 FAIL  src/auth/encryption/encryption.service.spec.ts
  ● EncryptionService › should be defined

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

    Potential solutions:
    - If ConfigService is a provider, is it part of the current RootTestModule?
    - If ConfigService is exported from a separate @Module, is that module imported within RootTestModule?
      @Module({
        imports: [ /* the Module containing ConfigService */ ]
      })

      at Injector.lookupComponentInParentModules (../node_modules/@nestjs/core/injector/injector.js:192:19)
      at Injector.resolveComponentInstance (../node_modules/@nestjs/core/injector/injector.js:148:33)
      at resolveParam (../node_modules/@nestjs/core/injector/injector.js:102:38)
          at async Promise.all (index 0)
      at Injector.resolveConstructorParams (../node_modules/@nestjs/core/injector/injector.js:117:27)
      at Injector.loadInstance (../node_modules/@nestjs/core/injector/injector.js:81:9)
      at Injector.loadProvider (../node_modules/@nestjs/core/injector/injector.js:38:9)
          at async Promise.all (index 3)
      at InstanceLoader.createInstancesOfProviders (../node_modules/@nestjs/core/injector/instance-loader.js:43:9)

  ● EncryptionService › should be defined

    expect(received).toBeDefined()

    Received: undefined

      14 | 
      15 |   it('should be defined', () => {
    > 16 |     expect(service).toBeDefined();
         |                     ^
      17 |   });
      18 | });
      19 | 

      at Object.<anonymous> (auth/encryption/encryption.service.spec.ts:16:21)

Test Suites: 1 failed, 1 passed, 2 total
Tests:       1 failed, 1 passed, 2 total
Snapshots:   0 total
Time:        1.329 s, estimated 3 s
adam-cowley commented 3 years ago

The test is complaining that Nest.js test instance doesn't recognise the ConfigService that is injected into EncryptionService. I must have left that test in by accident - it doesn't really test anything at the moment so you could either remove it for now, or add ConfigService to the providers for the TestingModule:

import { ConfigService } from '@nestjs/config';
describe('EncryptionService', () => {
  let service: EncryptionService;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [
        ConfigService, // <-- injected into the encryption service
        EncryptionService
      ],
    }).compile();

    service = module.get<EncryptionService>(EncryptionService);
  });

  it('should be defined', () => {
    expect(service).toBeDefined();
  });
});
tristolliday commented 3 years ago

Thanks for that, Jest still baffles me. Need to get my head around it. Cheers for the great template 👍

adam-cowley commented 3 years ago

It's a weird one with Nest's Dependency Injection, if you have watched any of the live streams I did then you'll know it can be a pain to get your head around. When you create a test module you need to define all of the dependencies. Because I'm using the ConfigService to get the hash rounds before encrypting it needs to be defined somewhere within the app as a provider so Nest knows what to inject.

I'm glad you like the starter, I'd love to see what you make with it. Give us a shout when you're up and running, we'd be happy to help promote what you're working on on the Neo4j Developer Blog.