-
-
Notifications
You must be signed in to change notification settings - Fork 181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds support for runInInjectionContext #664
Comments
Hi @NetanelBasal, First of all, I would like to thank you for creating this fantastic tool that Spectator is, it's been my go-to for writing tests in Angular for years. 💪 With the rise of popularity with DI functions in Angular, I've found myself missing out on Proposed solutionThe general support for export abstract class BaseSpectator {
// ...
public runInInjectionContext<T>(fn: () => T): T {
return TestBed.runInInjectionContext(fn);
}
} Just like that, It seems clear that something more is needed to make this change useful: a Spectator instance (let's call it export type SpectatorInjectionContextOptions = Pick<
BaseSpectatorOptions,
"imports" | "mockProvider" | "mocks" | "providers"
>;
export function createInjectionContextFactory(
options: SpectatorInjectionContextOptions
): SpectatorInjectionContextFactory {
//...
} The reason I suggest such type restriction is because I think it nicely highlights the expected use case for this particular Spectator instance. If that's too much of a limitation, The new factory can then be used just like that: describe("...", () => {
// TestModule provides TestService
const createContext = createInjectionContextFactory({
imports: [TestModule],
providers: [{ provide: TEST_TOKEN, useValue: "abcd" }],
});
let spectator: SpectatorInjectionContext;
beforeEach(() => (spectator = createContext()));
it("should execute fn in injection context", () => {
const service = spectator.inject(TestService);
service.flag = true;
const result = spectator.runInInjectionContext(() => testFn(2));
expect(result).toEqual({ token: "abcd", flag: true, arg: 2 });
});
}); This gives us a nice Spectator-ish way of testing DI functions, but at this point it's hard to ignore the similarity to the pure TestBed counterpart: it("should execute fn in injection context", () => {
// TestModule provides TestService
TestBed.configureTestingModule({
imports: [TestModule],
providers: [{ provide: TEST_TOKEN, useValue: "abcd" }],
});
const service = TestBed.inject(TestService);
service.flag = true;
const result = TestBed.runInInjectionContext(() => testFn(2));
expect(result).toEqual({ token: "abcd", flag: true, arg: 2 });
}); It doesn't really feel like the Spectator makes our lives any simpler by enabling this support and overall makes me question whether this brings some actual value beyond just feature parity with TestBed. I'm happy to hear your thoughts on this and ready to follow-up with a Pull Request if such implementation is deemed worthwhile. 😀 Why make it different from other factories?This is probably an obvious question that I prefered to leave for the end. Other Spectator factories universally employ the To keep the answer short - because DI functions are not classes. All we want to do in order to test them is setup some providers and run them in Injection Context. There's no need to pass the function internally to TestBed's |
Hi, seems like a nice idea. I'd love to see a PR :) |
It's up for review 🙂 #690 |
Description
Add support for
TestBed.runInInjectionContext
on spectatorProposed solution
N/A
Alternatives considered
N/A
Do you want to create a pull request?
No
The text was updated successfully, but these errors were encountered: