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

feat(config): allow percentage value for workers option #5982

Conversation

syi0808
Copy link
Contributor

@syi0808 syi0808 commented Jun 27, 2024

Description

I can't use maxWorkers option that jest supported after migrating to vitest.
So I modified it so that the percentage value can be used in maxworkers and minworkers.

jestjs/jest#9012 (comment)
Although it is a feature request in jest, I would like to get the same feature in viteest.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

@syi0808 syi0808 marked this pull request as draft June 27, 2024 07:41
@syi0808 syi0808 force-pushed the feat/config/allow-percentage-for-worker-option branch 2 times, most recently from 0663b6c to 7a35516 Compare June 27, 2024 09:54
@syi0808 syi0808 marked this pull request as ready for review June 27, 2024 09:54
@syi0808 syi0808 force-pushed the feat/config/allow-percentage-for-worker-option branch 2 times, most recently from 585ad06 to 6cdf19d Compare June 27, 2024 17:00
@syi0808
Copy link
Contributor Author

syi0808 commented Jun 27, 2024

I want to add test for worker option but I found two problems.

  1. Can't mock node:os inside bundled vitest/node
  2. If i import resolveConfig function from package/vitest/node/config.ts to test, throw type error from runtime.

I don't think I can test the code properly if I can't mock node:os. Is it okay to test with cpus count without mocking?
Or is it okay to test only exception handling other than calculate logic?

@sheremet-va
Copy link
Member

I want to add test for worker option but I found two problems.

  1. Can't mock node:os inside bundled vitest/node
  2. If i import resolveConfig function from package/vitest/node/config.ts to test, throw type error from runtime.

I don't think I can test the code properly if I can't mock node:os. Is it okay to test with cpus count without mocking? Or is it okay to test only exception handling other than calculate logic?

You can create a separate utility that accepts the workers number and the amount of CPU and test that function.

@syi0808 syi0808 force-pushed the feat/config/allow-percentage-for-worker-option branch from 6cdf19d to f643a52 Compare June 28, 2024 01:22
@syi0808
Copy link
Contributor Author

syi0808 commented Jun 28, 2024

I want to add test for worker option but I found two problems.

  1. Can't mock node:os inside bundled vitest/node
  2. If i import resolveConfig function from package/vitest/node/config.ts to test, throw type error from runtime.

I don't think I can test the code properly if I can't mock node:os. Is it okay to test with cpus count without mocking? Or is it okay to test only exception handling other than calculate logic?

You can create a separate utility that accepts the workers number and the amount of CPU and test that function.

Thanks for nice idea! I separate getWorkers function to utility and node:os mock is works. I added test now.

})

describe('workers util', () => {
vi.mock('node:os', async importOriginal => ({
Copy link
Member

@sheremet-va sheremet-va Jun 30, 2024

Choose a reason for hiding this comment

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

vi.mock is always hoisted to the top of the file so it doesn't make a sense to have it inside describe function, let's put it at the top to make it less confusing. You can also use import syntac so you don't need to case the Record type:

Suggested change
vi.mock('node:os', async importOriginal => ({
vi.mock(import('node:os'), async importOriginal => ({

Copy link
Contributor Author

@syi0808 syi0808 Jun 30, 2024

Choose a reason for hiding this comment

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

Now that I look at it, I wonder how vi.mock worked. It's all reflected. thank you!

docs/guide/cli-table.md Outdated Show resolved Hide resolved
@syi0808 syi0808 force-pushed the feat/config/allow-percentage-for-worker-option branch from b81b684 to 47bd7af Compare June 30, 2024 17:34
@syi0808 syi0808 force-pushed the feat/config/allow-percentage-for-worker-option branch from 47bd7af to 1615b56 Compare June 30, 2024 17:52
packages/vitest/src/node/config.ts Outdated Show resolved Hide resolved
packages/vitest/src/utils/workers.ts Outdated Show resolved Hide resolved
sheremet-va
sheremet-va previously approved these changes Jul 1, 2024
@syi0808
Copy link
Contributor Author

syi0808 commented Jul 1, 2024

test failed.. because maybe i changed node:os import method. I fix test in a minutes.

@syi0808 syi0808 force-pushed the feat/config/allow-percentage-for-worker-option branch from c642034 to e8e52ee Compare July 1, 2024 08:20
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