@interfere/test-utils 1.0.1 → 9.0.1-canary.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/tags.d.mts +6 -0
- package/dist/lib/tags.mjs +1 -0
- package/dist/lib/viewports.d.mts +14 -0
- package/dist/lib/viewports.mjs +1 -0
- package/dist/playwright/base.d.mts +48 -0
- package/dist/playwright/base.mjs +1 -0
- package/dist/playwright/define-config.d.mts +6 -0
- package/dist/playwright/define-config.mjs +1 -0
- package/dist/playwright/protection-bypass.d.mts +5 -0
- package/dist/playwright/protection-bypass.mjs +1 -0
- package/dist/vitest/base.d.mts +51 -0
- package/dist/vitest/base.mjs +1 -0
- package/dist/vitest/define-config.d.mts +6 -0
- package/dist/vitest/define-config.mjs +1 -0
- package/dist/vitest/msw-browser.d.mts +5 -0
- package/dist/vitest/msw-browser.mjs +1 -0
- package/dist/vitest/msw-http.d.mts +15 -0
- package/dist/vitest/msw-http.mjs +1 -0
- package/dist/vitest/package-contract.d.mts +21 -0
- package/dist/vitest/package-contract.mjs +4 -0
- package/dist/vitest/presets/browser.d.mts +29 -0
- package/dist/vitest/presets/browser.mjs +1 -0
- package/dist/vitest/presets/integration.d.mts +36 -0
- package/dist/vitest/presets/integration.mjs +1 -0
- package/dist/vitest/presets/node.d.mts +15 -0
- package/dist/vitest/presets/node.mjs +1 -0
- package/dist/vitest/presets/vite.d.mts +15 -0
- package/dist/vitest/presets/vite.mjs +1 -0
- package/package.json +74 -14
- package/CHANGELOG.md +0 -7
- package/src/lib/deep-merge.ts +0 -71
- package/src/lib/tags.ts +0 -3
- package/src/lib/viewports.ts +0 -6
- package/src/playwright/base.ts +0 -44
- package/src/playwright/define-config.ts +0 -13
- package/src/vitest/base.ts +0 -50
- package/src/vitest/define-config.ts +0 -13
- package/src/vitest/msw-browser.ts +0 -18
- package/src/vitest/msw-http.ts +0 -60
- package/src/vitest/presets/browser.ts +0 -55
- package/src/vitest/presets/node.ts +0 -23
- package/tsconfig.json +0 -10
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const COMMON=`@common`,DESKTOP=`@desktop`,MOBILE=`@mobile`;export{COMMON,DESKTOP,MOBILE};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { TestUserConfig } from "vitest/config";
|
|
2
|
+
|
|
3
|
+
//#region src/lib/viewports.d.ts
|
|
4
|
+
type Viewport = NonNullable<TestUserConfig["browser"]>["viewport"];
|
|
5
|
+
declare const DESKTOP: {
|
|
6
|
+
readonly width: 1280;
|
|
7
|
+
readonly height: 720;
|
|
8
|
+
};
|
|
9
|
+
declare const MOBILE: {
|
|
10
|
+
readonly width: 390;
|
|
11
|
+
readonly height: 844;
|
|
12
|
+
};
|
|
13
|
+
//#endregion
|
|
14
|
+
export { DESKTOP, MOBILE, Viewport };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const DESKTOP={width:1280,height:720},MOBILE={width:390,height:844};export{DESKTOP,MOBILE};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
//#region src/playwright/base.d.ts
|
|
2
|
+
declare const _default: {
|
|
3
|
+
fullyParallel: true;
|
|
4
|
+
forbidOnly: boolean;
|
|
5
|
+
retries: number;
|
|
6
|
+
reporter: ["html"][] | (["github"] | ["junit", {
|
|
7
|
+
outputFile: string;
|
|
8
|
+
}])[];
|
|
9
|
+
use: {
|
|
10
|
+
trace: "on-first-retry";
|
|
11
|
+
screenshot: "only-on-failure";
|
|
12
|
+
actionTimeout?: number;
|
|
13
|
+
launchOptions?: {
|
|
14
|
+
executablePath: string;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
expect?: {
|
|
18
|
+
timeout: number;
|
|
19
|
+
};
|
|
20
|
+
projects: ({
|
|
21
|
+
name: string;
|
|
22
|
+
grep: RegExp;
|
|
23
|
+
use: {
|
|
24
|
+
viewport: import("@playwright/test").ViewportSize;
|
|
25
|
+
userAgent: string;
|
|
26
|
+
deviceScaleFactor: number;
|
|
27
|
+
isMobile: boolean;
|
|
28
|
+
hasTouch: boolean;
|
|
29
|
+
defaultBrowserType: "chromium" | "firefox" | "webkit";
|
|
30
|
+
};
|
|
31
|
+
} | {
|
|
32
|
+
name: string;
|
|
33
|
+
grep: RegExp;
|
|
34
|
+
use: {
|
|
35
|
+
userAgent: string;
|
|
36
|
+
deviceScaleFactor: number;
|
|
37
|
+
defaultBrowserType: "chromium" | "firefox" | "webkit";
|
|
38
|
+
viewport: {
|
|
39
|
+
readonly width: 390;
|
|
40
|
+
readonly height: 844;
|
|
41
|
+
};
|
|
42
|
+
isMobile: true;
|
|
43
|
+
hasTouch: true;
|
|
44
|
+
};
|
|
45
|
+
})[];
|
|
46
|
+
};
|
|
47
|
+
//#endregion
|
|
48
|
+
export { _default as default };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{COMMON,DESKTOP,MOBILE}from"../lib/tags.mjs";import{MOBILE as MOBILE$1}from"../lib/viewports.mjs";import{devices}from"@playwright/test";const ci=!!process.env.CI,chromiumExecutablePath=process.env.CHROMIUM_EXECUTABLE_PATH;var base_default={fullyParallel:!0,forbidOnly:ci,retries:ci?2:0,reporter:ci?[[`github`],[`junit`,{outputFile:`./test-results/e2e-junit.xml`}]]:[[`html`]],use:{trace:`on-first-retry`,screenshot:`only-on-failure`,...ci&&{actionTimeout:1e4},...chromiumExecutablePath&&{launchOptions:{executablePath:chromiumExecutablePath}}},...ci&&{expect:{timeout:1e4}},projects:[{name:`desktop`,grep:RegExp(`${DESKTOP}|${COMMON}`),use:{...devices[`Desktop Chrome`]}},{name:`mobile`,grep:RegExp(`${MOBILE}|${COMMON}`),use:{...devices[`Desktop Chrome`],viewport:MOBILE$1,isMobile:!0,hasTouch:!0}}]};export{base_default as default};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { PlaywrightTestConfig } from "@playwright/test";
|
|
2
|
+
|
|
3
|
+
//#region src/playwright/define-config.d.ts
|
|
4
|
+
declare function defineConfig(...overrides: Array<Partial<PlaywrightTestConfig> | undefined>): PlaywrightTestConfig<{}, {}>;
|
|
5
|
+
//#endregion
|
|
6
|
+
export { defineConfig };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import base_default from"./base.mjs";import{defineConfig as defineConfig$1}from"@playwright/test";import{deepMerge}from"@interfere/helpers/deep-merge";function defineConfig(...overrides){return defineConfig$1(deepMerge(base_default,...overrides))}export{defineConfig};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
//#region src/playwright/protection-bypass.d.ts
|
|
2
|
+
type ProtectionProvider = "cloudflare-access";
|
|
3
|
+
declare function protectionBypassHeaders(baseURL: string | undefined, provider: ProtectionProvider): Record<string, string> | undefined;
|
|
4
|
+
//#endregion
|
|
5
|
+
export { ProtectionProvider, protectionBypassHeaders };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const LOCAL_HOSTNAMES=new Set([`localhost`,`127.0.0.1`,`0.0.0.0`,`[::1]`]);function isLocal(baseURL){try{return LOCAL_HOSTNAMES.has(new URL(baseURL).hostname)}catch{return!1}}function cloudflareAccessHeaders(baseURL){let clientId=process.env.CF_ACCESS_CLIENT_ID,clientSecret=process.env.CF_ACCESS_CLIENT_SECRET;if(!(clientId&&clientSecret))throw Error([`Missing CF_ACCESS_CLIENT_ID / CF_ACCESS_CLIENT_SECRET while BASE_URL points at a remote Cloudflare deployment.`,`BASE_URL=${baseURL}`,"Playwright cannot bypass Cloudflare Access without the `cf-iac-ci` service token.","Fetch both values from GSM: `cf-access-service-token-id` and `cf-access-service-token-secret`."].join(` `));return{"CF-Access-Client-Id":clientId,"CF-Access-Client-Secret":clientSecret}}function protectionBypassHeaders(baseURL,provider){if(!(!baseURL||isLocal(baseURL))){if(provider===`cloudflare-access`)return cloudflareAccessHeaders(baseURL);throw Error(`Unsupported protection provider: ${provider}`)}}export{protectionBypassHeaders};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
//#region src/vitest/base.d.ts
|
|
2
|
+
declare const _default: {
|
|
3
|
+
root: string;
|
|
4
|
+
resolve: {
|
|
5
|
+
conditions: string[];
|
|
6
|
+
};
|
|
7
|
+
ssr: {
|
|
8
|
+
resolve: {
|
|
9
|
+
conditions: string[];
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
test: {
|
|
13
|
+
env: {
|
|
14
|
+
NODE_ENV: string;
|
|
15
|
+
};
|
|
16
|
+
environment: "node";
|
|
17
|
+
globals: true;
|
|
18
|
+
isolate: true;
|
|
19
|
+
pool: "forks";
|
|
20
|
+
maxWorkers: string;
|
|
21
|
+
clearMocks: true;
|
|
22
|
+
restoreMocks: true;
|
|
23
|
+
ui: boolean;
|
|
24
|
+
unstubEnvs: true;
|
|
25
|
+
unstubGlobals: true;
|
|
26
|
+
sequence: {
|
|
27
|
+
shuffle: boolean;
|
|
28
|
+
};
|
|
29
|
+
server: {
|
|
30
|
+
deps: {
|
|
31
|
+
inline: string[];
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
include: string[];
|
|
35
|
+
exclude: string[];
|
|
36
|
+
reporters: ("default" | "junit")[];
|
|
37
|
+
coverage: {
|
|
38
|
+
enabled: true;
|
|
39
|
+
provider: "v8";
|
|
40
|
+
include: string[];
|
|
41
|
+
exclude: string[];
|
|
42
|
+
reporter: ("json" | "json-summary" | "lcov")[] | ("clover" | "html" | "json" | "text")[];
|
|
43
|
+
};
|
|
44
|
+
setupFiles?: string[];
|
|
45
|
+
outputFile?: {
|
|
46
|
+
junit: string;
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
//#endregion
|
|
51
|
+
export { _default as default };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{omitUndefined}from"@interfere/helpers/omit-undefined";import{existsSync}from"node:fs";import{resolve}from"node:path";import{config}from"dotenv";import{configDefaults}from"vitest/config";const root=process.cwd(),isCI=!!process.env.CI,env=config({path:resolve(root,`.env.test`),quiet:!0}).parsed??{},setupFiles=[`./test/setup.ts`,`./test/unit/setup.ts`].filter(rel=>existsSync(resolve(root,rel)));var base_default={root,resolve:{conditions:[`monorepo`]},ssr:{resolve:{conditions:[`monorepo`]}},test:{env:{...env,NODE_ENV:`test`},environment:`node`,globals:!0,isolate:!0,pool:`forks`,maxWorkers:`50%`,clearMocks:!0,restoreMocks:!0,ui:!!process.env.UI,unstubEnvs:!0,unstubGlobals:!0,sequence:{shuffle:!isCI},server:{deps:{inline:[`zod`]}},include:[`src/**/*.{test,spec}.?(c|m)[jt]s?(x)`,`test/**/*.{test,spec}.?(c|m)[jt]s?(x)`],exclude:[`test/e2e/**/*.{test,spec}.?(c|m)[jt]s?(x)`,...configDefaults.exclude],reporters:isCI?[`default`,`junit`]:[`default`],coverage:{enabled:!0,provider:`v8`,include:[`src/**/*.{ts,tsx,js,jsx,mts,cts,mjs,cjs}`],exclude:[`**/*.json`,...configDefaults.coverage?.exclude??[]],reporter:isCI?[`json`,`json-summary`,`lcov`]:[`text`,`html`,`clover`,`json`]},...omitUndefined({setupFiles:setupFiles.length>0?setupFiles:void 0,outputFile:isCI?{junit:`./test-results/junit.xml`}:void 0})}};export{base_default as default};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import base_default from"./base.mjs";import{deepMerge}from"@interfere/helpers/deep-merge";import{defineConfig as defineConfig$1}from"vitest/config";function defineConfig(...overrides){return defineConfig$1(deepMerge(base_default,...overrides))}export{defineConfig};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{setupWorker}from"msw/browser";import{afterAll,afterEach,beforeAll}from"vitest";const worker=setupWorker();function enableMSW(){beforeAll(async()=>{await worker.start({onUnhandledRequest:`bypass`,quiet:!0})}),afterEach(()=>{worker.resetHandlers()}),afterAll(()=>{worker.stop()})}export{enableMSW,worker};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//#region src/vitest/msw-http.d.ts
|
|
2
|
+
type HttpMethod = "post" | "get" | "put" | "delete" | "patch";
|
|
3
|
+
type JsonValue = string | number | boolean | null | JsonValue[] | {
|
|
4
|
+
[key: string]: JsonValue;
|
|
5
|
+
};
|
|
6
|
+
interface CapturedHttpCall {
|
|
7
|
+
body: unknown;
|
|
8
|
+
params: Record<string, string | readonly string[] | undefined>;
|
|
9
|
+
}
|
|
10
|
+
declare function createHttpInterceptor(baseUrl: string): {
|
|
11
|
+
intercept: (method: HttpMethod, path: string, response: JsonValue, status?: number) => CapturedHttpCall[];
|
|
12
|
+
interceptNetworkError: (method: HttpMethod, path: string) => void;
|
|
13
|
+
};
|
|
14
|
+
//#endregion
|
|
15
|
+
export { CapturedHttpCall, HttpMethod, JsonValue, createHttpInterceptor };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{worker}from"./msw-browser.mjs";import{HttpResponse,http}from"msw";async function parseBody(request){let clone=request.clone();if((clone.headers.get(`content-type`)??``).includes(`multipart/form-data`)){let formData=await clone.formData();return Object.fromEntries(formData.entries())}return clone.json().catch(()=>null)}function createHttpInterceptor(baseUrl){function intercept(method,path,response,status=200){let calls=[];return worker.use(http[method](`${baseUrl}${path}`,async({request,params})=>(calls.push({body:await parseBody(request),params}),HttpResponse.json(response,{status})))),calls}function interceptNetworkError(method,path){worker.use(http[method](`${baseUrl}${path}`,()=>HttpResponse.error()))}return{intercept,interceptNetworkError}}export{createHttpInterceptor};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region src/vitest/package-contract.d.ts
|
|
2
|
+
declare const PACKAGE_CONTRACT_TIMEOUT_MS = 30000;
|
|
3
|
+
interface PackageContractOptions {
|
|
4
|
+
packageJsonUrl: URL;
|
|
5
|
+
}
|
|
6
|
+
interface PackedPackageFilesOptions extends PackageContractOptions {
|
|
7
|
+
expectedFiles: string[];
|
|
8
|
+
}
|
|
9
|
+
interface NodePackageResolutionOptions extends PackageContractOptions {
|
|
10
|
+
specifiers: string[];
|
|
11
|
+
}
|
|
12
|
+
declare function expectPackedPackageFiles({
|
|
13
|
+
expectedFiles,
|
|
14
|
+
packageJsonUrl
|
|
15
|
+
}: PackedPackageFilesOptions): Promise<void>;
|
|
16
|
+
declare function expectNodeCanLoadPackageSpecifiers({
|
|
17
|
+
packageJsonUrl,
|
|
18
|
+
specifiers
|
|
19
|
+
}: NodePackageResolutionOptions): Promise<void>;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { PACKAGE_CONTRACT_TIMEOUT_MS, expectNodeCanLoadPackageSpecifiers, expectPackedPackageFiles };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import{dirname,join}from"node:path";import{expect}from"vitest";import{execFile}from"node:child_process";import{mkdir,mkdtemp,readFile,rename,rm,symlink}from"node:fs/promises";import{tmpdir}from"node:os";import{fileURLToPath}from"node:url";import{promisify}from"node:util";const execFileAsync=promisify(execFile),PACKED_LINE=/^packed\s+\S+\s+(.+)$/,TGZ_LINE=/^(\S+\.tgz)$/,PACKAGE_CONTRACT_TIMEOUT_MS=3e4;async function expectPackedPackageFiles({expectedFiles,packageJsonUrl}){let packedPackage=await createPackedPackage(packageJsonUrl);try{expect(packedPackage.files).toEqual(expect.arrayContaining(expectedFiles)),expect(packedPackage.files.filter(path=>path.startsWith(`src/`))).toEqual([]),expect(packedPackage.files.filter(path=>path.endsWith(`.map`))).toEqual([]),expect(packedPackage.packageJson.private).toBeUndefined(),expect(Object.keys(packedPackage.packageJson.devDependencies??{})).not.toContain(`@interfere/tsdown-config`)}finally{await packedPackage.cleanup()}}async function expectNodeCanLoadPackageSpecifiers({packageJsonUrl,specifiers}){let packedPackage=await createPackedPackage(packageJsonUrl);try{await runNodeScript({args:[`--input-type=module`,`--eval`,importScript(specifiers)],cwd:packedPackage.consumerDir,label:`ESM import`}),await runNodeScript({args:[`--eval`,requireScript(specifiers)],cwd:packedPackage.consumerDir,label:`CommonJS require`})}finally{await packedPackage.cleanup()}}async function createPackedPackage(packageJsonUrl){let packageDir=dirname(fileURLToPath(packageJsonUrl)),tempDir=await mkdtemp(join(tmpdir(),`interfere-package-contract-`)),consumerDir=join(tempDir,`consumer`),nodeModulesDir=join(consumerDir,`node_modules`),packageJson=JSON.parse(await readFile(packageJsonUrl,`utf8`)),installedPackageDir=joinPackageName(nodeModulesDir,packageJson.name);await mkdir(dirname(installedPackageDir),{recursive:!0});let{stdout}=await execFileAsync(`bun`,[`pm`,`pack`,`--ignore-scripts`],{cwd:packageDir}),{filename,files}=parseBunPackOutput(stdout),tarballPath=join(packageDir,filename),extractedDir=join(tempDir,`extracted`);await mkdir(extractedDir),await execFileAsync(`tar`,[`-xzf`,tarballPath,`-C`,extractedDir]),await rm(tarballPath),await rename(join(extractedDir,`package`),installedPackageDir),await linkRuntimeDependencies({nodeModulesDir,packageDir,packageJson});let packedPackageJson=JSON.parse(await readFile(join(installedPackageDir,`package.json`),`utf8`));return{cleanup:()=>rm(tempDir,{force:!0,recursive:!0}),consumerDir,files:files.toSorted(),packageJson:packedPackageJson}}function parseBunPackOutput(stdout){let files=[],filename=``;for(let line of stdout.split(`
|
|
2
|
+
`)){let packMatch=line.match(PACKED_LINE);if(packMatch?.[1]){files.push(packMatch[1]);continue}let tgzMatch=line.match(TGZ_LINE);tgzMatch?.[1]&&(filename=tgzMatch[1])}if(!filename)throw Error(`bun pm pack did not produce a tarball:\n${stdout}`);return{filename,files}}async function linkRuntimeDependencies({nodeModulesDir,packageDir,packageJson}){let dependencies=new Set([...Object.keys(packageJson.dependencies??{}),...Object.keys(packageJson.peerDependencies??{})]);for(let dependencyName of Array.from(dependencies)){if(dependencyName===packageJson.name)continue;let source=joinPackageName(join(packageDir,`node_modules`),dependencyName),target=joinPackageName(nodeModulesDir,dependencyName);await mkdir(dirname(target),{recursive:!0}),await symlink(source,target,`junction`)}}function joinPackageName(nodeModulesDir,packageName){return join(nodeModulesDir,...packageName.split(`/`))}async function runNodeScript({args,cwd,label}){try{await execFileAsync(process.execPath,args,{cwd})}catch(error){throw error instanceof Error?Error(`${label} package contract failed:\n${error.message}`):error}}function importScript(specifiers){return specifiers.map(specifier=>`await import(${JSON.stringify(specifier)});`).join(`
|
|
3
|
+
`)}function requireScript(specifiers){return specifiers.map(specifier=>`require(${JSON.stringify(specifier)});`).join(`
|
|
4
|
+
`)}export{PACKAGE_CONTRACT_TIMEOUT_MS,expectNodeCanLoadPackageSpecifiers,expectPackedPackageFiles};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { TestUserConfig, UserWorkspaceConfig } from "vitest/config";
|
|
2
|
+
|
|
3
|
+
//#region src/vitest/presets/browser.d.ts
|
|
4
|
+
type BrowserConfig = NonNullable<TestUserConfig["browser"]>;
|
|
5
|
+
type BrowserInstance = NonNullable<BrowserConfig["instances"]>[number];
|
|
6
|
+
interface BrowserProjectOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Playwright browser instances to run against. Use `dualViewport()` for the
|
|
9
|
+
* standard Desktop + Mobile split keyed on the `src/**/mobile/**` glob
|
|
10
|
+
* convention.
|
|
11
|
+
*/
|
|
12
|
+
instances: BrowserInstance[];
|
|
13
|
+
testAlias?: string | false;
|
|
14
|
+
trace?: boolean;
|
|
15
|
+
[key: string]: unknown;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Glob matching all tests under a `mobile/` directory. Used by `dualViewport`
|
|
19
|
+
* to split the Desktop / Mobile test runs from a single file tree.
|
|
20
|
+
*/
|
|
21
|
+
declare const MOBILE_TESTS = "src/**/mobile/*.{test,spec}.?(c|m)[jt]s?(x)";
|
|
22
|
+
/**
|
|
23
|
+
* Standard Desktop + Mobile viewport split for UI apps. Desktop runs every
|
|
24
|
+
* test except those under `src/**/mobile/**`; Mobile runs only those.
|
|
25
|
+
*/
|
|
26
|
+
declare function dualViewport(): BrowserInstance[];
|
|
27
|
+
declare function browserProject(opts: BrowserProjectOptions): UserWorkspaceConfig;
|
|
28
|
+
//#endregion
|
|
29
|
+
export { BrowserInstance, BrowserProjectOptions, MOBILE_TESTS, browserProject, dualViewport };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{DESKTOP,MOBILE}from"../../lib/viewports.mjs";import base_default from"../base.mjs";import{deepMerge}from"@interfere/helpers/deep-merge";import{resolve}from"node:path";import react from"@vitejs/plugin-react";import{playwright}from"@vitest/browser-playwright";const INSTANCE_BASELINE_EXCLUDE=[`test/e2e/**/*.{test,spec}.?(c|m)[jt]s?(x)`],chromiumExecutablePath=process.env.CHROMIUM_EXECUTABLE_PATH,MOBILE_TESTS=`src/**/mobile/*.{test,spec}.?(c|m)[jt]s?(x)`;function dualViewport(){return[{browser:`chromium`,name:`Chrome - Desktop`,viewport:DESKTOP,exclude:[...INSTANCE_BASELINE_EXCLUDE,MOBILE_TESTS]},{browser:`chromium`,name:`Chrome - Mobile`,viewport:MOBILE,exclude:INSTANCE_BASELINE_EXCLUDE,include:[MOBILE_TESTS]}]}function browserProject(opts){let{instances,trace,testAlias=`test/unit`,...rest}=opts;return deepMerge(base_default,{server:{port:0,strictPort:!1},test:{name:`browser`,browser:{enabled:!0,provider:chromiumExecutablePath?playwright({launchOptions:{executablePath:chromiumExecutablePath}}):playwright(),screenshotFailures:!1,instances,...trace&&{trace:{mode:`retain-on-failure`}}}},plugins:[react()],optimizeDeps:{entries:[`src/**/*.{ts,tsx}`,`test/unit/**/*.{ts,tsx}`],exclude:[`fsevents`,`@opentelemetry/otlp-transformer`]},...testAlias!==!1&&{resolve:{tsconfigPaths:!0,alias:{"@test":resolve(process.cwd(),testAlias)}}}},rest)}export{MOBILE_TESTS,browserProject,dualViewport};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { ViteUserConfig } from "vitest/config";
|
|
2
|
+
|
|
3
|
+
//#region src/vitest/presets/integration.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Integration test resources. Each entry maps to a globalSetup module in
|
|
6
|
+
* `@interfere/internal-test-utils` that owns a single shared container for
|
|
7
|
+
* the duration of the run and tears it down on success, failure, and most
|
|
8
|
+
* signal scenarios.
|
|
9
|
+
*
|
|
10
|
+
* The matching per-worker harness lives at
|
|
11
|
+
* `@interfere/internal-test-utils/lifecycle/<resource>/harness` — import it
|
|
12
|
+
* from your `test/setup.ts`.
|
|
13
|
+
*/
|
|
14
|
+
type IntegrationResource = "pg" | "redis";
|
|
15
|
+
interface IntegrationOptions {
|
|
16
|
+
/**
|
|
17
|
+
* Container resources to provision once per run. Defaults to `["pg"]`.
|
|
18
|
+
*/
|
|
19
|
+
resources?: IntegrationResource[];
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Build a Vitest config for an integration-test package. Wires up the
|
|
23
|
+
* shared-container globalSetup modules so each test run spins up exactly
|
|
24
|
+
* one of each requested resource (instead of one per worker fork) and tears
|
|
25
|
+
* them down reliably on exit.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* export default defineIntegrationConfig({ resources: ["pg", "redis"] }, {
|
|
30
|
+
* test: { coverage: { provider: "v8" } },
|
|
31
|
+
* });
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
declare function defineIntegrationConfig(options?: IntegrationOptions, ...overrides: Array<Partial<ViteUserConfig> | undefined>): ViteUserConfig;
|
|
35
|
+
//#endregion
|
|
36
|
+
export { IntegrationOptions, IntegrationResource, defineIntegrationConfig };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import base_default from"../base.mjs";import{deepMerge}from"@interfere/helpers/deep-merge";import{defineConfig}from"vitest/config";const GLOBAL_SETUP_BY_RESOURCE={pg:`@interfere/internal-test-utils/lifecycle/pg/global-setup`,redis:`@interfere/internal-test-utils/lifecycle/redis/global-setup`};function defineIntegrationConfig(options={},...overrides){let resources=options.resources??[`pg`],globalSetup=resources.map(r=>GLOBAL_SETUP_BY_RESOURCE[r]),redisOverrides=resources.includes(`redis`)?{test:{fileParallelism:!1}}:{};return defineConfig(deepMerge(base_default,{test:{globalSetup}},redisOverrides,...overrides))}export{defineIntegrationConfig};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { UserWorkspaceConfig } from "vitest/config";
|
|
2
|
+
|
|
3
|
+
//#region src/vitest/presets/node.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Build a Vitest project config running against the Node environment.
|
|
6
|
+
* Spreads `opts` over a shared base; caller-provided fields win.
|
|
7
|
+
*
|
|
8
|
+
* `*.test.{tsx,jsx}` files are excluded by default — React test files can't
|
|
9
|
+
* import `vitest-browser-react` outside of Browser Mode, so they always
|
|
10
|
+
* belong in the `browserProject`. Opt a specific TSX test in by adding its
|
|
11
|
+
* path to the caller's `test.include`.
|
|
12
|
+
*/
|
|
13
|
+
declare function nodeProject(opts?: Partial<UserWorkspaceConfig>): UserWorkspaceConfig;
|
|
14
|
+
//#endregion
|
|
15
|
+
export { nodeProject };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import base_default from"../base.mjs";import{deepMerge}from"@interfere/helpers/deep-merge";function nodeProject(opts={}){return deepMerge(base_default,{test:{name:`node`,environment:`node`,exclude:[`**/*.{test,spec}.tsx`,`**/*.{test,spec}.jsx`,`**/*.{test,spec}.mtsx`,`**/*.{test,spec}.mjsx`]}},opts)}export{nodeProject};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { TestUserConfig, UserWorkspaceConfig } from "vitest/config";
|
|
2
|
+
|
|
3
|
+
//#region src/vitest/presets/vite.d.ts
|
|
4
|
+
type BrowserConfig = NonNullable<TestUserConfig["browser"]>;
|
|
5
|
+
type BrowserInstance = NonNullable<BrowserConfig["instances"]>[number];
|
|
6
|
+
interface BrowserProjectOptions {
|
|
7
|
+
instances: BrowserInstance[];
|
|
8
|
+
testAlias?: string | false;
|
|
9
|
+
trace?: boolean;
|
|
10
|
+
[key: string]: unknown;
|
|
11
|
+
}
|
|
12
|
+
declare function dualViewport(): BrowserInstance[];
|
|
13
|
+
declare function viteProject(opts: BrowserProjectOptions): UserWorkspaceConfig;
|
|
14
|
+
//#endregion
|
|
15
|
+
export { BrowserInstance, BrowserProjectOptions, dualViewport, viteProject };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{DESKTOP,MOBILE}from"../../lib/viewports.mjs";import base_default from"../base.mjs";import{deepMerge}from"@interfere/helpers/deep-merge";import{playwright}from"@vitest/browser-playwright";const INSTANCE_BASELINE_EXCLUDE=[`test/e2e/**/*.{test,spec}.?(c|m)[jt]s?(x)`],chromiumExecutablePath=process.env.CHROMIUM_EXECUTABLE_PATH;function dualViewport(){return[{browser:`chromium`,name:`Chrome - Desktop`,viewport:DESKTOP,exclude:INSTANCE_BASELINE_EXCLUDE},{browser:`chromium`,name:`Chrome - Mobile`,viewport:MOBILE,exclude:INSTANCE_BASELINE_EXCLUDE}]}function viteProject(opts){let{instances,trace,testAlias=`test/unit`,...rest}=opts;return deepMerge({server:{port:0,strictPort:!1}},base_default,{test:{name:`vite`,browser:{enabled:!0,provider:chromiumExecutablePath?playwright({launchOptions:{executablePath:chromiumExecutablePath}}):playwright(),screenshotFailures:!1,instances,...trace&&{trace:{mode:`retain-on-failure`}}}}},rest)}export{dualViewport,viteProject};
|
package/package.json
CHANGED
|
@@ -1,26 +1,86 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@interfere/test-utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "9.0.1-canary.0",
|
|
4
4
|
"license": "MIT",
|
|
5
|
-
"publishConfig": {
|
|
6
|
-
"access": "public"
|
|
7
|
-
},
|
|
8
5
|
"description": "Shared test utilities used across Interfere packages.",
|
|
6
|
+
"homepage": "https://interfere.com",
|
|
7
|
+
"bugs": {
|
|
8
|
+
"url": "mailto:support@interfere.com"
|
|
9
|
+
},
|
|
10
|
+
"author": "Interfere <support@interfere.com> (https://interfere.com)",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://interfere.ghe.com/interfere/interfere.git",
|
|
14
|
+
"directory": "src/packages/public/test-utils"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
9
19
|
"type": "module",
|
|
10
20
|
"exports": {
|
|
11
|
-
"
|
|
21
|
+
"./lib/*": {
|
|
22
|
+
"monorepo": "./src/lib/*.ts",
|
|
23
|
+
"types": "./dist/lib/*.d.mts",
|
|
24
|
+
"default": "./dist/lib/*.mjs"
|
|
25
|
+
},
|
|
26
|
+
"./playwright/*": {
|
|
27
|
+
"monorepo": "./src/playwright/*.ts",
|
|
28
|
+
"types": "./dist/playwright/*.d.mts",
|
|
29
|
+
"default": "./dist/playwright/*.mjs"
|
|
30
|
+
},
|
|
31
|
+
"./vitest/*": {
|
|
32
|
+
"monorepo": "./src/vitest/*.ts",
|
|
33
|
+
"types": "./dist/vitest/*.d.mts",
|
|
34
|
+
"default": "./dist/vitest/*.mjs"
|
|
35
|
+
},
|
|
36
|
+
"./vitest/presets/*": {
|
|
37
|
+
"monorepo": "./src/vitest/presets/*.ts",
|
|
38
|
+
"types": "./dist/vitest/presets/*.d.mts",
|
|
39
|
+
"default": "./dist/vitest/presets/*.mjs"
|
|
40
|
+
},
|
|
41
|
+
"./package.json": "./package.json"
|
|
42
|
+
},
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public"
|
|
12
45
|
},
|
|
13
46
|
"scripts": {
|
|
14
|
-
"
|
|
47
|
+
"build": "tsdown",
|
|
48
|
+
"typecheck": "tsgo --noEmit"
|
|
49
|
+
},
|
|
50
|
+
"peerDependencies": {
|
|
51
|
+
"@playwright/test": "^1.61.1",
|
|
52
|
+
"@vitejs/plugin-react": "^6.0.3",
|
|
53
|
+
"@vitest/browser-playwright": "4.1.9",
|
|
54
|
+
"msw": "^2.14.6",
|
|
55
|
+
"vitest": "4.1.9"
|
|
56
|
+
},
|
|
57
|
+
"peerDependenciesMeta": {
|
|
58
|
+
"@playwright/test": {
|
|
59
|
+
"optional": true
|
|
60
|
+
},
|
|
61
|
+
"@vitejs/plugin-react": {
|
|
62
|
+
"optional": true
|
|
63
|
+
},
|
|
64
|
+
"@vitest/browser-playwright": {
|
|
65
|
+
"optional": true
|
|
66
|
+
},
|
|
67
|
+
"msw": {
|
|
68
|
+
"optional": true
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
"dependencies": {
|
|
72
|
+
"@interfere/helpers": "^9.0.0",
|
|
73
|
+
"dotenv": "^17.4.2"
|
|
15
74
|
},
|
|
16
75
|
"devDependencies": {
|
|
17
|
-
"@interfere/typescript-config": "^
|
|
18
|
-
"@playwright/test": "^1.
|
|
19
|
-
"@types/node": "^
|
|
20
|
-
"@
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"
|
|
76
|
+
"@interfere/typescript-config": "^9.0.0",
|
|
77
|
+
"@playwright/test": "^1.61.1",
|
|
78
|
+
"@types/node": "^26.0.0",
|
|
79
|
+
"@typescript/native-preview": "7.0.0-dev.20260623.1",
|
|
80
|
+
"@vitejs/plugin-react": "^6.0.3",
|
|
81
|
+
"@vitest/browser-playwright": "4.1.9",
|
|
82
|
+
"msw": "^2.14.6",
|
|
83
|
+
"tsdown": "^0.22.2",
|
|
84
|
+
"vitest": "4.1.9"
|
|
25
85
|
}
|
|
26
86
|
}
|
package/CHANGELOG.md
DELETED
package/src/lib/deep-merge.ts
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
2
|
-
return (
|
|
3
|
-
typeof value === "object" &&
|
|
4
|
-
value !== null &&
|
|
5
|
-
!Array.isArray(value) &&
|
|
6
|
-
Object.getPrototypeOf(value) === Object.prototype
|
|
7
|
-
);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
function cloneValue<T>(value: T): T {
|
|
11
|
-
if (Array.isArray(value)) {
|
|
12
|
-
return value.map((item) => cloneValue(item)) as T;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
if (isPlainObject(value)) {
|
|
16
|
-
const out: Record<string, unknown> = {};
|
|
17
|
-
for (const [key, nested] of Object.entries(value)) {
|
|
18
|
-
out[key] = cloneValue(nested);
|
|
19
|
-
}
|
|
20
|
-
return out as T;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return value;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function mergeValues(base: unknown, override: unknown): unknown {
|
|
27
|
-
if (typeof override === "undefined") {
|
|
28
|
-
return cloneValue(base);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if (Array.isArray(base) || Array.isArray(override)) {
|
|
32
|
-
const baseArr = Array.isArray(base) ? base : [];
|
|
33
|
-
const overrideArr = Array.isArray(override) ? override : [];
|
|
34
|
-
return [...baseArr, ...overrideArr].map((item) => cloneValue(item));
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (isPlainObject(base) && isPlainObject(override)) {
|
|
38
|
-
const out: Record<string, unknown> = {};
|
|
39
|
-
const keys = new Set(Object.keys(base)).union(
|
|
40
|
-
new Set(Object.keys(override))
|
|
41
|
-
);
|
|
42
|
-
for (const key of keys) {
|
|
43
|
-
out[key] = mergeValues(base[key], override[key]);
|
|
44
|
-
}
|
|
45
|
-
return out;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return cloneValue(override);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Deep-merge any number of config objects.
|
|
53
|
-
* - Plain objects merge recursively.
|
|
54
|
-
* - Arrays are concatenated (base first, then overrides).
|
|
55
|
-
* - Primitives take the rightmost value.
|
|
56
|
-
*/
|
|
57
|
-
export function deepMerge<T extends object>(
|
|
58
|
-
base: T,
|
|
59
|
-
...overrides: Array<Record<string, unknown> | undefined>
|
|
60
|
-
): T {
|
|
61
|
-
let result = cloneValue(base);
|
|
62
|
-
|
|
63
|
-
for (const override of overrides) {
|
|
64
|
-
if (override === undefined) {
|
|
65
|
-
continue;
|
|
66
|
-
}
|
|
67
|
-
result = mergeValues(result, override) as T;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
return result;
|
|
71
|
-
}
|
package/src/lib/tags.ts
DELETED
package/src/lib/viewports.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { TestUserConfig } from "vitest/config";
|
|
2
|
-
|
|
3
|
-
export type Viewport = NonNullable<TestUserConfig["browser"]>["viewport"];
|
|
4
|
-
|
|
5
|
-
export const DESKTOP = { width: 1280, height: 720 } as const satisfies Viewport;
|
|
6
|
-
export const MOBILE = { width: 390, height: 844 } as const satisfies Viewport;
|
package/src/playwright/base.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { devices, type PlaywrightTestConfig } from "@playwright/test";
|
|
2
|
-
|
|
3
|
-
import { COMMON, DESKTOP, MOBILE as MOBILE_TAG } from "../lib/tags";
|
|
4
|
-
import { MOBILE } from "../lib/viewports";
|
|
5
|
-
|
|
6
|
-
const ci = !!process.env["CI"];
|
|
7
|
-
|
|
8
|
-
export default {
|
|
9
|
-
fullyParallel: true,
|
|
10
|
-
forbidOnly: ci,
|
|
11
|
-
reporter: ci
|
|
12
|
-
? [["github"], ["junit", { outputFile: "./test-results/e2e-junit.xml" }]]
|
|
13
|
-
: [["html"]],
|
|
14
|
-
|
|
15
|
-
use: {
|
|
16
|
-
trace: "on-first-retry",
|
|
17
|
-
screenshot: "only-on-failure",
|
|
18
|
-
...(ci && {
|
|
19
|
-
actionTimeout: 10_000,
|
|
20
|
-
}),
|
|
21
|
-
},
|
|
22
|
-
|
|
23
|
-
...(ci && {
|
|
24
|
-
expect: { timeout: 10_000 },
|
|
25
|
-
}),
|
|
26
|
-
|
|
27
|
-
projects: [
|
|
28
|
-
{
|
|
29
|
-
name: "desktop",
|
|
30
|
-
grep: new RegExp(`${DESKTOP}|${COMMON}`),
|
|
31
|
-
use: { ...devices["Desktop Chrome"] },
|
|
32
|
-
},
|
|
33
|
-
{
|
|
34
|
-
name: "mobile",
|
|
35
|
-
grep: new RegExp(`${MOBILE_TAG}|${COMMON}`),
|
|
36
|
-
use: {
|
|
37
|
-
...devices["Desktop Chrome"],
|
|
38
|
-
viewport: MOBILE,
|
|
39
|
-
isMobile: true,
|
|
40
|
-
hasTouch: true,
|
|
41
|
-
},
|
|
42
|
-
},
|
|
43
|
-
],
|
|
44
|
-
} satisfies PlaywrightTestConfig;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type PlaywrightTestConfig,
|
|
3
|
-
defineConfig as pwDefineConfig,
|
|
4
|
-
} from "@playwright/test";
|
|
5
|
-
|
|
6
|
-
import { deepMerge } from "../lib/deep-merge";
|
|
7
|
-
import base from "./base";
|
|
8
|
-
|
|
9
|
-
export function defineConfig(
|
|
10
|
-
...overrides: Array<Partial<PlaywrightTestConfig> | undefined>
|
|
11
|
-
) {
|
|
12
|
-
return pwDefineConfig(deepMerge(base, ...overrides));
|
|
13
|
-
}
|
package/src/vitest/base.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { resolve } from "node:path";
|
|
2
|
-
import { config } from "dotenv";
|
|
3
|
-
import { configDefaults, type ViteUserConfig } from "vitest/config";
|
|
4
|
-
|
|
5
|
-
const env =
|
|
6
|
-
config({ path: resolve(process.cwd(), ".env.test"), quiet: true }).parsed ??
|
|
7
|
-
{};
|
|
8
|
-
|
|
9
|
-
export default {
|
|
10
|
-
root: process.cwd(),
|
|
11
|
-
resolve: {
|
|
12
|
-
conditions: ["@source"],
|
|
13
|
-
},
|
|
14
|
-
ssr: {
|
|
15
|
-
resolve: {
|
|
16
|
-
conditions: ["@source"],
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
test: {
|
|
20
|
-
env: {
|
|
21
|
-
...env,
|
|
22
|
-
NODE_ENV: "test",
|
|
23
|
-
},
|
|
24
|
-
environment: "node",
|
|
25
|
-
globals: true,
|
|
26
|
-
isolate: true,
|
|
27
|
-
pool: "forks",
|
|
28
|
-
maxWorkers: "50%",
|
|
29
|
-
clearMocks: true,
|
|
30
|
-
restoreMocks: true,
|
|
31
|
-
unstubEnvs: true,
|
|
32
|
-
unstubGlobals: true,
|
|
33
|
-
sequence: {
|
|
34
|
-
shuffle: true,
|
|
35
|
-
},
|
|
36
|
-
include: [
|
|
37
|
-
"src/**/*.{test,spec}.?(c|m)[jt]s?(x)",
|
|
38
|
-
"test/**/*.{test,spec}.?(c|m)[jt]s?(x)",
|
|
39
|
-
],
|
|
40
|
-
exclude: [...configDefaults.exclude],
|
|
41
|
-
reporters: process.env["CI"] ? ["default", "junit"] : ["default"],
|
|
42
|
-
...(process.env["CI"] && {
|
|
43
|
-
outputFile: { junit: "./test-results/junit.xml" },
|
|
44
|
-
}),
|
|
45
|
-
coverage: {
|
|
46
|
-
include: ["src/**/*.{ts,tsx,js,jsx,mts,cts,mjs,cjs}"],
|
|
47
|
-
exclude: [...(configDefaults.coverage?.exclude ?? [])],
|
|
48
|
-
},
|
|
49
|
-
},
|
|
50
|
-
} satisfies ViteUserConfig;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type ViteUserConfig,
|
|
3
|
-
defineConfig as vitestDefineConfig,
|
|
4
|
-
} from "vitest/config";
|
|
5
|
-
|
|
6
|
-
import { deepMerge } from "../lib/deep-merge.ts";
|
|
7
|
-
import base from "./base.ts";
|
|
8
|
-
|
|
9
|
-
export function defineConfig(
|
|
10
|
-
...overrides: Array<Partial<ViteUserConfig> | undefined>
|
|
11
|
-
) {
|
|
12
|
-
return vitestDefineConfig(deepMerge(base, ...overrides));
|
|
13
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { setupWorker } from "msw/browser";
|
|
2
|
-
import { afterAll, afterEach, beforeAll } from "vitest";
|
|
3
|
-
|
|
4
|
-
export const worker = setupWorker();
|
|
5
|
-
|
|
6
|
-
export function enableMSW(): void {
|
|
7
|
-
beforeAll(async () => {
|
|
8
|
-
await worker.start({ onUnhandledRequest: "bypass", quiet: true });
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
afterEach(() => {
|
|
12
|
-
worker.resetHandlers();
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
afterAll(() => {
|
|
16
|
-
worker.stop();
|
|
17
|
-
});
|
|
18
|
-
}
|
package/src/vitest/msw-http.ts
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { HttpResponse, http } from "msw";
|
|
2
|
-
|
|
3
|
-
import { worker } from "./msw-browser.js";
|
|
4
|
-
|
|
5
|
-
export type HttpMethod = "post" | "get" | "put" | "delete" | "patch";
|
|
6
|
-
export type JsonValue =
|
|
7
|
-
| string
|
|
8
|
-
| number
|
|
9
|
-
| boolean
|
|
10
|
-
| null
|
|
11
|
-
| JsonValue[]
|
|
12
|
-
| { [key: string]: JsonValue };
|
|
13
|
-
|
|
14
|
-
export interface CapturedHttpCall {
|
|
15
|
-
body: unknown;
|
|
16
|
-
params: Record<string, string | readonly string[] | undefined>;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
async function parseBody(request: Request): Promise<unknown> {
|
|
20
|
-
const clone = request.clone();
|
|
21
|
-
const contentType = clone.headers.get("content-type") ?? "";
|
|
22
|
-
if (contentType.includes("multipart/form-data")) {
|
|
23
|
-
const formData = await clone.formData();
|
|
24
|
-
return Object.fromEntries(formData.entries());
|
|
25
|
-
}
|
|
26
|
-
return clone.json().catch(() => null);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function createHttpInterceptor(baseUrl: string) {
|
|
30
|
-
function intercept(
|
|
31
|
-
method: HttpMethod,
|
|
32
|
-
path: string,
|
|
33
|
-
response: JsonValue,
|
|
34
|
-
status = 200
|
|
35
|
-
): CapturedHttpCall[] {
|
|
36
|
-
const calls: CapturedHttpCall[] = [];
|
|
37
|
-
|
|
38
|
-
worker.use(
|
|
39
|
-
http[method](`${baseUrl}${path}`, async ({ request, params }) => {
|
|
40
|
-
calls.push({
|
|
41
|
-
body: await parseBody(request),
|
|
42
|
-
params,
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
return HttpResponse.json(response, { status });
|
|
46
|
-
})
|
|
47
|
-
);
|
|
48
|
-
|
|
49
|
-
return calls;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function interceptNetworkError(method: HttpMethod, path: string): void {
|
|
53
|
-
worker.use(http[method](`${baseUrl}${path}`, () => HttpResponse.error()));
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return {
|
|
57
|
-
intercept,
|
|
58
|
-
interceptNetworkError,
|
|
59
|
-
};
|
|
60
|
-
}
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { playwright } from "@vitest/browser-playwright";
|
|
2
|
-
import type { TestUserConfig, ViteUserConfig } from "vitest/config";
|
|
3
|
-
|
|
4
|
-
import { deepMerge } from "../../lib/deep-merge.ts";
|
|
5
|
-
import base from "../base.ts";
|
|
6
|
-
|
|
7
|
-
type BrowserInstance = NonNullable<
|
|
8
|
-
NonNullable<TestUserConfig["browser"]>["instances"]
|
|
9
|
-
>[number];
|
|
10
|
-
type InlineProjectConfig = NonNullable<TestUserConfig["projects"]>[number];
|
|
11
|
-
|
|
12
|
-
export function browserProject(opts: {
|
|
13
|
-
instances: BrowserInstance[];
|
|
14
|
-
name?: string;
|
|
15
|
-
setupFiles?: string[];
|
|
16
|
-
plugins?: ViteUserConfig["plugins"];
|
|
17
|
-
include?: string[];
|
|
18
|
-
exclude?: string[];
|
|
19
|
-
trace?: boolean;
|
|
20
|
-
optimizeDeps?: ViteUserConfig["optimizeDeps"];
|
|
21
|
-
define?: ViteUserConfig["define"];
|
|
22
|
-
resolve?: ViteUserConfig["resolve"];
|
|
23
|
-
}): InlineProjectConfig {
|
|
24
|
-
return deepMerge(
|
|
25
|
-
{
|
|
26
|
-
...(opts.plugins && { plugins: opts.plugins }),
|
|
27
|
-
...(opts.define && { define: opts.define }),
|
|
28
|
-
...(opts.resolve && { resolve: opts.resolve }),
|
|
29
|
-
...(opts.optimizeDeps && { optimizeDeps: opts.optimizeDeps }),
|
|
30
|
-
server: { port: 0, strictPort: false },
|
|
31
|
-
},
|
|
32
|
-
{
|
|
33
|
-
test: {
|
|
34
|
-
...base.test,
|
|
35
|
-
name: opts.name ?? "browser",
|
|
36
|
-
...(opts.setupFiles && { setupFiles: opts.setupFiles }),
|
|
37
|
-
...(opts.include && { include: opts.include }),
|
|
38
|
-
...(opts.exclude && { exclude: opts.exclude }),
|
|
39
|
-
browser: {
|
|
40
|
-
enabled: true,
|
|
41
|
-
provider: playwright(),
|
|
42
|
-
screenshotFailures: false,
|
|
43
|
-
instances: opts.instances,
|
|
44
|
-
...(opts.trace && {
|
|
45
|
-
trace: {
|
|
46
|
-
mode: "retain-on-failure" as const,
|
|
47
|
-
},
|
|
48
|
-
}),
|
|
49
|
-
},
|
|
50
|
-
},
|
|
51
|
-
}
|
|
52
|
-
) as InlineProjectConfig;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export type { BrowserInstance };
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import type { TestUserConfig } from "vitest/config";
|
|
2
|
-
|
|
3
|
-
import base from "../base.ts";
|
|
4
|
-
|
|
5
|
-
type InlineProjectConfig = NonNullable<TestUserConfig["projects"]>[number];
|
|
6
|
-
|
|
7
|
-
export function nodeProject(opts?: {
|
|
8
|
-
name?: string;
|
|
9
|
-
include?: string[];
|
|
10
|
-
exclude?: string[];
|
|
11
|
-
setupFiles?: string[];
|
|
12
|
-
}): InlineProjectConfig {
|
|
13
|
-
return {
|
|
14
|
-
test: {
|
|
15
|
-
...base.test,
|
|
16
|
-
name: opts?.name ?? "node",
|
|
17
|
-
environment: "node",
|
|
18
|
-
...(opts?.include && { include: opts.include }),
|
|
19
|
-
...(opts?.exclude && { exclude: opts.exclude }),
|
|
20
|
-
...(opts?.setupFiles && { setupFiles: opts.setupFiles }),
|
|
21
|
-
},
|
|
22
|
-
};
|
|
23
|
-
}
|