question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Consider switching from dependencies.method(...) to inject(method)(...)

See original GitHub issue

Overview

It’s a little annoying how the dependency injection pattern here means you can’t F12 / right-click > go-to-definition on injected methods…

Actual Behavior

dependencies.method(/* ... */);

Expected Behavior

What if an inject method were used instead:

inject(method)(/* ... */)

…where inject.ts looks something like:

export const inject = process.env !== 'test'
    ? <Method>(method: Method) => method
    : require('./spies').inject;

… and test-inject.ts uses a global Map of jest.fns that are reset before each test?

const spies = new Map<[Function, jest.Mock]>();

export const inject = (method: Function) => {
    const existingSpy = spies.get(method);
    if (existingSpy) {
        return spy;
    }
    
    const newSpy = jest.fn();
    spies.add(method, newSpy);
    return newSpy;
};

export const getSpy = <Method>(method: Method): jest.Mock<Method> =>
    spies.get(method) as Method;

Tests would then be able to getSpy(method) for any injected method…

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JoshuaKGoldbergcommented, Apr 25, 2020

Aha, I have it! They’ll still take in a first parameter, but it’ll be inject: Inject. Now they won’t have to explicitly declare dependencies.

export type Inject = <Value>(raw: Value) =>
    Value extends (inject: Inject, ...args: infer Args) => infer Return
    ? (...args: Args) => Return
    : Value;
import { logger } from '../../adapters/logger';

// ...

inject(logger).stdout.write('...');
0reactions
KingDarBojacommented, Jul 16, 2020

💀 Rest in Peace I though you solved it and got lazy 😆

Read more comments on GitHub >

github_iconTop Results From Across the Web

A quick intro to Dependency Injection - freeCodeCamp
In software engineering, dependency injection is a technique whereby one object (or static method) supplies the dependencies of another ...
Read more >
Understanding Method Injection - Manning
Method Injection supplies a consumer with a Dependency by passing it as method argument on a method called outside the Composition Root. The ......
Read more >
Best Practices for Dependency Injection with Spring
In this post, I'm going to show you how to use Project Lombok for best practices in dependency injection with the Spring Framework....
Read more >
Dependency Injection Using Unity - Resolve ... - C# Corner
Inversion of control (IOC) and Dependency Injection (DI) work hand in hand and make our application more loosely coupled and easy to expand....
Read more >
Dependency Injection - TutorialsTeacher
Method Injection : In this type of injection, the client class implements an interface which declares the method(s) to supply the dependency and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found