I don't know what is going on with the awilix container in a node-express app, but it is not working for new classes created in there, so basically what I did is created this dummy class
import { CustomLogger, getLogger } from "@somePath/logger";
export class TestClass {
private logger: CustomLogger;
constructor() {
this.logger = getLogger(TestClass.name);
}
testFunction = (text: string): void => {
console.log(text);
};
}
then registered it on the awilix container as the other classes in the app got registered before:
import { InjectionMode, asClass, createContainer } from "awilix";
import { TestClass } from "@somePath/testClass";
const registerDependencies = () => {
const container = createContainer({ injectionMode: InjectionMode.CLASSIC }).register({
testClass: asClass(TestClass).transient(),
});
return container;
};
export default registerDependencies;
then I try instantiating the TestClass using the called constructor in a test:
describe("testClass", () => {
it("testClass function test", async () => {
const testclass = new TestClass();
testClass.testFunction("print me");
//....some other stuff in the test
});
})
but I get TypeError: testClass_1.TestClass is not a constructor, in fact if I manually call the awilix container function in the test like:
const app = express();
const container = registerDependencies();
app.use(scopePerRequest(container));
describe("testClass", () => {
it("testClass function test", async () => {
const testclass = new TestClass();
testClass.testFunction("print me");
//....some other stuff in the test
});
})
I get AwilixTypeError: asClass: expected Type to be class, but got undefined. , does someone know what I'm I missing, or what's wrong there?
As I mentioned, previously registered classes are being registered, and I'm able to instantiate them, but when created a new one I was not able to do that because I think either the class is not registered, or awilix is not able to recognize it, or some additional setup is needed, but so fat I've checked exhaustively differences with the previously registered classes and I have not found any difference, I'm doing exactly the same stuff.
Thanks!