Closed zveljkovic closed 2 years ago
I have created a PR for this https://github.com/testdeck/testdeck/pull/247
Until you merge it, how can we I use my fork instead of regular npm package? I haven't worked with Learna mono repos and forks...
Edit: Did a PR
@zveljkovic Thank you for your work.
However, this changes the semantics of the test suite.
Currently, the test class will be instantiated before each test and not just once beforeAll tests.
This has both historical as well as practical reasons, as you will not have to clean up after each test in order to get a clean state.
As such, we cannot change this behaviour as it would otherwise break existing test suites out there in the field.
Your best bet is actually the constructor of your test suite, which acts similar to a surrounding describe
and the scope that it will provide for the nested it
S.
However, and if you need longer living instances of say a kafka consumer or other service connectors, your best bet is to allocate them in the constructor as static variables of your class, e.g.
@suite class WithStaticState {
private static SERVICE_CONNECTOR : ServiceConnector;
constructor() {
if (WithStaticState.SERVICE_CONNECTOR == null) {
WithStaticState.SERVICE_CONNECTOR = ...;
}
}
}
And since you are using dependency injection, the instance of the service (connector) are maintained by the container, so why even bother? Depending on the container being used, the container will keep the service instances alive, even make them singletons, and hand out the same instance over and over again.
As such, the longevity state was already refactored out to the container and you can simply just inject that into your constructor.
Coming back to your example this would then mean
@suite
@injectable()
class KafkaReaderTest {
private static topicName: string = 'b-' + Utils.randomString();
private static bool topicCreated = false;
constructor(private kafkaManager: KafkaManager) {}
async before(): Promise<any> {
if (!KafkaReaderTest.topicCreated) {
await this.kafkaManager.createTopic({
topic: this.topicName,
});
}
}
or something similar along that line.
Closing as this can be resolved using standard language paradigms instead of introducing an altogether new behaviour to testdeck, which in fact would break existing mocha-typescript behaviour, making it difficult to migrate from mocha-typescript to testdeck/mocha.
@zveljkovic do you have a working inversify handler? Or should we provide one?
It is rather simple, see the testdeck/di-typedi package which integrates the typedi container.
Hi and thanks for your response. Everything is clear and I agree with your thoughts on the subject. I have made a simple Inversify handler myself, but it would be nice for other users to have one implementation in the core/docs.
If I understand correctly the long-story-short is we need DI to resolve arguments in lifecycle hooks and test methods.
@panayot-cankov great to have you back, as I have been busy with studying and doing my own stuff. yes, that it what was proposed, as you might remember, far back in the past. nevertheless, I do not have any time to implement this, nor update the existing documentation.
apart from that, before all needs to be a static hook and cannot be executed otherwise, which was indeed the original question. and yes, injecting these in non static before or even test methods would be great.
however, the OP seems to be misunderstanding the before_all and before mechanism, where before_all maps to e.g. mocha before_all which gets run before any tests of a given suite are being run, similarly to after_all. and the before, which gets run before each test, similarly to the after, which gets run after each test.
so, reorganizing the code a wee bit should actually do the trick, even with DI working only at the constructor level. however, being able to inject on the hook/test level seems to be a goal to go for, as long as it does not interfere with @params
and so on.
I feel like doing this over the weekend.
beforeall is always static and this cannot be changed.
Do we have DI hooks for before and after?
@pana-cc
The injection takes place at the constructor level only, see the di-typedi package.
I don't see why user can't use a helper class that is a singleton that would then provide such facilities like kafka etc.
@Singleton
class KafkaFacility {
}
@suite
class MySuite {
static before() {
KafkaFacility.getInstance()...
}
static after() {
KafkaFacility.getInstance()...
}
}
To put static before
into context of the supported test frameworks
describe('...', function () {
beforeAll(...);
...
afterAll(...);
})
Question
I need some of my DI objects in beforeAll, is there any non static version of beforeAll? Namely, I want in below code to inject KafkaManager to create a new topic. I can DI only in constructor, and from within static before() I can't use instance variables...