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

process.chdir() is not supported in workers (--no-threads doesn't work) #1436

Closed
6 tasks done
cyco130 opened this issue Jun 5, 2022 · 16 comments · Fixed by #2772
Closed
6 tasks done

process.chdir() is not supported in workers (--no-threads doesn't work) #1436

cyco130 opened this issue Jun 5, 2022 · 16 comments · Fixed by #2772
Labels
enhancement New feature or request

Comments

@cyco130
Copy link
Contributor

cyco130 commented Jun 5, 2022

Describe the bug

When I try to call process.chdir in a test, I get "TypeError: process.chdir() is not supported in workers". From #566 I understand the solution is passing --no-threads but it doesn't seem to work.

Reproduction

https://stackblitz.com/edit/node-ve8ywd?file=package.json

System Info

System:
  OS: Linux 5.4 Linux Mint 20.3 (Una)
  CPU: (8) x64 Intel(R) Core(TM) i7-3630QM CPU @ 2.40GHz
  Memory: 3.70 GB / 15.52 GB
  Container: Yes
  Shell: 5.0.17 - /bin/bash
Binaries:
  Node: 18.2.0 - /usr/local/bin/node
  Yarn: 1.22.11 - ~/.npm-global/bin/yarn
  npm: 8.5.1 - ~/.npm-global/bin/npm
Browsers:
  Chrome: 102.0.5005.61
  Firefox: 101.0
npmPackages:
  vitest: ^0.13.1 => 0.13.1

Used Package Manager

pnpm

Validations

@sheremet-va
Copy link
Member

--no-threads no longer runs tests in the same thread, so the workaround no longer works, that is true. I don't think there is a workaround at the moment.

Does it work with jest? If so, is it an actual chdir call in jest or mocked one? Maybe we need to provide our own mocked process.

@cyco130
Copy link
Contributor Author

cyco130 commented Jun 5, 2022

Thanks for the quick reply!

I haven't tried with jest, I don't have it set up at the moment, I haven't used it in a while :) Mocking won't help in my case because I needed this to run some CI tests, I will just use spawn's cwd option.

It is surprising and confusing though, maybe we should document it more clearly?

@cyco130
Copy link
Contributor Author

cyco130 commented Jun 5, 2022

My quick attempt at trying the same with jest didn't work at all. No idea what's wrong. Maybe it doesn't work on StackBlitz?

@sheremet-va
Copy link
Member

sheremet-va commented Jun 5, 2022

My quick attempt at trying the same with jest didn't work at all. No idea what's wrong. Maybe it doesn't work on StackBlitz?

I guess stackblitz doesn't support Jest.

It is surprising and confusing though

Why is it confusing? I thought it was a common knowledge that we run tests in worker. I think we can try forward calling chdir inside a worker to the main thread, but that action will be async, so if some code depends on it right afterwards it will fail. So, maybe this is even more confusing 😄

@cyco130
Copy link
Contributor Author

cyco130 commented Jun 5, 2022

I tried locally with jest and it works as expected both in ESM and CJS. I can statSync files in the target directory meaning it really does change the directory.

I think we can try forward calling chdir inside a worker to the main thread

I was hoping for an option to run code in the main thread like the old --no-threads. It may be useful since workers may have other incompatibilities.

@sheremet-va
Copy link
Member

I was hoping for an option to run code in the main thread

We don't provide this option, so tests won't have access to the same global space as Vitest internals and server.

@cyco130
Copy link
Contributor Author

cyco130 commented Jun 5, 2022

It's reasonable. Thanks for your help!

@sheremet-va sheremet-va added bug enhancement New feature or request labels Jun 9, 2022
@fyyyyy
Copy link

fyyyyy commented Jul 8, 2022

I am converting jest specs to vitest.
I can confirm process.chdir(path) has no issue with jest, but vitest throws an error regardless of --threads=false
We need this option to test something as vue-cli which generates code in a different folder.

@scalvert
Copy link

scalvert commented Jul 19, 2022

So what's the path forward here? Simply not using process.chdir doesn't seem like a reasonable forward path.

The use case we currently support in many packages is creating a tmp dir or npm package using something like fixturify-project, and use that directory as a fixture for tests. At the end of the test, we dispose of the test directory, which requires us to switch directories before to support OS tests such as windows-latest, which will error if you try to rm a directory when it's the current dir.

This seems like a reasonably common usage, and we were previously using --no-threads with vitest when we needed to support this scenario. Now that --no-threads no longer works for this use case, there doesn't seem to be a workaround.

I understand that this is a larger issue with threads and changing directories, but it seems like we could collectively figure out a solution.

adriencaccia added a commit to swarmion/swarmion that referenced this issue Sep 11, 2022
We cannot migrate this package to vitest yet, until the following
issue is resolved:
vitest-dev/vitest#1436
@NullVoxPopuli
Copy link

Is there a way to turn of workers for a file?
I'd really like to test all my node utils using chdir :D

@JamesHenry
Copy link

Closed my duplicate issue in favour of this one. At the very least this limitation should be called out on the jest migration guide IMO, but obviously it would be even better to have some kind of official path forward on this! Lerna makes extensive use of changing directory in its jest tests that I’d love to use vitest for

@silverwind
Copy link
Contributor

silverwind commented Nov 18, 2022

How about an option to use childProcess.fork instead of worker_threads? That's how ava does it.

@jiby-aurum
Copy link

@sheremet-va would the core team be willing to accept a PR to add an option that enables to run tests in main thread without going through tinypool.

I was trying to utilise vitest to build a runtime test suite system for an internal project, where I would need the test to run in process so it can access the system in runtime, and this would be a requirement then.

@sheremet-va
Copy link
Member

@sheremet-va would the core team be willing to accept a PR to add an option that enables to run tests in main thread without going through tinypool.

I was trying to utilise vitest to build a runtime test suite system for an internal project, where I would need the test to run in process so it can access the system in runtime, and this would be a requirement then.

I already tried it, but communication was really slow: #1790 You can tackle it, if you want.

@koistya
Copy link

koistya commented May 26, 2023

Q: When Vitest workspaces are used, is there a way to set cwd for each workspace? Otherwise, the root cwd is used instead breaking the tests..

@sheremet-va
Copy link
Member

sheremet-va commented May 29, 2023

Q: When Vitest workspaces are used, is there a way to set cwd for each workspace? Otherwise, the root cwd is used instead breaking the tests..

@koistya you can mock it in your setup files:

vi.spyOn(process, 'cwd').mockReturnValue('/path/you/want')

Please, next time open a discussion instead of commenting in closes issues.

@vitest-dev vitest-dev locked as resolved and limited conversation to collaborators May 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants