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

test(backend): configure db per jest worker #45

Merged
merged 4 commits into from
Jul 9, 2021

Conversation

cairin
Copy link
Collaborator

@cairin cairin commented Jul 9, 2021

Changes proposed in this pull request

  • Creates a db per jest worker thread, to ensure that tests can run concurrently. Inspiration

Context

Checklist

  • Related issues linked using fixes #number
  • Tests added/updated
  • Documentation added
  • Make sure that all checks pass

@@ -22,10 +22,14 @@ describe('Account Service', (): void => {
const mockMessageProducer = {
send: jest.fn()
}
const overrideConfig = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this instead happen in createTestApp like how it currently is in main (what I removed in the testcontainers pr) instead of each test file?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved it to the config file. It's needed when we create the IoC container, which happens before createTestApp is run.

@@ -7,6 +7,8 @@ const POSTGRES_PORT = 5432
const REDIS_PORT = 6379

module.exports = async () => {
const workers = parseInt(process.env.JEST_WORKERS || '1')
Copy link
Contributor

@wilsonianb wilsonianb Jul 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the number of actual workers exceeds process.env.JEST_WORKERS?
It'd be nice if jest had a way for the global setup file to know the number of workers, but I'm not seeing one.

Copy link
Contributor

@wilsonianb wilsonianb Jul 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--maxWorkers

this defaults to the number of the cores available on your machine minus one for the main thread.

Could we ditch process.env.JEST_WORKERS and calculate that default value ourselves?

Suggested change
const workers = parseInt(process.env.JEST_WORKERS || '1')
const workers = os.cpus().length - 1

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jestjs/jest#8708

I guess we could just write a script that counts how many test files there are rather.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we ditch process.env.JEST_WORKERS and calculate that default value ourselves?

Didn't see that, that could work 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, maxWorkers is accessible in globalSetup:

This function gets Jest's globalConfig object as a parameter.

So no computation required

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function gets Jest's globalConfig object as a parameter.

That's not actually accessible from the function, that's just for storing global values like db configs that need to be accessed in teardown.

Co-authored-by: Brandon Wilson <brandon@coil.com>
@cairin cairin merged commit eb40e58 into bw-testcontainers Jul 9, 2021
@cairin cairin deleted the cairin-concurrent-tests branch July 9, 2021 16:05
wilsonianb added a commit that referenced this pull request Jul 12, 2021
* feat(connector): use testcontainers

* feat(accounts): use testcontainers

* feat(backend): use testcontainers

* chore(tests): run testcontainers if no database configuration

* test(backend): configure db per jest worker (#45)

* test(backend): configure db per jest worker

* refactor: move test db name into config

* fix: replace JEST_WORKERS with os cpu count

* fix: use globalConfig for maxWorkers

Co-authored-by: Brandon Wilson <brandon@coil.com>

Co-authored-by: Brandon Wilson <brandon@coil.com>

* test(connector): configure redis db per jest worker

Co-authored-by: Cairin Michie <cairinmichie@gmail.com>
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

Successfully merging this pull request may close these issues.

2 participants