-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(browser): add commands to communicate betweens server and the br…
…owser (#5097)
- Loading branch information
1 parent
222ce44
commit aa431f4
Showing
23 changed files
with
683 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import type { ResolvedConfig } from 'vitest' | ||
|
||
export type BufferEncoding = | ||
| 'ascii' | ||
| 'utf8' | ||
| 'utf-8' | ||
| 'utf16le' | ||
| 'utf-16le' | ||
| 'ucs2' | ||
| 'ucs-2' | ||
| 'base64' | ||
| 'base64url' | ||
| 'latin1' | ||
| 'binary' | ||
| 'hex' | ||
|
||
export interface FsOptions { | ||
encoding?: BufferEncoding | ||
flag?: string | number | ||
} | ||
|
||
export interface TypePayload { type: string } | ||
export interface PressPayload { press: string } | ||
export interface DownPayload { down: string } | ||
export interface UpPayload { up: string } | ||
|
||
export type SendKeysPayload = TypePayload | PressPayload | DownPayload | UpPayload | ||
|
||
export interface BrowserCommands { | ||
readFile: (path: string, options?: BufferEncoding | FsOptions) => Promise<string> | ||
writeFile: (path: string, content: string, options?: BufferEncoding | FsOptions & { mode?: number | string }) => Promise<void> | ||
removeFile: (path: string) => Promise<void> | ||
sendKeys: (payload: SendKeysPayload) => Promise<void> | ||
} | ||
|
||
type Platform = | ||
| 'aix' | ||
| 'android' | ||
| 'darwin' | ||
| 'freebsd' | ||
| 'haiku' | ||
| 'linux' | ||
| 'openbsd' | ||
| 'sunos' | ||
| 'win32' | ||
| 'cygwin' | ||
| 'netbsd' | ||
|
||
export const server: { | ||
/** | ||
* Platform the Vitest server is running on. | ||
* The same as calling `process.platform` on the server. | ||
*/ | ||
platform: Platform | ||
/** | ||
* Runtime version of the Vitest server. | ||
* The same as calling `process.version` on the server. | ||
*/ | ||
version: string | ||
/** | ||
* Name of the browser provider. | ||
*/ | ||
provider: string | ||
/** | ||
* Available commands for the browser. | ||
* @see {@link https://vitest.dev/guide/browser#commands} | ||
*/ | ||
commands: BrowserCommands | ||
} | ||
|
||
/** | ||
* Available commands for the browser. | ||
* A shortcut to `server.commands`. | ||
* @see {@link https://vitest.dev/guide/browser#commands} | ||
*/ | ||
export const commands: BrowserCommands | ||
|
||
export const page: { | ||
/** | ||
* Serialized test config. | ||
*/ | ||
config: ResolvedConfig | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// empty file to not break bundling | ||
// Vitest resolves "@vitest/browser/context" as a virtual module instead |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import fs, { promises as fsp } from 'node:fs' | ||
import { dirname, resolve } from 'node:path' | ||
import { isFileServingAllowed } from 'vitest/node' | ||
import type { BrowserCommand, WorkspaceProject } from 'vitest/node' | ||
import type { BrowserCommands } from '../../../context' | ||
|
||
function assertFileAccess(path: string, project: WorkspaceProject) { | ||
if (!isFileServingAllowed(path, project.server) && !isFileServingAllowed(path, project.ctx.server)) | ||
throw new Error(`Access denied to "${path}". See Vite config documentation for "server.fs": https://vitejs.dev/config/server-options.html#server-fs-strict.`) | ||
} | ||
|
||
export const readFile: BrowserCommand<Parameters<BrowserCommands['readFile']>> = async ({ project, testPath = process.cwd() }, path, options = {}) => { | ||
const filepath = resolve(dirname(testPath), path) | ||
assertFileAccess(filepath, project) | ||
// never return a Buffer | ||
if (typeof options === 'object' && !options.encoding) | ||
options.encoding = 'utf-8' | ||
return fsp.readFile(filepath, options) | ||
} | ||
|
||
export const writeFile: BrowserCommand<Parameters<BrowserCommands['writeFile']>> = async ({ project, testPath = process.cwd() }, path, data, options) => { | ||
const filepath = resolve(dirname(testPath), path) | ||
assertFileAccess(filepath, project) | ||
const dir = dirname(filepath) | ||
if (!fs.existsSync(dir)) | ||
await fsp.mkdir(dir, { recursive: true }) | ||
await fsp.writeFile(filepath, data, options) | ||
} | ||
|
||
export const removeFile: BrowserCommand<Parameters<BrowserCommands['removeFile']>> = async ({ project, testPath = process.cwd() }, path) => { | ||
const filepath = resolve(dirname(testPath), path) | ||
assertFileAccess(filepath, project) | ||
await fsp.rm(filepath) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { | ||
readFile, | ||
removeFile, | ||
writeFile, | ||
} from './fs' | ||
import { sendKeys } from './keyboard' | ||
|
||
export default { | ||
readFile, | ||
removeFile, | ||
writeFile, | ||
sendKeys, | ||
} |
Oops, something went wrong.