|
| 1 | +--- |
| 2 | +title: Advanced API |
| 3 | +--- |
| 4 | + |
| 5 | +# Getting Started |
| 6 | + |
| 7 | +::: warning |
| 8 | +This guide lists advanced APIs to run tests via a Node.js script. If you just want to [run tests](/guide/), you probably don't need this. It is primarily used by library authors. |
| 9 | +::: |
| 10 | + |
| 11 | +You can import any method from the `vitest/node` entry-point. |
| 12 | + |
| 13 | +## startVitest |
| 14 | + |
| 15 | +```ts |
| 16 | +function startVitest( |
| 17 | + mode: VitestRunMode, |
| 18 | + cliFilters: string[] = [], |
| 19 | + options: CliOptions = {}, |
| 20 | + viteOverrides?: ViteUserConfig, |
| 21 | + vitestOptions?: VitestOptions, |
| 22 | +): Promise<Vitest> |
| 23 | +``` |
| 24 | + |
| 25 | +You can start running Vitest tests using its Node API: |
| 26 | + |
| 27 | +```js |
| 28 | +import { startVitest } from 'vitest/node' |
| 29 | +
|
| 30 | +const vitest = await startVitest('test') |
| 31 | +
|
| 32 | +await vitest.close() |
| 33 | +``` |
| 34 | + |
| 35 | +`startVitest` function returns [`Vitest`](/advanced/api/vitest) instance if tests can be started. |
| 36 | + |
| 37 | +If watch mode is not enabled, Vitest will call `close` method automatically. |
| 38 | + |
| 39 | +If watch mode is enabled and the terminal supports TTY, Vitest will register console shortcuts. |
| 40 | + |
| 41 | +You can pass down a list of filters as a second argument. Vitest will run only tests that contain at least one of the passed-down strings in their file path. |
| 42 | + |
| 43 | +Additionally, you can use the third argument to pass in CLI arguments, which will override any test config options. Alternatively, you can pass in the complete Vite config as the fourth argument, which will take precedence over any other user-defined options. |
| 44 | + |
| 45 | +After running the tests, you can get the results from the [`state.getTestModules`](/advanced/api/test-module) API: |
| 46 | + |
| 47 | +```ts |
| 48 | +import type { TestModule } from 'vitest/node' |
| 49 | +
|
| 50 | +const vitest = await startVitest('test') |
| 51 | +
|
| 52 | +console.log(vitest.state.getTestModules()) // [TestModule] |
| 53 | +``` |
| 54 | + |
| 55 | +::: tip |
| 56 | +The ["Running Tests"](/advanced/guide/tests#startvitest) guide has a usage example. |
| 57 | +::: |
| 58 | + |
| 59 | +## createVitest |
| 60 | + |
| 61 | +```ts |
| 62 | +function createVitest( |
| 63 | + mode: VitestRunMode, |
| 64 | + options: UserConfig, |
| 65 | + viteOverrides: ViteUserConfig = {}, |
| 66 | + vitestOptions: VitestOptions = {}, |
| 67 | +): Promise<Vitest> |
| 68 | +``` |
| 69 | + |
| 70 | +You can create Vitest instance by using `createVitest` function. It returns the same [`Vitest`](/advanced/api/vitest) instance as `startVitest`, but it doesn't start tests and doesn't validate installed packages. |
| 71 | + |
| 72 | +```js |
| 73 | +import { createVitest } from 'vitest/node' |
| 74 | +
|
| 75 | +const vitest = await createVitest('test', { |
| 76 | + watch: false, |
| 77 | +}) |
| 78 | +``` |
| 79 | + |
| 80 | +::: tip |
| 81 | +The ["Running Tests"](/advanced/guide/tests#createvitest) guide has a usage example. |
| 82 | +::: |
| 83 | + |
| 84 | +## resolveConfig |
| 85 | + |
| 86 | +```ts |
| 87 | +function resolveConfig( |
| 88 | + options: UserConfig = {}, |
| 89 | + viteOverrides: ViteUserConfig = {}, |
| 90 | +): Promise<{ |
| 91 | + vitestConfig: ResolvedConfig |
| 92 | + viteConfig: ResolvedViteConfig |
| 93 | +}> |
| 94 | +``` |
| 95 | + |
| 96 | +This method resolves the config with custom parameters. If no parameters are given, the `root` will be `process.cwd()`. |
| 97 | + |
| 98 | +```ts |
| 99 | +import { resolveConfig } from 'vitest/node' |
| 100 | +
|
| 101 | +// vitestConfig only has resolved "test" properties |
| 102 | +const { vitestConfig, viteConfig } = await resolveConfig({ |
| 103 | + mode: 'custom', |
| 104 | + configFile: false, |
| 105 | + resolve: { |
| 106 | + conditions: ['custom'] |
| 107 | + }, |
| 108 | + test: { |
| 109 | + setupFiles: ['/my-setup-file.js'], |
| 110 | + pool: 'threads', |
| 111 | + }, |
| 112 | +}) |
| 113 | +``` |
| 114 | + |
| 115 | +::: info |
| 116 | +Due to how Vite's `createServer` works, Vitest has to resolve the config during the plugin's `configResolve` hook. Therefore, this method is not actually used internally and is exposed exclusively as a public API. |
| 117 | + |
| 118 | +If you pass down the config to the `startVitest` or `createVitest` APIs, Vitest will still resolve the config again. |
| 119 | +::: |
| 120 | + |
| 121 | +::: warning |
| 122 | +The `resolveConfig` doesn't resolve the `workspace`. To resolve workspace configs, Vitest needs an established Vite server. |
| 123 | + |
| 124 | +Also note that `viteConfig.test` will not be fully resolved. If you need Vitest config, use `vitestConfig` instead. |
| 125 | +::: |
| 126 | + |
| 127 | +## parseCLI |
| 128 | + |
| 129 | +```ts |
| 130 | +function parseCLI(argv: string | string[], config: CliParseOptions = {}): { |
| 131 | + filter: string[] |
| 132 | + options: CliOptions |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +You can use this method to parse CLI arguments. It accepts a string (where arguments are split by a single space) or a strings array of CLI arguments in the same format that Vitest CLI uses. It returns a filter and `options` that you can later pass down to `createVitest` or `startVitest` methods. |
| 137 | + |
| 138 | +```ts |
| 139 | +import { parseCLI } from 'vitest/node' |
| 140 | +
|
| 141 | +const result = parseCLI('vitest ./files.ts --coverage --browser=chrome') |
| 142 | +
|
| 143 | +result.options |
| 144 | +// { |
| 145 | +// coverage: { enabled: true }, |
| 146 | +// browser: { name: 'chrome', enabled: true } |
| 147 | +// } |
| 148 | +
|
| 149 | +result.filter |
| 150 | +// ['./files.ts'] |
| 151 | +``` |
0 commit comments