title: How to test a nestjs service by passing in a ConfigService with custom values?
body: <p>I've created a service, and the module for it looks like this:</p>
  <p><strong>launchdarkly.module.ts</strong></p>
  <pre class="lang-js prettyprint-override"><code>@Module({
    providers: [LaunchdarklyService],
    exports: [LaunchdarklyService],
    imports: [ConfigService],
  })
  export class LaunchdarklyModule {}
  </code></pre>
  <p>(this service/module is to let the application use LaunchDarkly feature-flagging)</p>
  <p>I'm happy to show the service-implementation if you'd like, but to keep this question shorter I skipped it. The important point is that this service imports the <code>ConfigService</code> (which it uses to grab the LaunchDarkly SDK key).</p>
  <p>But how can I test the <code>Launchdarkly</code> service? It reads a key from <code>ConfigService</code> so I want to  write tests where <code>ConfigService</code> has various values, but after hours of trying I can't figure out how to configure <code>ConfigService</code> in a test.</p>
  <p>Here's the test:</p>
  <p><strong>launchdarkly.service.spec.ts</strong></p>
  <pre class="lang-js prettyprint-override"><code>describe('LaunchdarklyService', () =&gt; {
    let service: LaunchdarklyService;
  
    beforeEach(async () =&gt; {
      const module: TestingModule = await Test.createTestingModule({
        providers: [LaunchdarklyService],
        imports: [ConfigModule],
      }).compile();
  
      service = module.get&lt;LaunchdarklyService&gt;(LaunchdarklyService);
    });
  
    it(&quot;should not create a client if there's no key&quot;, async () =&gt; {
      // somehow I need ConfigService to have key FOO=undefined for this test
      expect(service.client).toBeUndefined();
    });
  
    it(&quot;should create a client if an SDK key is specified&quot;, async () =&gt; {
      // For this test ConfigService needs to specify FOO=123
      expect(service.client).toBeDefined();
    });
  })
  </code></pre>
  <p>I'm open for any non-hacky suggestions, I just want to feature-flag my application!</p>