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

Error when running E2E tests with Nest.js #10

Open
no-creative-name opened this issue Sep 28, 2022 · 4 comments
Open

Error when running E2E tests with Nest.js #10

no-creative-name opened this issue Sep 28, 2022 · 4 comments

Comments

@no-creative-name
Copy link

no-creative-name commented Sep 28, 2022

Hi there,

thanks for the package! It really is super useful.
My issue is that we can't run our E2E tests with it as of now. We get the console error Error: DataSource with name "default" has already added. all the time. I assume it has to do with the dataSourceFactory being called multiple times (in multiple TestingModules) for the same database. Any suggestion how to approach/debug this? The setup is more or less the same as the default testing setup that Nest.js suggests.

Screenshot 2022-09-28 at 16 32 31

@Aliheym
Copy link
Owner

Aliheym commented Sep 28, 2022

Hi, thanks for your issue. I think I need to investigate this more to handle this.

Now you can also use something like that to prevent adding the data source several times:

import { addTransactionalDataSource, getDataSourceByName } from 'typeorm-transactional';

// ...

TypeOrmModule.forRootAsync({
    dataSourceFactory: async (options) => {
      return getDataSourceByName('default') || addTransactionalDataSource(new DataSource(options));
    },
  });

or

import { addTransactionalDataSource, deleteDataSourceByName } from 'typeorm-transactional';

// ...

TypeOrmModule.forRootAsync({
    dataSourceFactory: async (options) => {
      deleteDataSourceByName('default');

      return addTransactionalDataSource(new DataSource(options));
    },
  });

@no-creative-name
Copy link
Author

no-creative-name commented Sep 29, 2022

If I use the getDataSourceByName workaround, then I get the error Error: No CLS namespace defined in your app ... please call initializeTransactionalContext() before application start.
when running the tests. Even though I actually call initializeTransactionalContext() in the beginning of my bootstrap() function. It seems like something's off at a deeper level here.

Edit: My bad, I forgot that bootstrap() wasn't called by the E2E test setup. When I call initializeTransactionalContext() in my E2E setup function it works. Thanks a bunch! If you need testing or anything else for the main issue to solve here, let me know.

@Aliheym
Copy link
Owner

Aliheym commented Sep 29, 2022

Thanks! Will notify you.

@dsmabulage
Copy link

      return getDataSourceByName('default') || addTransactionalDataSource(new DataSource(options));

Thank you. If I'm not mistaken if we add a name to options, if we use multiple data sources, all the data sources get the name as the default

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants