Skip to content

Commit b092985

Browse files
sheremet-vadammy001
andauthoredAug 1, 2023
feat!: support running tests using VM context (#3203)
Co-authored-by: Anjorin Damilare <damilareanjorin1@gmail.com>
1 parent 5de9af2 commit b092985

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+2750
-587
lines changed
 

‎.github/workflows/ci.yml

+3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ jobs:
8484
- name: Test Single Thread
8585
run: pnpm run test:ci:single-thread
8686

87+
- name: Test Vm Threads
88+
run: pnpm run test:ci:vm-threads
89+
8790
test-ui:
8891
runs-on: ubuntu-latest
8992

‎docs/config/index.md

+72-2
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,11 @@ Handling for dependencies resolution.
144144
- **Type:** `(string | RegExp)[]`
145145
- **Default:** `[/\/node_modules\//]`
146146

147-
Externalize means that Vite will bypass the package to native Node. Externalized dependencies will not be applied Vite's transformers and resolvers, so they do not support HMR on reload. All packages under `node_modules` are externalized.
147+
Externalize means that Vite will bypass the package to the native Node. Externalized dependencies will not be applied to Vite's transformers and resolvers, so they do not support HMR on reload. By default, all packages inside `node_modules` are externalized.
148+
149+
These options support package names as they are written in `node_modules` or specified inside [`deps.moduleDirectories`](#deps-moduledirectories). For example, package `@company/some-name` located inside `packages/some-name` should be specified as `some-name`, and `packages` should be included in `deps.moduleDirectories`. Basically, Vitest always checks the file path, not the actual package name.
150+
151+
If regexp is used, Vitest calls it on the _file path_, not the package name.
148152

149153
#### server.deps.inline
150154

@@ -421,6 +425,7 @@ import type { Environment } from 'vitest'
421425

422426
export default <Environment>{
423427
name: 'custom',
428+
transformMode: 'ssr',
424429
setup() {
425430
// custom setup
426431
return {
@@ -468,7 +473,7 @@ export default defineConfig({
468473

469474
### poolMatchGlobs
470475

471-
- **Type:** `[string, 'browser' | 'threads' | 'child_process'][]`
476+
- **Type:** `[string, 'threads' | 'child_process' | 'experimentalVmThreads'][]`
472477
- **Default:** `[]`
473478
- **Version:** Since Vitest 0.29.4
474479

@@ -542,6 +547,69 @@ Custom reporters for output. Reporters can be [a Reporter instance](https://gith
542547
Write test results to a file when the `--reporter=json`, `--reporter=html` or `--reporter=junit` option is also specified.
543548
By providing an object instead of a string you can define individual outputs when using multiple reporters.
544549

550+
### experimentalVmThreads
551+
552+
- **Type:** `boolean`
553+
- **CLI:** `--experimentalVmThreads`, `--experimental-vm-threads`
554+
- **Version:** Since Vitest 0.34.0
555+
556+
Run tests using [VM context](https://nodejs.org/api/vm.html) (inside a sandboxed environment) in a worker pool.
557+
558+
This makes tests run faster, but the VM module is unstable when running [ESM code](https://github.com/nodejs/node/issues/37648). Your tests will [leak memory](https://github.com/nodejs/node/issues/33439) - to battle that, consider manually editing [`experimentalVmWorkerMemoryLimit`](#experimentalvmworkermemorylimit) value.
559+
560+
::: warning
561+
Running code in a sandbox has some advantages (faster tests), but also comes with a number of disadvantages.
562+
563+
- The globals within native modules, such as (`fs`, `path`, etc), differ from the globals present in your test environment. As a result, any error thrown by these native modules will reference a different Error constructor compared to the one used in your code:
564+
565+
```ts
566+
try {
567+
fs.writeFileSync('/doesnt exist')
568+
}
569+
catch (err) {
570+
console.log(err instanceof Error) // false
571+
}
572+
```
573+
574+
- Importing ES modules caches them indefinitely which introduces memory leaks if you have a lot of contexts (test files). There is no API in Node.js that clears that cache.
575+
- Accessing globals [takes longer](https://github.com/nodejs/node/issues/31658) in a sandbox environment.
576+
577+
Please, be aware of these issues when using this option. Vitest team cannot fix any of the issues on our side.
578+
:::
579+
580+
### experimentalVmWorkerMemoryLimit
581+
582+
- **Type:** `string | number`
583+
- **CLI:** `--experimentalVmWorkerMemoryLimit`, `--experimental-vm-worker-memory-limit`
584+
- **Default:** `1 / CPU Cores`
585+
- **Version:** Since Vitest 0.34.0
586+
587+
Specifies the memory limit for workers before they are recycled. This value heavily depends on your environment, so it's better to specify it manually instead of relying on the default.
588+
589+
This option only affects workers that run tests in [VM context](#experimentalvmthreads).
590+
591+
::: tip
592+
The implementation is based on Jest's [`workerIdleMemoryLimit`](https://jestjs.io/docs/configuration#workeridlememorylimit-numberstring).
593+
594+
The limit can be specified in a number of different ways and whatever the result is `Math.floor` is used to turn it into an integer value:
595+
596+
- `<= 1` - The value is assumed to be a percentage of system memory. So 0.5 sets the memory limit of the worker to half of the total system memory
597+
- `\> 1` - Assumed to be a fixed byte value. Because of the previous rule if you wanted a value of 1 byte (I don't know why) you could use 1.1.
598+
- With units
599+
- `50%` - As above, a percentage of total system memory
600+
- `100KB`, `65MB`, etc - With units to denote a fixed memory limit.
601+
- `K` / `KB` - Kilobytes (x1000)
602+
- `KiB` - Kibibytes (x1024)
603+
- `M` / `MB` - Megabytes
604+
- `MiB` - Mebibytes
605+
- `G` / `GB` - Gigabytes
606+
- `GiB` - Gibibytes
607+
:::
608+
609+
::: warning
610+
Percentage based memory limit [does not work on Linux CircleCI](https://github.com/jestjs/jest/issues/11956#issuecomment-1212925677) workers due to incorrect system memory being reported.
611+
:::
612+
545613
### threads
546614

547615
- **Type:** `boolean`
@@ -708,6 +776,8 @@ Make sure that your files are not excluded by `watchExclude`.
708776

709777
Isolate environment for each test file. Does not work if you disable [`--threads`](#threads).
710778

779+
This options has no effect on [`experimentalVmThreads`](#experimentalvmthreads).
780+
711781
### coverage<NonProjectOption />
712782

713783
You can use [`v8`](https://v8.dev/blog/javascript-code-coverage), [`istanbul`](https://istanbul.js.org/) or [a custom coverage solution](/guide/coverage#custom-coverage-provider) for coverage collection.

0 commit comments

Comments
 (0)
Please sign in to comment.