Skip to content
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

[QUESTION] - Awilix.fast-glob Fails to Autoload Modules in Tests with TypeScript Setup #439

Closed
eskylake opened this issue Apr 27, 2024 · 1 comment

Comments

@eskylake
Copy link

I know that this may not be an issue and its better to be asked in any stackoverflow-like media.
I already asked one.
Also I opened an issue for Awilix recently. Due to the issue I'm asking it in here.

Environment

  • Node.js Version: node:20.12.2-alpine
"typescript": "^5"
"vitest": "^1"
"awilix": "^8"

Actual behavior

As Awilix is using fast-glob, awilix.loadModules() doesn't load modules while creating container through my tests as it doesn't have access to the actual .ts files.
Due to my research, I couldn't find any proper solution to the problem.
I don't wanna use type: module in package.json & I can't load .ts files directly into the container. Also I'm not sure if loading .ts files into the container directly is correct.

This won't work:

  container.loadModules(
    [
      `${resolve(__dirname, '../*.js')}`,
      `${resolve(__dirname, '../**/*.js')}`,
    ],
    {
      resolverOptions: {
        lifetime: Lifetime.SINGLETON,
        register: asClass,
      },
    },
  );

I could fix it by:
1- Including my test directory in both tsconfig.json and my testing library config (here is my vitest.config.ts). But I'm not sure if this is correct. Due to this solution my built version of files are running tests & I believe they are for runtime only.

2- Register each module in the container manually.

  container.register({
    MyModule: asClass(MyModuleClass, {
      lifetime: Lifetime.SINGLETON,
    }),
  });

Expected behavior

I expect the provided script auto loads modules located in the paths in both tests and production version:

I have just explained the entire process and all the code snippets related to the problem on stackoverflow.

Would you plz help me with this issue?

Thanks in advance

@mrmlnc
Copy link
Owner

mrmlnc commented Nov 23, 2024

I am not sure I can help here. Looks like awilix just call fast-glob without any major options.

Also, I do not use awilix in my applications, preferring to explicitly import dependencies using the DI model from (or like) Nest.js.

@mrmlnc mrmlnc closed this as completed Nov 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants