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

Can't use clsService across different test runs with --runInBand #49

Closed
lazaroofarrill opened this issue Dec 19, 2022 · 4 comments
Closed
Assignees
Labels
external bug The problem related to a 3rd party tool

Comments

@lazaroofarrill
Copy link

I'm using a common setup function for all my unit tests. However when running several test suites together only the first one to use the ClsService succeeds.
Repro url: https://github.com/lazaroofarrill/nestjs-cls-error-across-multiple-test-runs

@Papooch Papooch added the investigation Why is this happening? label Dec 19, 2022
@Papooch Papooch self-assigned this Dec 19, 2022
@Papooch Papooch added the bug Something isn't working label Dec 19, 2022
@Papooch
Copy link
Owner

Papooch commented Dec 19, 2022

Hm, I was able to reproduce it. I suspect there's something strange happening with how Jest treats async_hooks, because the error only happens with the --runInBand flag set. Without it, it works okay.

It also seems it's not related to your common setup, as it reliably fails even without it.

Given you're only concerned with unit tests that don't need to run in band, this should be enough of a "workaround" for you, but I'll investigate further.

@Papooch Papooch changed the title Can't use clsService across different test runs. Can't use clsService across different test runs with --runInBand Dec 19, 2022
@Papooch
Copy link
Owner

Papooch commented Dec 19, 2022

It's most likely related to this bug in Jest that was closed with the following:

For anyone else who comes across this issue, if you want to have some automatic ALS setup for each test, beforeEach is probably a non-starter for now. More resilient approaches might be to 1. enterWith at the start of each test, or 2. roll your own test function that wraps your test in an ALS run.

Which is what I would also suggest.

The fact that the enterWith method is still not stable does not help the case.


To summarize:

  • the issue only happens when the --runInBand flag AND when enter is called in a beforeXX hook.
  • it does not happen when any of the following is true
    • the --runInBand is NOT used
    • enter is called inside the test case
    • the test case is wrapped with run (this is the safest option)

@Papooch Papooch added external bug The problem related to a 3rd party tool and removed bug Something isn't working investigation Why is this happening? labels Dec 19, 2022
@lazaroofarrill
Copy link
Author

lazaroofarrill commented Dec 19, 2022

This is truly I sad. I was using a custom it function to wrap run but I really wanted to avoid that. I have to run the tests in band otherwise they crash my computer.

@Papooch
Copy link
Owner

Papooch commented Dec 19, 2022

I mean, in unit tests, you should treat ClsService as an external dependency and mock it out anyway. E2E tests should also work fine, since the request goes through the middleware. If need be, a custom it function seems like a viable workaround.

I have to close this though, because there's nothing I can do about it. Maybe try mocha as your test runner?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
external bug The problem related to a 3rd party tool
Projects
None yet
Development

No branches or pull requests

2 participants