@ohos-ports/nuxt-test-utils 0.0.0-stage → 4.4.0-beta.1
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/LICENSE +21 -0
- package/README.md +20 -2
- package/dirs.js +4 -0
- package/dist/browser/index.d.mts +7 -0
- package/dist/browser/index.mjs +12 -0
- package/dist/browser/pure.d.mts +2 -0
- package/dist/browser/pure.mjs +1 -0
- package/dist/config.d.mts +82 -0
- package/dist/config.mjs +336 -0
- package/dist/e2e/dev-server.d.mts +1 -0
- package/dist/e2e/dev-server.mjs +10 -0
- package/dist/e2e-BO5mj9tk.d.mts +244 -0
- package/dist/e2e-DzsWJ8cS.mjs +373 -0
- package/dist/e2e.d.mts +2 -0
- package/dist/e2e.mjs +3 -0
- package/dist/experimental.d.mts +5 -0
- package/dist/experimental.mjs +20 -0
- package/dist/interface-Dhh_nx8F.mjs +34 -0
- package/dist/module.d.mts +32 -0
- package/dist/module.mjs +987 -0
- package/dist/playwright.d.mts +33 -0
- package/dist/playwright.mjs +68 -0
- package/dist/pure-Ba9-jZYN.d.mts +56 -0
- package/dist/pure-C_CSGRkt.mjs +114 -0
- package/dist/runtime/browser-entry.mjs +15 -0
- package/dist/runtime/entry.mjs +19 -0
- package/dist/runtime/global-setup.mjs +15 -0
- package/dist/runtime/mocks/vue-devtools.mjs +11 -0
- package/dist/runtime/nuxt-root.mjs +17 -0
- package/dist/runtime/shared/environment.mjs +67 -0
- package/dist/runtime/shared/h3-v1.mjs +32 -0
- package/dist/runtime/shared/h3-v2.mjs +34 -0
- package/dist/runtime/shared/h3.mjs +5 -0
- package/dist/runtime/shared/nuxt.mjs +17 -0
- package/dist/runtime/shared/vue-wrapper-plugin.mjs +42 -0
- package/dist/runtime-utils/index.d.mts +208 -0
- package/dist/runtime-utils/index.mjs +276 -0
- package/dist/server-Vl1UKNYB.mjs +301 -0
- package/dist/suspended-BbyJTvUP.d.mts +45 -0
- package/dist/suspended-Bjq6Zao6.mjs +1 -0
- package/dist/suspended-UrdAzOmb.mjs +255 -0
- package/dist/utils-CCklE0VI.mjs +196 -0
- package/dist/vitest-environment.d.mts +42 -0
- package/dist/vitest-environment.mjs +318 -0
- package/dist/vitest-wrapper/cli.d.mts +1 -0
- package/dist/vitest-wrapper/cli.mjs +81 -0
- package/package.json +145 -4
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { O as GotoOptions, _ as TestHooks, v as TestOptions$1 } from "./e2e-BO5mj9tk.mjs";
|
|
2
|
+
import { expect } from "@ohos-ports/playwright-test";
|
|
3
|
+
import { Response } from "@ohos-ports/playwright-core";
|
|
4
|
+
export type ConfigOptions = {
|
|
5
|
+
nuxt: Partial<TestOptions$1> | undefined;
|
|
6
|
+
defaults: {
|
|
7
|
+
nuxt: Partial<TestOptions$1> | undefined;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
type WorkerOptions = {
|
|
11
|
+
_nuxtHooks: TestHooks;
|
|
12
|
+
};
|
|
13
|
+
type TestOptions = {
|
|
14
|
+
goto: (url: string, options?: GotoOptions) => Promise<Response | null>;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Use a preconfigured Nuxt fixture.
|
|
18
|
+
*
|
|
19
|
+
* You can pass a `nuxt: {}` object in your device configuration, in the `use` key of your config file,
|
|
20
|
+
* or use the following syntax within your test file to configure your Nuxt fixture:
|
|
21
|
+
*
|
|
22
|
+
```ts
|
|
23
|
+
test.use({
|
|
24
|
+
nuxt: {
|
|
25
|
+
rootDir: fileURLToPath(new URL('.', import.meta.url)),
|
|
26
|
+
}
|
|
27
|
+
})
|
|
28
|
+
```
|
|
29
|
+
*
|
|
30
|
+
* In `playwright.config.ts` you can pass `defaults: { nuxt: {} }` object for merging with test.use nuxt options
|
|
31
|
+
*/
|
|
32
|
+
export declare const test: import("@ohos-ports/playwright-test").TestType<import("@ohos-ports/playwright-test").PlaywrightTestArgs & import("@ohos-ports/playwright-test").PlaywrightTestOptions & TestOptions, import("@ohos-ports/playwright-test").PlaywrightWorkerArgs & import("@ohos-ports/playwright-test").PlaywrightWorkerOptions & WorkerOptions & ConfigOptions>;
|
|
33
|
+
export { expect };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { s as url } from "./server-Vl1UKNYB.mjs";
|
|
2
|
+
import { f as waitForHydration, n as createTest } from "./e2e-DzsWJ8cS.mjs";
|
|
3
|
+
import defu from "defu";
|
|
4
|
+
import { expect, test as test$1 } from "@ohos-ports/playwright-test";
|
|
5
|
+
// src/playwright.ts
|
|
6
|
+
const FIXTURE_TIMEOUT_BACKSTOP = 18e5;
|
|
7
|
+
const ABANDONED_SETUP_GRACE = 3e4;
|
|
8
|
+
/**
|
|
9
|
+
* Use a preconfigured Nuxt fixture.
|
|
10
|
+
*
|
|
11
|
+
* You can pass a `nuxt: {}` object in your device configuration, in the `use` key of your config file,
|
|
12
|
+
* or use the following syntax within your test file to configure your Nuxt fixture:
|
|
13
|
+
*
|
|
14
|
+
```ts
|
|
15
|
+
test.use({
|
|
16
|
+
nuxt: {
|
|
17
|
+
rootDir: fileURLToPath(new URL('.', import.meta.url)),
|
|
18
|
+
}
|
|
19
|
+
})
|
|
20
|
+
```
|
|
21
|
+
*
|
|
22
|
+
* In `playwright.config.ts` you can pass `defaults: { nuxt: {} }` object for merging with test.use nuxt options
|
|
23
|
+
*/
|
|
24
|
+
const test = test$1.extend({
|
|
25
|
+
nuxt: [void 0, {
|
|
26
|
+
option: true,
|
|
27
|
+
scope: "worker"
|
|
28
|
+
}],
|
|
29
|
+
defaults: [{ nuxt: void 0 }, {
|
|
30
|
+
option: true,
|
|
31
|
+
scope: "worker"
|
|
32
|
+
}],
|
|
33
|
+
_nuxtHooks: [async ({ nuxt, defaults }, use) => {
|
|
34
|
+
const hooks = createTest(defu(nuxt || {}, defaults.nuxt || {}));
|
|
35
|
+
const { setupTimeout } = hooks.ctx.options;
|
|
36
|
+
const setup = hooks.beforeAll();
|
|
37
|
+
let timer;
|
|
38
|
+
if (await Promise.race([setup.then(() => false), new Promise((resolve) => {
|
|
39
|
+
timer = setTimeout(() => resolve(true), setupTimeout);
|
|
40
|
+
})]).finally(() => clearTimeout(timer))) {
|
|
41
|
+
await hooks.afterAll();
|
|
42
|
+
await Promise.race([setup.catch(() => {}), new Promise((resolve) => setTimeout(resolve, ABANDONED_SETUP_GRACE))]);
|
|
43
|
+
await hooks.afterAll();
|
|
44
|
+
const logs = hooks.ctx.serverLogs.slice(-50).join("\n");
|
|
45
|
+
throw new Error(`Nuxt test setup timed out after ${setupTimeout}ms. Increase \`setupTimeout\` in your \`nuxt\` fixture options if this is expected.${logs ? `\n\nServer output:\n${logs}` : ""}`);
|
|
46
|
+
}
|
|
47
|
+
await use(hooks);
|
|
48
|
+
await hooks.afterAll();
|
|
49
|
+
}, {
|
|
50
|
+
scope: "worker",
|
|
51
|
+
timeout: FIXTURE_TIMEOUT_BACKSTOP
|
|
52
|
+
}],
|
|
53
|
+
baseURL: async ({ _nuxtHooks }, use) => {
|
|
54
|
+
_nuxtHooks.beforeEach();
|
|
55
|
+
await use(url("/"));
|
|
56
|
+
_nuxtHooks.afterEach();
|
|
57
|
+
},
|
|
58
|
+
goto: async ({ page }, use) => {
|
|
59
|
+
await use(async (url, options) => {
|
|
60
|
+
const waitUntil = options?.waitUntil;
|
|
61
|
+
if (waitUntil && ["hydration", "route"].includes(waitUntil)) delete options.waitUntil;
|
|
62
|
+
const response = await page.goto(url, options);
|
|
63
|
+
await waitForHydration(page, url, waitUntil);
|
|
64
|
+
return response;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
export { expect, test };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { n as WrapperSuspendedAdditionalOptions, r as WrapperSuspendedOptions, t as SetupState } from "./suspended-BbyJTvUP.mjs";
|
|
2
|
+
import { MountingOptions, config, mount } from "@ohos-ports/vue-test-utils";
|
|
3
|
+
import { Locator, LocatorSelectors, PrettyDOMOptions } from "vitest/browser";
|
|
4
|
+
type ComponentProps<T> = T extends (new (...args: never[]) => {
|
|
5
|
+
$props: infer P;
|
|
6
|
+
}) ? NonNullable<P> : T extends ((props: infer P, ...args: never[]) => unknown) ? P : object;
|
|
7
|
+
type WrapperFn<C> = typeof mount<C>;
|
|
8
|
+
type WrapperOptions<C> = Pick<WrapperSuspendedOptions<WrapperFn<C>>, Exclude<keyof MountingOptions<object>, 'attachTo'> | keyof WrapperSuspendedAdditionalOptions> & {
|
|
9
|
+
/** Use this option instead of the `@ohos-ports/vue-test-utils` `attachTo` option. */
|
|
10
|
+
container?: HTMLElement;
|
|
11
|
+
baseElement?: HTMLElement;
|
|
12
|
+
};
|
|
13
|
+
interface RenderResult<Props, T = unknown> extends LocatorSelectors {
|
|
14
|
+
container: HTMLElement;
|
|
15
|
+
baseElement: HTMLElement;
|
|
16
|
+
locator: Locator;
|
|
17
|
+
/**
|
|
18
|
+
* The return value of the component setup, mocked when the `spy` option is enabled.
|
|
19
|
+
*/
|
|
20
|
+
setupState: SetupState<T>;
|
|
21
|
+
debug(el?: HTMLElement | HTMLElement[] | Locator | Locator[], maxLength?: number, options?: PrettyDOMOptions): void;
|
|
22
|
+
/** Unmount the component. Also records a `nuxt.unmount` trace mark. */
|
|
23
|
+
unmount(): Promise<void>;
|
|
24
|
+
emitted<T = unknown>(): Record<string, T[]>;
|
|
25
|
+
emitted<T = unknown[]>(eventName: string): undefined | T[];
|
|
26
|
+
/** Re-render the component with new props. Also records a `nuxt.rerender` trace mark. */
|
|
27
|
+
rerender(props: Partial<Props>): Promise<void>;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* `render` allows you to mount any vue component within the vitest browser mode.
|
|
31
|
+
*
|
|
32
|
+
* ```ts
|
|
33
|
+
* import { page } from 'vitest/browser'
|
|
34
|
+
* import { render } from '@nuxt/test-utils/browser'
|
|
35
|
+
*
|
|
36
|
+
* it('can render', async () => {
|
|
37
|
+
* const screen = await render(App, { route: '/' })
|
|
38
|
+
*
|
|
39
|
+
* const title = screen.getByRole('heading')
|
|
40
|
+
* await expect.element(title).toHaveTextContent('Index')
|
|
41
|
+
* })
|
|
42
|
+
*
|
|
43
|
+
* it('can render with page', async () => {
|
|
44
|
+
* const screen = await page.render(App, { route: '/' })
|
|
45
|
+
*
|
|
46
|
+
* const title = screen.getByRole('heading')
|
|
47
|
+
* await expect.element(title).toHaveTextContent('Index')
|
|
48
|
+
* })
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* @param component the component to be tested
|
|
52
|
+
* @param options optional options to set up your component
|
|
53
|
+
*/
|
|
54
|
+
declare function render<T>(component: T, options?: WrapperOptions<T>): Promise<RenderResult<ComponentProps<T>, T>>;
|
|
55
|
+
declare function cleanup(): void;
|
|
56
|
+
export { render as i, cleanup as n, config as r, RenderResult as t };
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { n as patchWrapperSetProps, r as wrapperSuspended, t as cleanupAll } from "./suspended-UrdAzOmb.mjs";
|
|
2
|
+
import { config, mount } from "@ohos-ports/vue-test-utils";
|
|
3
|
+
import { page, server, utils } from "vitest/browser";
|
|
4
|
+
// src/browser/pure.ts
|
|
5
|
+
const mountedWrappers = /* @__PURE__ */ new Set();
|
|
6
|
+
let idx = 0;
|
|
7
|
+
function ensureTestIdAttribute(element) {
|
|
8
|
+
const attributeId = server.config.browser.locators.testIdAttribute;
|
|
9
|
+
if (!element.hasAttribute(attributeId)) element.setAttribute(attributeId, `__vitest_${idx++}__`);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* `render` allows you to mount any vue component within the vitest browser mode.
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { page } from 'vitest/browser'
|
|
16
|
+
* import { render } from '@nuxt/test-utils/browser'
|
|
17
|
+
*
|
|
18
|
+
* it('can render', async () => {
|
|
19
|
+
* const screen = await render(App, { route: '/' })
|
|
20
|
+
*
|
|
21
|
+
* const title = screen.getByRole('heading')
|
|
22
|
+
* await expect.element(title).toHaveTextContent('Index')
|
|
23
|
+
* })
|
|
24
|
+
*
|
|
25
|
+
* it('can render with page', async () => {
|
|
26
|
+
* const screen = await page.render(App, { route: '/' })
|
|
27
|
+
*
|
|
28
|
+
* const title = screen.getByRole('heading')
|
|
29
|
+
* await expect.element(title).toHaveTextContent('Index')
|
|
30
|
+
* })
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @param component the component to be tested
|
|
34
|
+
* @param options optional options to set up your component
|
|
35
|
+
*/
|
|
36
|
+
async function render(component, options = {}) {
|
|
37
|
+
const { container, baseElement, wrapper } = await mountWrapperSuspended(component, options);
|
|
38
|
+
ensureTestIdAttribute(container);
|
|
39
|
+
ensureTestIdAttribute(baseElement);
|
|
40
|
+
const { debug, getElementLocatorSelectors } = utils;
|
|
41
|
+
const renderResult = {
|
|
42
|
+
container,
|
|
43
|
+
baseElement,
|
|
44
|
+
locator: page.elementLocator(container),
|
|
45
|
+
get setupState() {
|
|
46
|
+
return wrapper.setupState;
|
|
47
|
+
},
|
|
48
|
+
debug: (el = container, maxLength, options) => debug(el, maxLength, options),
|
|
49
|
+
unmount: async () => {
|
|
50
|
+
wrapper.unmount();
|
|
51
|
+
mountedWrappers.delete(wrapper);
|
|
52
|
+
await mark(renderResult.locator, "nuxt.unmount", renderResult.unmount);
|
|
53
|
+
},
|
|
54
|
+
emitted: ((eventName) => wrapper.emitted(eventName)),
|
|
55
|
+
rerender: async (props) => {
|
|
56
|
+
await wrapper.setProps(props);
|
|
57
|
+
await mark(renderResult.locator, "nuxt.rerender", renderResult.rerender);
|
|
58
|
+
},
|
|
59
|
+
...getElementLocatorSelectors(container)
|
|
60
|
+
};
|
|
61
|
+
return renderResult;
|
|
62
|
+
}
|
|
63
|
+
function cleanup() {
|
|
64
|
+
mountedWrappers.forEach((wrapper) => {
|
|
65
|
+
wrapper.unmount();
|
|
66
|
+
mountedWrappers.delete(wrapper);
|
|
67
|
+
});
|
|
68
|
+
cleanupAll();
|
|
69
|
+
}
|
|
70
|
+
async function mark(locator, name, fn) {
|
|
71
|
+
if (!locator.mark) return;
|
|
72
|
+
const error = new Error(name);
|
|
73
|
+
if ("captureStackTrace" in Error) Error.captureStackTrace(error, fn);
|
|
74
|
+
return locator.mark(name, error);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* `@ohos-ports/vue-test-utils` mounts into a `div` of its own inside `attachTo`. Removing that div keeps the
|
|
78
|
+
* rendered markup a direct child of the container, so locators and snapshots see the component's
|
|
79
|
+
* own DOM.
|
|
80
|
+
*/
|
|
81
|
+
function unwrapNode(node) {
|
|
82
|
+
if (node && typeof node.replaceWith === "function") node.replaceWith(...node.childNodes);
|
|
83
|
+
}
|
|
84
|
+
async function mountWrapperSuspended(component, options = {}) {
|
|
85
|
+
const { container: containerOption, baseElement: baseElementOption, ...wrapperOptions } = options;
|
|
86
|
+
const baseElement = baseElementOption || document.body;
|
|
87
|
+
const createdContainer = containerOption ? void 0 : baseElement.appendChild(document.createElement("div"));
|
|
88
|
+
const container = containerOption || createdContainer;
|
|
89
|
+
const suspendedOptions = {
|
|
90
|
+
...wrapperOptions,
|
|
91
|
+
attachTo: container
|
|
92
|
+
};
|
|
93
|
+
const { wrapper, setProps, cleanup } = await wrapperSuspended(component, suspendedOptions, {
|
|
94
|
+
wrapperFn: mount,
|
|
95
|
+
suspendedHelperName: "BrowserSuspendedHelper",
|
|
96
|
+
clonedComponentName: "BrowserSuspendedComponent",
|
|
97
|
+
stubRouterLink: false
|
|
98
|
+
});
|
|
99
|
+
patchWrapperSetProps(wrapper, setProps);
|
|
100
|
+
const _unmount = wrapper.unmount.bind(wrapper);
|
|
101
|
+
wrapper.unmount = () => {
|
|
102
|
+
_unmount();
|
|
103
|
+
cleanup();
|
|
104
|
+
createdContainer?.remove();
|
|
105
|
+
};
|
|
106
|
+
mountedWrappers.add(wrapper);
|
|
107
|
+
unwrapNode(wrapper.element.parentElement);
|
|
108
|
+
return {
|
|
109
|
+
container,
|
|
110
|
+
baseElement,
|
|
111
|
+
wrapper
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
export { config as n, render as r, cleanup as t };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { setupNuxt } from "./shared/nuxt.mjs";
|
|
2
|
+
import { setupWindow } from "./shared/environment.mjs";
|
|
3
|
+
import environmentOptions from "nuxt-vitest-environment-options";
|
|
4
|
+
import { tryUseNuxtApp } from "#imports";
|
|
5
|
+
// src/runtime/browser-entry.ts
|
|
6
|
+
const win = window;
|
|
7
|
+
win.__NUXT_VITEST_ENVIRONMENT_BROWSER_ENTRY__ = true;
|
|
8
|
+
if (!win.__NUXT_VITEST_ENVIRONMENT__) await setupWindow(win, environmentOptions);
|
|
9
|
+
win.__NUXT_VITEST_ENVIRONMENT_PROMISE__ ??= setupNuxt().catch((err) => {
|
|
10
|
+
tryUseNuxtApp()?.vueApp?.unmount();
|
|
11
|
+
delete win.__NUXT_VITEST_ENVIRONMENT_PROMISE__;
|
|
12
|
+
throw err;
|
|
13
|
+
});
|
|
14
|
+
await win.__NUXT_VITEST_ENVIRONMENT_PROMISE__;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { setupNuxt } from "./shared/nuxt.mjs";
|
|
2
|
+
import { tryUseNuxtApp } from "#imports";
|
|
3
|
+
import { beforeAll, vi } from "vitest";
|
|
4
|
+
// src/runtime/entry.ts
|
|
5
|
+
if (typeof window !== "undefined") {
|
|
6
|
+
const win = window;
|
|
7
|
+
if (win.__NUXT_VITEST_ENVIRONMENT__ && !win.__NUXT_VITEST_ENVIRONMENT_BROWSER_ENTRY__) {
|
|
8
|
+
if (!tryUseNuxtApp()) vi.resetModules();
|
|
9
|
+
beforeAll(async () => {
|
|
10
|
+
win.__NUXT_VITEST_ENVIRONMENT_PROMISE__ ??= setupNuxt().catch((err) => {
|
|
11
|
+
tryUseNuxtApp()?.vueApp?.unmount();
|
|
12
|
+
delete win.__NUXT_VITEST_ENVIRONMENT_PROMISE__;
|
|
13
|
+
throw err;
|
|
14
|
+
});
|
|
15
|
+
await win.__NUXT_VITEST_ENVIRONMENT_PROMISE__;
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { consola } from "consola";
|
|
2
|
+
import { createTest, exposeContextToEnv } from "@nuxt/test-utils/e2e";
|
|
3
|
+
// src/runtime/global-setup.ts
|
|
4
|
+
const options = JSON.parse(process.env.NUXT_TEST_OPTIONS || "{}");
|
|
5
|
+
const hooks = createTest(options);
|
|
6
|
+
const setup = async () => {
|
|
7
|
+
consola.info("Building Nuxt app...");
|
|
8
|
+
await hooks.beforeAll();
|
|
9
|
+
exposeContextToEnv();
|
|
10
|
+
consola.info("Running tests...");
|
|
11
|
+
};
|
|
12
|
+
const teardown = async () => {
|
|
13
|
+
await hooks.afterAll();
|
|
14
|
+
};
|
|
15
|
+
export { setup, teardown };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// src/runtime/mocks/vue-devtools.ts
|
|
2
|
+
const functions = [];
|
|
3
|
+
function createRpcServer() {}
|
|
4
|
+
const devtools = { init() {} };
|
|
5
|
+
function addCustomCommand() {}
|
|
6
|
+
function addCustomTab() {}
|
|
7
|
+
function onDevToolsClientConnected() {}
|
|
8
|
+
function onDevToolsConnected() {}
|
|
9
|
+
function removeCustomCommand() {}
|
|
10
|
+
function setupDevToolsPlugin() {}
|
|
11
|
+
export { addCustomCommand, addCustomTab, createRpcServer, devtools, functions, onDevToolsClientConnected, onDevToolsConnected, removeCustomCommand, setupDevToolsPlugin };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { isNuxtError, useNuxtApp, useRoute } from "#imports";
|
|
2
|
+
import { Suspense, defineComponent, h, onErrorCaptured, provide } from "vue";
|
|
3
|
+
import { PageRouteSymbol } from "#app/components/injections";
|
|
4
|
+
// src/runtime/nuxt-root.ts
|
|
5
|
+
var nuxt_root_default = defineComponent({ setup(_options, { slots }) {
|
|
6
|
+
const nuxtApp = useNuxtApp();
|
|
7
|
+
provide(PageRouteSymbol, useRoute());
|
|
8
|
+
const done = nuxtApp.deferHydration();
|
|
9
|
+
const results = nuxtApp.hooks.callHookWith((hooks) => hooks.map((hook) => hook()), "vue:setup", []);
|
|
10
|
+
if (import.meta.dev && results && results.some((i) => i && "then" in i)) console.error("[nuxt] Error in `vue:setup`. Callbacks must be synchronous.");
|
|
11
|
+
onErrorCaptured((err, target, info) => {
|
|
12
|
+
nuxtApp.hooks.callHook("vue:error", err, target, info)?.catch((hookError) => console.error("[nuxt] Error in `vue:error` hook", hookError));
|
|
13
|
+
if (isNuxtError(err) && (err.fatal || err.unhandled)) return false;
|
|
14
|
+
});
|
|
15
|
+
return () => h(Suspense, { onResolve: done }, slots.default?.());
|
|
16
|
+
} });
|
|
17
|
+
export { nuxt_root_default as default };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { defineEventHandler } from "./h3.mjs";
|
|
2
|
+
import { createFetchForH3V1 } from "./h3-v1.mjs";
|
|
3
|
+
import { createFetchForH3V2 } from "./h3-v2.mjs";
|
|
4
|
+
import { createFetch } from "ofetch";
|
|
5
|
+
import { joinURL } from "ufo";
|
|
6
|
+
import { createRouter, exportMatcher, toRouteMatcher } from "radix3";
|
|
7
|
+
// src/runtime/shared/environment.ts
|
|
8
|
+
async function setupWindow(win, environmentOptions) {
|
|
9
|
+
win.__NUXT_VITEST_ENVIRONMENT__ = true;
|
|
10
|
+
win.__NUXT__ = {
|
|
11
|
+
serverRendered: false,
|
|
12
|
+
config: {
|
|
13
|
+
public: {},
|
|
14
|
+
app: { baseURL: "/" },
|
|
15
|
+
...environmentOptions?.nuxtRuntimeConfig
|
|
16
|
+
},
|
|
17
|
+
data: {},
|
|
18
|
+
state: {}
|
|
19
|
+
};
|
|
20
|
+
const consoleInfo = console.info;
|
|
21
|
+
console.info = (...args) => {
|
|
22
|
+
if (args[0] === "<Suspense> is an experimental feature and its API will likely change.") return;
|
|
23
|
+
return consoleInfo(...args);
|
|
24
|
+
};
|
|
25
|
+
const appConfig = environmentOptions.nuxtAppConfig;
|
|
26
|
+
createElementAndAppend(win, appConfig?.rootTag || "div", {
|
|
27
|
+
...appConfig?.rootAttrs,
|
|
28
|
+
id: environmentOptions.nuxt.rootId || appConfig?.rootAttrs?.id || "nuxt-test"
|
|
29
|
+
});
|
|
30
|
+
createElementAndAppend(win, appConfig?.teleportTag || "div", appConfig?.teleportAttrs || { id: "teleports" });
|
|
31
|
+
const res = environmentOptions.nuxt.h3Version === 2 ? await createFetchForH3V2() : await createFetchForH3V1();
|
|
32
|
+
win.fetch = res.fetch;
|
|
33
|
+
win.$fetch = createFetch({
|
|
34
|
+
fetch: win.fetch,
|
|
35
|
+
Headers: win.Headers
|
|
36
|
+
});
|
|
37
|
+
win.__registry = res.registry;
|
|
38
|
+
win.__app = res.h3App;
|
|
39
|
+
const timestamp = Date.now();
|
|
40
|
+
const routeRulesMatcher = toRouteMatcher(createRouter({ routes: environmentOptions.nuxtRouteRules || {} }));
|
|
41
|
+
const matcher = exportMatcher(routeRulesMatcher);
|
|
42
|
+
const manifestOutputPath = joinURL(environmentOptions?.nuxtRuntimeConfig?.app?.baseURL || "/", environmentOptions?.nuxtRuntimeConfig?.app?.buildAssetsDir || "_nuxt", "builds");
|
|
43
|
+
const manifestBaseRoutePath = joinURL("/_", manifestOutputPath);
|
|
44
|
+
const buildId = win.__NUXT__.config?.app.buildId || "test";
|
|
45
|
+
res.h3App.use(`${manifestBaseRoutePath}/latest.json`, defineEventHandler(() => ({
|
|
46
|
+
id: buildId,
|
|
47
|
+
timestamp
|
|
48
|
+
})));
|
|
49
|
+
res.h3App.use(`${manifestBaseRoutePath}/meta/${buildId}.json`, defineEventHandler(() => ({
|
|
50
|
+
id: buildId,
|
|
51
|
+
timestamp,
|
|
52
|
+
matcher,
|
|
53
|
+
prerendered: []
|
|
54
|
+
})));
|
|
55
|
+
res.registry.add(`${manifestOutputPath}/latest.json`);
|
|
56
|
+
res.registry.add(`${manifestOutputPath}/meta/${buildId}.json`);
|
|
57
|
+
return () => {
|
|
58
|
+
console.info = consoleInfo;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function createElementAndAppend(win, tag, attrs) {
|
|
62
|
+
if (attrs?.id && win.document.getElementById(attrs.id)) return;
|
|
63
|
+
const element = win.document.createElement(tag);
|
|
64
|
+
for (const [key, value] of Object.entries(attrs ?? {})) if (value !== false && value != null) element.setAttribute(key, value === true ? "" : String(value));
|
|
65
|
+
win.document.body.appendChild(element);
|
|
66
|
+
}
|
|
67
|
+
export { setupWindow };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// src/runtime/shared/h3-v1.ts
|
|
2
|
+
async function createFetchForH3V1() {
|
|
3
|
+
const { createApp, toWebHandler } = await import("#nuxt-test-utils/h3");
|
|
4
|
+
const h3App = createApp();
|
|
5
|
+
const webHandler = toWebHandler(h3App);
|
|
6
|
+
const registry = /* @__PURE__ */ new Set();
|
|
7
|
+
const _fetch = fetch;
|
|
8
|
+
const h3Fetch = (async (input, _init) => {
|
|
9
|
+
let url;
|
|
10
|
+
let init = _init;
|
|
11
|
+
if (typeof input === "string") url = input;
|
|
12
|
+
else if (input instanceof URL) url = input.toString();
|
|
13
|
+
else {
|
|
14
|
+
url = input.url;
|
|
15
|
+
init = {
|
|
16
|
+
method: init?.method ?? input.method,
|
|
17
|
+
body: init?.body ?? input.body,
|
|
18
|
+
headers: init?.headers ?? input.headers
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
const base = url.split("?")[0];
|
|
22
|
+
if (registry.has(base) || registry.has(url)) url = "/_" + url;
|
|
23
|
+
if (url.startsWith("/")) return webHandler(new Request(new URL(url, "http://localhost"), init));
|
|
24
|
+
return _fetch(input, _init);
|
|
25
|
+
});
|
|
26
|
+
return {
|
|
27
|
+
h3App,
|
|
28
|
+
registry,
|
|
29
|
+
fetch: h3Fetch
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
export { createFetchForH3V1 };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// src/runtime/shared/h3-v2.ts
|
|
2
|
+
async function createFetchForH3V2() {
|
|
3
|
+
const { H3 } = await import("#nuxt-test-utils/h3");
|
|
4
|
+
const h3App = new H3();
|
|
5
|
+
const registry = /* @__PURE__ */ new Set();
|
|
6
|
+
const _fetch = fetch;
|
|
7
|
+
const h3Fetch = (async (input, _init) => {
|
|
8
|
+
let url;
|
|
9
|
+
let init = _init;
|
|
10
|
+
if (typeof input === "string") url = input;
|
|
11
|
+
else if (input instanceof URL) url = input.toString();
|
|
12
|
+
else {
|
|
13
|
+
url = input.url;
|
|
14
|
+
init = {
|
|
15
|
+
method: init?.method ?? input.method,
|
|
16
|
+
body: init?.body ?? input.body,
|
|
17
|
+
headers: init?.headers ?? input.headers
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
const base = url.split("?")[0];
|
|
21
|
+
if (registry.has(base) || registry.has(url)) return h3App.fetch(new Request("/_" + url, init));
|
|
22
|
+
if (url.startsWith("/")) return new Response("Not Found", {
|
|
23
|
+
status: 404,
|
|
24
|
+
statusText: "Not Found"
|
|
25
|
+
});
|
|
26
|
+
return _fetch(input, _init);
|
|
27
|
+
});
|
|
28
|
+
return {
|
|
29
|
+
h3App,
|
|
30
|
+
registry,
|
|
31
|
+
fetch: h3Fetch
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export { createFetchForH3V2 };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { getVueWrapperPlugin } from "./vue-wrapper-plugin.mjs";
|
|
2
|
+
// src/runtime/shared/nuxt.ts
|
|
3
|
+
async function setupNuxt() {
|
|
4
|
+
const { useRouter } = await import("#app/composables/router");
|
|
5
|
+
await import("#app/nuxt-vitest-app-entry").then((r) => r.default());
|
|
6
|
+
const nuxtApp = useNuxtApp();
|
|
7
|
+
function sync() {
|
|
8
|
+
return nuxtApp._route.sync ? nuxtApp._route.sync() : nuxtApp.callHook("page:finish");
|
|
9
|
+
}
|
|
10
|
+
const { hasNuxtPage } = getVueWrapperPlugin();
|
|
11
|
+
useRouter().afterEach(() => {
|
|
12
|
+
if (hasNuxtPage()) return;
|
|
13
|
+
return sync();
|
|
14
|
+
});
|
|
15
|
+
return sync();
|
|
16
|
+
}
|
|
17
|
+
export { setupNuxt };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// src/runtime/shared/vue-wrapper-plugin.ts
|
|
2
|
+
const PLUGIN_NAME = "nuxt-test-utils";
|
|
3
|
+
const config = await import("@ohos-ports/vue-test-utils").then((r) => r.config).catch(() => void 0);
|
|
4
|
+
function getVueWrapperPlugin() {
|
|
5
|
+
if (!config) return { hasNuxtPage: () => false };
|
|
6
|
+
const installed = config.plugins.VueWrapper.installedPlugins.find(({ options }) => options?._name === PLUGIN_NAME);
|
|
7
|
+
if (installed) return installed.options;
|
|
8
|
+
const options = createPluginOptions();
|
|
9
|
+
config.plugins.VueWrapper.install((instance, options) => {
|
|
10
|
+
options.addInstance(instance);
|
|
11
|
+
return {};
|
|
12
|
+
}, options);
|
|
13
|
+
return options;
|
|
14
|
+
}
|
|
15
|
+
function createPluginOptions() {
|
|
16
|
+
const options = {
|
|
17
|
+
_name: PLUGIN_NAME,
|
|
18
|
+
_instances: [],
|
|
19
|
+
get instances() {
|
|
20
|
+
const instances = [];
|
|
21
|
+
options._instances = options._instances.filter((ref) => {
|
|
22
|
+
const instance = ref.deref();
|
|
23
|
+
if (!instance) return false;
|
|
24
|
+
instances.push(instance);
|
|
25
|
+
return true;
|
|
26
|
+
});
|
|
27
|
+
return instances;
|
|
28
|
+
},
|
|
29
|
+
addInstance(instance) {
|
|
30
|
+
if (options.instances.includes(instance)) return;
|
|
31
|
+
options._instances.push(new WeakRef(instance));
|
|
32
|
+
},
|
|
33
|
+
hasNuxtPage() {
|
|
34
|
+
return options._hasComponent("NuxtPage");
|
|
35
|
+
},
|
|
36
|
+
_hasComponent(componentName) {
|
|
37
|
+
return options.instances.some((v) => v.exists() && v.findComponent({ name: componentName }).exists());
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
return options;
|
|
41
|
+
}
|
|
42
|
+
export { getVueWrapperPlugin };
|