Consider switching from dependencies.method(...) to inject(method)(...)
See original GitHub issueOverview
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.fn
s 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:
- Created 3 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.💀 Rest in Peace I though you solved it and got lazy 😆