@nuxt/test-utils 0.2.2 → 3.0.0-rc.2
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/README.md +2 -60
- package/dist/index.d.ts +53 -62
- package/dist/index.mjs +285 -0
- package/package.json +23 -34
- package/CHANGELOG.md +0 -97
- package/LICENSE +0 -21
- package/dist/index.js +0 -274
- package/jest-preset.js +0 -16
package/README.md
CHANGED
|
@@ -1,61 +1,3 @@
|
|
|
1
|
-
|
|
1
|
+
# Nuxt Test Utils
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
[![npm version][npm-version-src]][npm-version-href]
|
|
6
|
-
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
7
|
-
[![Checks][checks-src]][checks-href]
|
|
8
|
-
[![Codecov][codecov-src]][codecov-href]
|
|
9
|
-
|
|
10
|
-
> Test utilities for [Nuxt.js](https://nuxtjs.org)
|
|
11
|
-
|
|
12
|
-
- [✨ **Release Notes**](./CHANGELOG.md)
|
|
13
|
-
- [📖 **Documentation**](https://test-utils.nuxtjs.org)
|
|
14
|
-
|
|
15
|
-
## Features
|
|
16
|
-
|
|
17
|
-
- Easy to setup tests
|
|
18
|
-
- Test in browser
|
|
19
|
-
- Extend Jest matchers
|
|
20
|
-
- Written in TypeScript
|
|
21
|
-
|
|
22
|
-
📖 [Read more](https://test-utils.nuxtjs.org)
|
|
23
|
-
|
|
24
|
-
## Usage
|
|
25
|
-
|
|
26
|
-
1. Add `@nuxt/test-utils` devDependency to your project
|
|
27
|
-
|
|
28
|
-
```bash
|
|
29
|
-
yarn add --dev @nuxt/test-utils # or npm install --save-dev @nuxt/test-utils
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
2. Add `@nuxt/test-utils` to the `preset` section of `jest.config.js`
|
|
33
|
-
|
|
34
|
-
```js
|
|
35
|
-
module.exports = {
|
|
36
|
-
preset: '@nuxt/test-utils'
|
|
37
|
-
}
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
[📖 **Read more about setup and configuration**](https://test-utils.nuxtjs.org/api-reference/setup)
|
|
41
|
-
|
|
42
|
-
## Development
|
|
43
|
-
|
|
44
|
-
1. Clone this repository
|
|
45
|
-
2. Install dependencies using `yarn install`
|
|
46
|
-
|
|
47
|
-
## 📑 License
|
|
48
|
-
|
|
49
|
-
[MIT License](./LICENSE)
|
|
50
|
-
|
|
51
|
-
Copyright (c) Nuxt.js Team
|
|
52
|
-
|
|
53
|
-
<!-- Badges -->
|
|
54
|
-
[npm-version-src]: https://flat.badgen.net/npm/v/@nuxt/test-utils
|
|
55
|
-
[npm-version-href]: https://npmjs.com/package/@nuxt/test-utils
|
|
56
|
-
[npm-downloads-src]: https://flat.badgen.net/npm/dm/@nuxt/test-utils
|
|
57
|
-
[npm-downloads-href]: https://npmjs.com/package/@nuxt/test-utils
|
|
58
|
-
[checks-src]: https://flat.badgen.net/github/checks/nuxt/test-utils/main
|
|
59
|
-
[checks-href]: https://github.com/nuxt/test-utils/actions
|
|
60
|
-
[codecov-src]: https://flat.badgen.net/codecov/c/github/nuxt/test-utils
|
|
61
|
-
[codecov-href]: https://codecov.io/gh/nuxt/test-utils
|
|
3
|
+
Test utilities for Nuxt.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,91 +1,82 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
declare function expectModuleToBeCalledWith(method: ModuleContainerMethod, ...args: any[]): void;
|
|
8
|
-
declare function expectModuleNotToBeCalledWith(method: ModuleContainerMethod, ...args: any[]): void;
|
|
9
|
-
declare function expectFileToBeGenerated(path: string): void;
|
|
10
|
-
declare function expectFileNotToBeGenerated(path: string): void;
|
|
1
|
+
import * as playwright_core from 'playwright-core';
|
|
2
|
+
import { Browser, BrowserContextOptions, LaunchOptions } from 'playwright';
|
|
3
|
+
import { NuxtConfig, Nuxt } from '@nuxt/schema';
|
|
4
|
+
import { ExecaChildProcess } from 'execa';
|
|
5
|
+
import { Listener } from 'listhen';
|
|
6
|
+
import { FetchOptions } from 'ohmyfetch';
|
|
11
7
|
|
|
12
8
|
declare function createBrowser(): Promise<void>;
|
|
13
9
|
declare function getBrowser(): Promise<Browser>;
|
|
14
|
-
declare function createPage(path?: string, options?: BrowserContextOptions): Promise<Page>;
|
|
15
|
-
|
|
16
|
-
declare function build(): Promise<void>;
|
|
10
|
+
declare function createPage(path?: string, options?: BrowserContextOptions): Promise<playwright_core.Page>;
|
|
17
11
|
|
|
18
|
-
|
|
12
|
+
declare type TestRunner = 'vitest' | 'jest';
|
|
13
|
+
interface TestOptions {
|
|
19
14
|
testDir: string;
|
|
20
15
|
fixture: string;
|
|
21
16
|
configFile: string;
|
|
22
17
|
rootDir: string;
|
|
23
18
|
buildDir: string;
|
|
24
|
-
|
|
19
|
+
nuxtConfig: NuxtConfig;
|
|
25
20
|
build: boolean;
|
|
26
|
-
|
|
27
|
-
generateOptions: {
|
|
28
|
-
build: boolean;
|
|
29
|
-
init: boolean;
|
|
30
|
-
};
|
|
21
|
+
dev: boolean;
|
|
31
22
|
setupTimeout: number;
|
|
32
23
|
waitFor: number;
|
|
33
24
|
browser: boolean;
|
|
25
|
+
runner: TestRunner;
|
|
26
|
+
logLevel: number;
|
|
34
27
|
browserOptions: {
|
|
35
28
|
type: 'chromium' | 'firefox' | 'webkit';
|
|
36
29
|
launch?: LaunchOptions;
|
|
37
30
|
};
|
|
38
31
|
server: boolean;
|
|
39
32
|
}
|
|
40
|
-
interface
|
|
41
|
-
options:
|
|
42
|
-
nuxt?:
|
|
43
|
-
options: NuxtOptions;
|
|
44
|
-
listen: (port?: number, host?: string, socket?: string) => any;
|
|
45
|
-
ready: () => any;
|
|
46
|
-
close: (callback?: Function) => any;
|
|
47
|
-
resolver: any;
|
|
48
|
-
moduleContainer: any;
|
|
49
|
-
resolveAlias(path: string): string;
|
|
50
|
-
resolvePath(path: string, opts?: any): string;
|
|
51
|
-
renderRoute(...args: any[]): any;
|
|
52
|
-
renderAndGetWindow(url: string, opts?: any, config?: any): any;
|
|
53
|
-
};
|
|
33
|
+
interface TestContext {
|
|
34
|
+
options: TestOptions;
|
|
35
|
+
nuxt?: Nuxt;
|
|
54
36
|
browser?: Browser;
|
|
55
37
|
url?: string;
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
38
|
+
serverProcess?: ExecaChildProcess;
|
|
39
|
+
listener?: Listener;
|
|
40
|
+
}
|
|
41
|
+
interface TestHooks {
|
|
42
|
+
beforeEach: () => void;
|
|
43
|
+
afterEach: () => void;
|
|
44
|
+
afterAll: () => void;
|
|
45
|
+
setup: () => void;
|
|
46
|
+
ctx: TestContext;
|
|
59
47
|
}
|
|
60
|
-
declare function createContext(options: Partial<NuxtTestOptions>): NuxtTestContext;
|
|
61
|
-
declare function getContext(): NuxtTestContext;
|
|
62
|
-
declare function setContext(context: NuxtTestContext): NuxtTestContext;
|
|
63
|
-
|
|
64
|
-
declare function generate(): Promise<void>;
|
|
65
48
|
|
|
66
|
-
declare function
|
|
49
|
+
declare function createTestContext(options: Partial<TestOptions>): TestContext;
|
|
50
|
+
declare function useTestContext(): TestContext;
|
|
51
|
+
declare function setTestContext(context: TestContext): TestContext;
|
|
52
|
+
declare function isDev(): boolean;
|
|
67
53
|
|
|
68
|
-
declare function loadNuxt(): Promise<void>;
|
|
69
54
|
declare function loadFixture(): Promise<void>;
|
|
70
|
-
declare function
|
|
71
|
-
declare function getNuxt(): {
|
|
72
|
-
options: NuxtOptions;
|
|
73
|
-
listen: (port?: number, host?: string, socket?: string) => any;
|
|
74
|
-
ready: () => any;
|
|
75
|
-
close: (callback?: Function) => any;
|
|
76
|
-
resolver: any;
|
|
77
|
-
moduleContainer: any;
|
|
78
|
-
resolveAlias(path: string): string;
|
|
79
|
-
resolvePath(path: string, opts?: any): string;
|
|
80
|
-
renderRoute(...args: any[]): any;
|
|
81
|
-
renderAndGetWindow(url: string, opts?: any, config?: any): any;
|
|
82
|
-
};
|
|
55
|
+
declare function buildFixture(): Promise<void>;
|
|
83
56
|
|
|
84
|
-
declare function
|
|
85
|
-
declare function
|
|
57
|
+
declare function startServer(): Promise<void>;
|
|
58
|
+
declare function stopServer(): Promise<void>;
|
|
59
|
+
declare function fetch(path: string, options?: any): Promise<Response>;
|
|
60
|
+
declare function $fetch(path: string, options?: FetchOptions): Promise<any>;
|
|
86
61
|
declare function url(path: string): string;
|
|
87
62
|
|
|
88
|
-
declare function
|
|
89
|
-
|
|
63
|
+
declare function setupJest(hooks: TestHooks): void;
|
|
64
|
+
|
|
65
|
+
declare function setupVitest(hooks: TestHooks): Promise<void>;
|
|
66
|
+
|
|
67
|
+
declare const setupMaps: {
|
|
68
|
+
jest: typeof setupJest;
|
|
69
|
+
vitest: typeof setupVitest;
|
|
70
|
+
};
|
|
71
|
+
declare function createTest(options: Partial<TestOptions>): TestHooks;
|
|
72
|
+
declare function setup(options: Partial<TestOptions>): Promise<void>;
|
|
73
|
+
|
|
74
|
+
interface RunTestOptions {
|
|
75
|
+
rootDir: string;
|
|
76
|
+
dev?: boolean;
|
|
77
|
+
watch?: boolean;
|
|
78
|
+
runner?: 'vitest';
|
|
79
|
+
}
|
|
80
|
+
declare function runTests(opts: RunTestOptions): Promise<void>;
|
|
90
81
|
|
|
91
|
-
export {
|
|
82
|
+
export { $fetch, RunTestOptions, buildFixture, createBrowser, createPage, createTest, createTestContext, fetch, getBrowser, isDev, loadFixture, runTests, setTestContext, setup, setupMaps, startServer, stopServer, url, useTestContext };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import { resolve } from 'node:path';
|
|
2
|
+
import defu from 'defu';
|
|
3
|
+
import { execa } from 'execa';
|
|
4
|
+
import { getRandomPort, waitForPort } from 'get-port-please';
|
|
5
|
+
import { $fetch as $fetch$1, fetch as fetch$1 } from 'ohmyfetch';
|
|
6
|
+
import * as _kit from '@nuxt/kit';
|
|
7
|
+
import { promises, existsSync } from 'node:fs';
|
|
8
|
+
|
|
9
|
+
let currentContext;
|
|
10
|
+
function createTestContext(options) {
|
|
11
|
+
const _options = defu(options, {
|
|
12
|
+
testDir: resolve(process.cwd(), "test"),
|
|
13
|
+
fixture: "fixture",
|
|
14
|
+
configFile: "nuxt.config",
|
|
15
|
+
setupTimeout: 6e4,
|
|
16
|
+
dev: !!JSON.parse(process.env.NUXT_TEST_DEV || "false"),
|
|
17
|
+
logLevel: 1,
|
|
18
|
+
server: true,
|
|
19
|
+
build: options.browser !== false || options.server !== false,
|
|
20
|
+
nuxtConfig: {},
|
|
21
|
+
runner: "vitest",
|
|
22
|
+
browserOptions: {
|
|
23
|
+
type: "chromium"
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
return setTestContext({ options: _options });
|
|
27
|
+
}
|
|
28
|
+
function useTestContext() {
|
|
29
|
+
if (!currentContext) {
|
|
30
|
+
throw new Error("No context is available. (Forgot calling setup or createContext?)");
|
|
31
|
+
}
|
|
32
|
+
return currentContext;
|
|
33
|
+
}
|
|
34
|
+
function setTestContext(context) {
|
|
35
|
+
currentContext = context;
|
|
36
|
+
return currentContext;
|
|
37
|
+
}
|
|
38
|
+
function isDev() {
|
|
39
|
+
const ctx = useTestContext();
|
|
40
|
+
return ctx.options.dev;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const kit$1 = _kit.default || _kit;
|
|
44
|
+
async function startServer() {
|
|
45
|
+
const ctx = useTestContext();
|
|
46
|
+
await stopServer();
|
|
47
|
+
const port = await getRandomPort();
|
|
48
|
+
ctx.url = "http://localhost:" + port;
|
|
49
|
+
if (ctx.options.dev) {
|
|
50
|
+
const nuxiCLI = await kit$1.resolvePath("nuxi/cli");
|
|
51
|
+
ctx.serverProcess = execa(nuxiCLI, ["dev"], {
|
|
52
|
+
cwd: ctx.nuxt.options.rootDir,
|
|
53
|
+
stdio: "inherit",
|
|
54
|
+
env: {
|
|
55
|
+
...process.env,
|
|
56
|
+
PORT: String(port),
|
|
57
|
+
NODE_ENV: "development"
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
await waitForPort(port, { retries: 32 });
|
|
61
|
+
for (let i = 0; i < 50; i++) {
|
|
62
|
+
await new Promise((resolve2) => setTimeout(resolve2, 100));
|
|
63
|
+
try {
|
|
64
|
+
const res = await $fetch("/");
|
|
65
|
+
if (!res.includes("__NUXT_LOADING__")) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
} catch {
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
throw new Error("Timeout waiting for dev server!");
|
|
72
|
+
} else {
|
|
73
|
+
ctx.serverProcess = execa("node", [
|
|
74
|
+
resolve(ctx.nuxt.options.nitro.output.dir, "server/index.mjs")
|
|
75
|
+
], {
|
|
76
|
+
stdio: "inherit",
|
|
77
|
+
env: {
|
|
78
|
+
...process.env,
|
|
79
|
+
PORT: String(port),
|
|
80
|
+
NODE_ENV: "test"
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
await waitForPort(port, { retries: 8 });
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
async function stopServer() {
|
|
87
|
+
const ctx = useTestContext();
|
|
88
|
+
if (ctx.serverProcess) {
|
|
89
|
+
await ctx.serverProcess.kill();
|
|
90
|
+
}
|
|
91
|
+
if (ctx.listener) {
|
|
92
|
+
await ctx.listener.close();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function fetch(path, options) {
|
|
96
|
+
return fetch$1(url(path), options);
|
|
97
|
+
}
|
|
98
|
+
function $fetch(path, options) {
|
|
99
|
+
return $fetch$1(url(path), options);
|
|
100
|
+
}
|
|
101
|
+
function url(path) {
|
|
102
|
+
const ctx = useTestContext();
|
|
103
|
+
if (!ctx.url) {
|
|
104
|
+
throw new Error("url is not availabe (is server option enabled?)");
|
|
105
|
+
}
|
|
106
|
+
return ctx.url + path;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async function createBrowser() {
|
|
110
|
+
const ctx = useTestContext();
|
|
111
|
+
let playwright;
|
|
112
|
+
try {
|
|
113
|
+
playwright = await import(String("playwright"));
|
|
114
|
+
} catch {
|
|
115
|
+
throw new Error(`
|
|
116
|
+
The dependency 'playwright' not found.
|
|
117
|
+
Please run 'yarn add --dev playwright' or 'npm install --save-dev playwright'
|
|
118
|
+
`);
|
|
119
|
+
}
|
|
120
|
+
const { type, launch } = ctx.options.browserOptions;
|
|
121
|
+
if (!playwright[type]) {
|
|
122
|
+
throw new Error(`Invalid browser '${type}'`);
|
|
123
|
+
}
|
|
124
|
+
ctx.browser = await playwright[type].launch(launch);
|
|
125
|
+
}
|
|
126
|
+
async function getBrowser() {
|
|
127
|
+
const ctx = useTestContext();
|
|
128
|
+
if (!ctx.browser) {
|
|
129
|
+
await createBrowser();
|
|
130
|
+
}
|
|
131
|
+
return ctx.browser;
|
|
132
|
+
}
|
|
133
|
+
async function createPage(path, options) {
|
|
134
|
+
const browser = await getBrowser();
|
|
135
|
+
const page = await browser.newPage(options);
|
|
136
|
+
if (path) {
|
|
137
|
+
await page.goto(url(path));
|
|
138
|
+
}
|
|
139
|
+
return page;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const kit = _kit.default || _kit;
|
|
143
|
+
const isNuxtApp = (dir) => {
|
|
144
|
+
return existsSync(dir) && (existsSync(resolve(dir, "pages")) || existsSync(resolve(dir, "nuxt.config.js")) || existsSync(resolve(dir, "nuxt.config.ts")));
|
|
145
|
+
};
|
|
146
|
+
const resolveRootDir = () => {
|
|
147
|
+
const { options } = useTestContext();
|
|
148
|
+
const dirs = [
|
|
149
|
+
options.rootDir,
|
|
150
|
+
resolve(options.testDir, options.fixture),
|
|
151
|
+
process.cwd()
|
|
152
|
+
];
|
|
153
|
+
for (const dir of dirs) {
|
|
154
|
+
if (dir && isNuxtApp(dir)) {
|
|
155
|
+
return dir;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
throw new Error("Invalid nuxt app. (Please explicitly set `options.rootDir` pointing to a valid nuxt app)");
|
|
159
|
+
};
|
|
160
|
+
async function loadFixture() {
|
|
161
|
+
const ctx = useTestContext();
|
|
162
|
+
ctx.options.rootDir = resolveRootDir();
|
|
163
|
+
if (!ctx.options.dev) {
|
|
164
|
+
const randomId = Math.random().toString(36).slice(2, 8);
|
|
165
|
+
const buildDir = resolve(ctx.options.rootDir, ".nuxt", randomId);
|
|
166
|
+
Object.assign(ctx.options.nuxtConfig, {
|
|
167
|
+
buildDir,
|
|
168
|
+
nitro: {
|
|
169
|
+
output: {
|
|
170
|
+
dir: resolve(buildDir, "output")
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
ctx.nuxt = await kit.loadNuxt({
|
|
176
|
+
cwd: ctx.options.rootDir,
|
|
177
|
+
dev: ctx.options.dev,
|
|
178
|
+
overrides: ctx.options.nuxtConfig,
|
|
179
|
+
configFile: ctx.options.configFile
|
|
180
|
+
});
|
|
181
|
+
kit.logger.level = ctx.options.logLevel;
|
|
182
|
+
await promises.mkdir(ctx.nuxt.options.buildDir, { recursive: true });
|
|
183
|
+
}
|
|
184
|
+
async function buildFixture() {
|
|
185
|
+
const ctx = useTestContext();
|
|
186
|
+
await kit.buildNuxt(ctx.nuxt);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function setupJest(hooks) {
|
|
190
|
+
test("setup", hooks.setup, 120 * 1e3);
|
|
191
|
+
beforeEach(hooks.beforeEach);
|
|
192
|
+
afterEach(hooks.afterEach);
|
|
193
|
+
afterAll(hooks.afterAll);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async function setupVitest(hooks) {
|
|
197
|
+
const vitest = await import('vitest');
|
|
198
|
+
vitest.beforeAll(hooks.setup, 120 * 1e3);
|
|
199
|
+
vitest.beforeEach(hooks.beforeEach);
|
|
200
|
+
vitest.afterEach(hooks.afterEach);
|
|
201
|
+
vitest.afterAll(hooks.afterAll);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const setupMaps = {
|
|
205
|
+
jest: setupJest,
|
|
206
|
+
vitest: setupVitest
|
|
207
|
+
};
|
|
208
|
+
function createTest(options) {
|
|
209
|
+
const ctx = createTestContext(options);
|
|
210
|
+
const beforeEach = () => {
|
|
211
|
+
setTestContext(ctx);
|
|
212
|
+
};
|
|
213
|
+
const afterEach = () => {
|
|
214
|
+
setTestContext(void 0);
|
|
215
|
+
};
|
|
216
|
+
const afterAll = async () => {
|
|
217
|
+
if (ctx.serverProcess) {
|
|
218
|
+
setTestContext(ctx);
|
|
219
|
+
await stopServer();
|
|
220
|
+
setTestContext(void 0);
|
|
221
|
+
}
|
|
222
|
+
if (ctx.nuxt && ctx.nuxt.options.dev) {
|
|
223
|
+
await ctx.nuxt.close();
|
|
224
|
+
}
|
|
225
|
+
if (ctx.browser) {
|
|
226
|
+
await ctx.browser.close();
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
const setup2 = async () => {
|
|
230
|
+
if (ctx.options.fixture) {
|
|
231
|
+
await loadFixture();
|
|
232
|
+
}
|
|
233
|
+
if (ctx.options.build) {
|
|
234
|
+
await buildFixture();
|
|
235
|
+
}
|
|
236
|
+
if (ctx.options.server) {
|
|
237
|
+
await startServer();
|
|
238
|
+
}
|
|
239
|
+
if (ctx.options.waitFor) {
|
|
240
|
+
await new Promise((resolve) => setTimeout(resolve, ctx.options.waitFor));
|
|
241
|
+
}
|
|
242
|
+
if (ctx.options.browser) {
|
|
243
|
+
await createBrowser();
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
return {
|
|
247
|
+
beforeEach,
|
|
248
|
+
afterEach,
|
|
249
|
+
afterAll,
|
|
250
|
+
setup: setup2,
|
|
251
|
+
ctx
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
async function setup(options) {
|
|
255
|
+
const hooks = createTest(options);
|
|
256
|
+
const setupFn = setupMaps[hooks.ctx.options.runner];
|
|
257
|
+
await setupFn(hooks);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const RunTestDefaults = {
|
|
261
|
+
runner: "vitest"
|
|
262
|
+
};
|
|
263
|
+
async function runTests(opts) {
|
|
264
|
+
opts = { ...RunTestDefaults, ...opts };
|
|
265
|
+
if (opts.runner !== "vitest") {
|
|
266
|
+
throw new Error(`Unsupported runner: ${opts.runner}. Currently only vitest runner is supported.`);
|
|
267
|
+
}
|
|
268
|
+
if (opts.dev) {
|
|
269
|
+
process.env.NUXT_TEST_DEV = "true";
|
|
270
|
+
}
|
|
271
|
+
const { startVitest } = await import('vitest/dist/node.js');
|
|
272
|
+
const succeeded = await startVitest([], {
|
|
273
|
+
root: opts.rootDir,
|
|
274
|
+
run: !opts.watch
|
|
275
|
+
}, {
|
|
276
|
+
esbuild: {
|
|
277
|
+
tsconfigRaw: "{}"
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
if (!succeeded) {
|
|
281
|
+
process.exit(1);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export { $fetch, buildFixture, createBrowser, createPage, createTest, createTestContext, fetch, getBrowser, isDev, loadFixture, runTests, setTestContext, setup, setupMaps, startServer, stopServer, url, useTestContext };
|
package/package.json
CHANGED
|
@@ -1,46 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nuxt/test-utils",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"repository": "nuxt-community/module-test-utils",
|
|
3
|
+
"version": "3.0.0-rc.2",
|
|
4
|
+
"repository": "nuxt/framework",
|
|
6
5
|
"license": "MIT",
|
|
7
|
-
"
|
|
8
|
-
"
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": "./dist/index.mjs",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
9
|
"files": [
|
|
10
|
-
"dist"
|
|
11
|
-
"jest-preset.js"
|
|
10
|
+
"dist"
|
|
12
11
|
],
|
|
13
12
|
"scripts": {
|
|
14
|
-
"
|
|
15
|
-
"lint": "eslint --ext .ts,.js,.vue .",
|
|
16
|
-
"release": "yarn test && standard-version && yarn build && git push --follow-tags && npm publish",
|
|
17
|
-
"test": "yarn lint && yarn test:unit && yarn test:e2e",
|
|
18
|
-
"test:e2e": "jest test/e2e --forceExit",
|
|
19
|
-
"test:unit": "jest test/unit --forceExit"
|
|
13
|
+
"prepack": "unbuild"
|
|
20
14
|
},
|
|
21
15
|
"dependencies": {
|
|
22
|
-
"@
|
|
23
|
-
"
|
|
24
|
-
"defu": "^
|
|
25
|
-
"
|
|
26
|
-
"
|
|
16
|
+
"@nuxt/kit": "^3.0.0-rc.2",
|
|
17
|
+
"@nuxt/schema": "^3.0.0-rc.2",
|
|
18
|
+
"defu": "^6.0.0",
|
|
19
|
+
"execa": "^6.1.0",
|
|
20
|
+
"get-port-please": "^2.5.0",
|
|
21
|
+
"jiti": "^1.13.0",
|
|
22
|
+
"ohmyfetch": "^0.4.16"
|
|
27
23
|
},
|
|
28
24
|
"devDependencies": {
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
"
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
"
|
|
38
|
-
"eslint": "latest",
|
|
39
|
-
"jest": "latest",
|
|
40
|
-
"nuxt-edge": "latest",
|
|
41
|
-
"playwright": "latest",
|
|
42
|
-
"siroc": "latest",
|
|
43
|
-
"standard-version": "latest",
|
|
44
|
-
"typescript": "latest"
|
|
25
|
+
"playwright": "^1.21.1",
|
|
26
|
+
"unbuild": "latest",
|
|
27
|
+
"vitest": "^0.10.0"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"vue": "3.2.33"
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": "^14.16.0 || ^16.11.0 || ^17.0.0 || ^18.0.0"
|
|
45
34
|
}
|
|
46
35
|
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
|
-
|
|
5
|
-
### [0.2.2](https://github.com/nuxt-community/module-test-utils/compare/v0.2.1...v0.2.2) (2021-04-08)
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
### Features
|
|
9
|
-
|
|
10
|
-
* resolve rootDir ([#107](https://github.com/nuxt-community/module-test-utils/issues/107)) ([f604dff](https://github.com/nuxt-community/module-test-utils/commit/f604dff709bc523c93f1f17db7a972d5a5252c03))
|
|
11
|
-
|
|
12
|
-
### [0.2.1](https://github.com/nuxt-community/module-test-utils/compare/v0.2.0...v0.2.1) (2021-04-01)
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
### Bug Fixes
|
|
16
|
-
|
|
17
|
-
* detect `nuxt.config` with different extensions ([#105](https://github.com/nuxt-community/module-test-utils/issues/105)) ([006bebf](https://github.com/nuxt-community/module-test-utils/commit/006bebf4391487aa8186dd6e7e4b3813a7d93f8f))
|
|
18
|
-
* **types:** add more type definitions to nuxt interface ([#106](https://github.com/nuxt-community/module-test-utils/issues/106)) ([1a7d223](https://github.com/nuxt-community/module-test-utils/commit/1a7d223905729baf0eb15555c24fadfb5c072ee4))
|
|
19
|
-
|
|
20
|
-
## [0.2.0](https://github.com/nuxt-community/module-test-utils/compare/v0.1.3...v0.2.0) (2021-03-06)
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
### ⚠ BREAKING CHANGES
|
|
24
|
-
|
|
25
|
-
* use loadNuxtConfig
|
|
26
|
-
|
|
27
|
-
### Features
|
|
28
|
-
|
|
29
|
-
* add generated assertions ([#94](https://github.com/nuxt-community/module-test-utils/issues/94)) ([cc90f6b](https://github.com/nuxt-community/module-test-utils/commit/cc90f6b6935ec75921983214423cb0d4056c6007))
|
|
30
|
-
* mockConsola ([#91](https://github.com/nuxt-community/module-test-utils/issues/91)) ([c8e7940](https://github.com/nuxt-community/module-test-utils/commit/c8e7940f997038abcc767533674e8c27007046a2))
|
|
31
|
-
* use loadNuxtConfig ([111ec67](https://github.com/nuxt-community/module-test-utils/commit/111ec67d729edc404f14a54231479f567ba3f9a2)), closes [#87](https://github.com/nuxt-community/module-test-utils/issues/87)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
### Bug Fixes
|
|
35
|
-
|
|
36
|
-
* add missing @babel/preset-typescript dependency ([#93](https://github.com/nuxt-community/module-test-utils/issues/93)) ([8edf5c9](https://github.com/nuxt-community/module-test-utils/commit/8edf5c9b965dad96b44a0116169ab760db32273b))
|
|
37
|
-
|
|
38
|
-
### [0.1.3](https://github.com/nuxt-community/module-test-utils/compare/v0.1.2...v0.1.3) (2021-02-08)
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
### Bug Fixes
|
|
42
|
-
|
|
43
|
-
* **server:** try to use `config.server.port` ([#84](https://github.com/nuxt-community/module-test-utils/issues/84)) ([b017f8a](https://github.com/nuxt-community/module-test-utils/commit/b017f8adcc3eda41997b4582b25f2d5bb6d63fdd))
|
|
44
|
-
|
|
45
|
-
### [0.1.2](https://github.com/nuxt-community/module-test-utils/compare/v0.1.1...v0.1.2) (2020-12-15)
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
### Bug Fixes
|
|
49
|
-
|
|
50
|
-
* fix exported jest preset with node target ([66b9a88](https://github.com/nuxt-community/module-test-utils/commit/66b9a88a390b609ffc04c3aefa451e07c3c5e079))
|
|
51
|
-
|
|
52
|
-
### 0.1.1 (2020-12-15)
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
### Features
|
|
56
|
-
|
|
57
|
-
* add `init` helper ([0472396](https://github.com/nuxt-community/module-test-utils/commit/04723963d079228ad6298d4b1b1126312b284256))
|
|
58
|
-
* add negative assertions ([#57](https://github.com/nuxt-community/module-test-utils/issues/57)) ([0a5e206](https://github.com/nuxt-community/module-test-utils/commit/0a5e206fb2054c8794e20481567cd119bc3bf6c8))
|
|
59
|
-
* add option `beforeNuxtReady` ([8ed04e7](https://github.com/nuxt-community/module-test-utils/commit/8ed04e792e97afddda08660375b28233cc8a6375))
|
|
60
|
-
* add option `override` to `loadConfig` ([778cb07](https://github.com/nuxt-community/module-test-utils/commit/778cb0707164a439346ba1d05b69a36f595bcb34))
|
|
61
|
-
* add option `waitFor` ([0e787e3](https://github.com/nuxt-community/module-test-utils/commit/0e787e3a032948305f603d5a0dfcb450b96f3c2a))
|
|
62
|
-
* add options parameter on get function ([bf6dd20](https://github.com/nuxt-community/module-test-utils/commit/bf6dd200657e366820b04223deb1285a0027bc1d))
|
|
63
|
-
* add parameter `options` ([da04ea4](https://github.com/nuxt-community/module-test-utils/commit/da04ea4e1ed2d08faf763f92786a270b59ddb432))
|
|
64
|
-
* allow merging config overrides in loadConfig ([#2](https://github.com/nuxt-community/module-test-utils/issues/2)) ([ad61aef](https://github.com/nuxt-community/module-test-utils/commit/ad61aef7533effc320acf3ae9e5a7e760ed7819b)), closes [#1](https://github.com/nuxt-community/module-test-utils/issues/1)
|
|
65
|
-
* create jest preset ([#60](https://github.com/nuxt-community/module-test-utils/issues/60)) ([5bf4305](https://github.com/nuxt-community/module-test-utils/commit/5bf4305d2807257bb85ffeae36c23b0089ee36ee))
|
|
66
|
-
* custom browser ([#25](https://github.com/nuxt-community/module-test-utils/issues/25)) ([3e18e35](https://github.com/nuxt-community/module-test-utils/commit/3e18e35907023b57904d4c40b0491517c3532a4f))
|
|
67
|
-
* ensure puppeteer is installed ([#42](https://github.com/nuxt-community/module-test-utils/issues/42)) ([bc6d1e6](https://github.com/nuxt-community/module-test-utils/commit/bc6d1e6fa5b3b14da76489190647746f9f6b56a6))
|
|
68
|
-
* generate port ([#9](https://github.com/nuxt-community/module-test-utils/issues/9)) ([4940bd2](https://github.com/nuxt-community/module-test-utils/commit/4940bd2b836adce123be386664ac93b81007dc9c))
|
|
69
|
-
* inject `builder` and `generator` ([6818cce](https://github.com/nuxt-community/module-test-utils/commit/6818cced3f62be1086091e9c61de47cb22ec82ff))
|
|
70
|
-
* keep internal context ([#51](https://github.com/nuxt-community/module-test-utils/issues/51)) ([7f09991](https://github.com/nuxt-community/module-test-utils/commit/7f0999168c1d1dd3f734a7ff5a7dbd95c59d48e9))
|
|
71
|
-
* migrate to playwright ([#54](https://github.com/nuxt-community/module-test-utils/issues/54)) ([d82102d](https://github.com/nuxt-community/module-test-utils/commit/d82102dfaebc876098551963a424f8250b94ca64))
|
|
72
|
-
* request helper ([#39](https://github.com/nuxt-community/module-test-utils/issues/39)) ([7949aad](https://github.com/nuxt-community/module-test-utils/commit/7949aad900f47506c903abd5da7429b956b42a61))
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
### Bug Fixes
|
|
76
|
-
|
|
77
|
-
* **jest:** use node: current as target ([2f6efc8](https://github.com/nuxt-community/module-test-utils/commit/2f6efc8ebfd9432cb7dc68e407deac89c224b227))
|
|
78
|
-
* **setup:** listen before generate ([87aeb9a](https://github.com/nuxt-community/module-test-utils/commit/87aeb9a60abf372519db45faee87f5565415bf8c)), closes [#64](https://github.com/nuxt-community/module-test-utils/issues/64)
|
|
79
|
-
* use random `buildDir` to avoid conflicts ([#53](https://github.com/nuxt-community/module-test-utils/issues/53)) ([b627493](https://github.com/nuxt-community/module-test-utils/commit/b62749305d78d6c023f0c760fc8a8533df379fb7))
|
|
80
|
-
* **types:** correct types of get method options ([#22](https://github.com/nuxt-community/module-test-utils/issues/22)) ([f80d07a](https://github.com/nuxt-community/module-test-utils/commit/f80d07a98afb9c07f619ec9e1831b697faffc0cd))
|
|
81
|
-
* **types:** include types in published package ([#20](https://github.com/nuxt-community/module-test-utils/issues/20)) ([511a9fe](https://github.com/nuxt-community/module-test-utils/commit/511a9fe7fcfee06c69f1cef60e4b1e5eb4d8fd16))
|
|
82
|
-
* don't allow mutating original config object by the caller ([#5](https://github.com/nuxt-community/module-test-utils/issues/5)) ([642cd1b](https://github.com/nuxt-community/module-test-utils/commit/642cd1b53f1d762a795ab551f8e19de7ea28bd36))
|
|
83
|
-
* pass options as second paramter ([b3386ed](https://github.com/nuxt-community/module-test-utils/commit/b3386ede653e7ca4efa0fb8a7a40024f0c364d26))
|
|
84
|
-
* use `request-promise-native` ([982e562](https://github.com/nuxt-community/module-test-utils/commit/982e562529341f777aae7383ab5f90c31d8eec17))
|
|
85
|
-
* waitFor ([6f3dd54](https://github.com/nuxt-community/module-test-utils/commit/6f3dd543af7f1aee594d6365700615b8b1f52651))
|
|
86
|
-
|
|
87
|
-
### [0.0.1](https://github.com/nuxt-community/module-test-utils/compare/v2.0.0-3...v0.0.1) (2020-10-02)
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
### Features
|
|
91
|
-
|
|
92
|
-
* create jest preset ([#60](https://github.com/nuxt-community/module-test-utils/issues/60)) ([5bf4305](https://github.com/nuxt-community/module-test-utils/commit/5bf4305d2807257bb85ffeae36c23b0089ee36ee))
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
### Bug Fixes
|
|
96
|
-
|
|
97
|
-
* **setup:** listen before generate ([87aeb9a](https://github.com/nuxt-community/module-test-utils/commit/87aeb9a60abf372519db45faee87f5565415bf8c)), closes [#64](https://github.com/nuxt-community/module-test-utils/issues/64)
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) Nuxt.js
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
package/dist/index.js
DELETED
|
@@ -1,274 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
const fs = require('fs');
|
|
6
|
-
const path = require('path');
|
|
7
|
-
const defu = require('defu');
|
|
8
|
-
const getPort = require('get-port');
|
|
9
|
-
const got = require('got');
|
|
10
|
-
const consola = require('consola');
|
|
11
|
-
|
|
12
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
13
|
-
|
|
14
|
-
function _interopNamespace(e) {
|
|
15
|
-
if (e && e.__esModule) return e;
|
|
16
|
-
var n = Object.create(null);
|
|
17
|
-
if (e) {
|
|
18
|
-
Object.keys(e).forEach(function (k) {
|
|
19
|
-
if (k !== 'default') {
|
|
20
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
21
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
22
|
-
enumerable: true,
|
|
23
|
-
get: function () {
|
|
24
|
-
return e[k];
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
n['default'] = e;
|
|
31
|
-
return Object.freeze(n);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const defu__default = /*#__PURE__*/_interopDefaultLegacy(defu);
|
|
35
|
-
const getPort__default = /*#__PURE__*/_interopDefaultLegacy(getPort);
|
|
36
|
-
const got__default = /*#__PURE__*/_interopDefaultLegacy(got);
|
|
37
|
-
const consola__default = /*#__PURE__*/_interopDefaultLegacy(consola);
|
|
38
|
-
|
|
39
|
-
let currentContext;
|
|
40
|
-
function createContext(options) {
|
|
41
|
-
const _options = defu__default['default'](options, {
|
|
42
|
-
testDir: path.resolve(process.cwd(), "test"),
|
|
43
|
-
fixture: "fixture",
|
|
44
|
-
configFile: "nuxt.config",
|
|
45
|
-
setupTimeout: 6e4,
|
|
46
|
-
server: options.browser,
|
|
47
|
-
build: options.browser || options.server,
|
|
48
|
-
config: {},
|
|
49
|
-
browserOptions: {
|
|
50
|
-
type: "chromium"
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
return setContext({options: _options});
|
|
54
|
-
}
|
|
55
|
-
function getContext() {
|
|
56
|
-
if (!currentContext) {
|
|
57
|
-
throw new Error("No context is available. (Forgot calling setup or createContext?)");
|
|
58
|
-
}
|
|
59
|
-
return currentContext;
|
|
60
|
-
}
|
|
61
|
-
function setContext(context) {
|
|
62
|
-
currentContext = context;
|
|
63
|
-
return currentContext;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
async function loadNuxt() {
|
|
67
|
-
const ctx = getContext();
|
|
68
|
-
const {Nuxt} = await loadNuxtPackage();
|
|
69
|
-
ctx.nuxt = new Nuxt(ctx.options.config);
|
|
70
|
-
}
|
|
71
|
-
const isNuxtApp = (dir) => {
|
|
72
|
-
return fs.existsSync(dir) && (fs.existsSync(path.resolve(dir, "pages")) || fs.existsSync(path.resolve(dir, "nuxt.config.js")) || fs.existsSync(path.resolve(dir, "nuxt.config.ts")));
|
|
73
|
-
};
|
|
74
|
-
const resolveRootDir = () => {
|
|
75
|
-
const {options} = getContext();
|
|
76
|
-
const dirs = [
|
|
77
|
-
options.rootDir,
|
|
78
|
-
path.resolve(options.testDir, options.fixture),
|
|
79
|
-
process.cwd()
|
|
80
|
-
];
|
|
81
|
-
for (const dir of dirs) {
|
|
82
|
-
if (dir && isNuxtApp(dir)) {
|
|
83
|
-
return dir;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
throw new Error("Invalid nuxt app. (Please explicitly set `options.rootDir` pointing to a valid nuxt app)");
|
|
87
|
-
};
|
|
88
|
-
async function loadFixture() {
|
|
89
|
-
const {options} = getContext();
|
|
90
|
-
options.rootDir = resolveRootDir();
|
|
91
|
-
const {loadNuxtConfig} = await loadNuxtPackage();
|
|
92
|
-
options.config = await loadNuxtConfig({
|
|
93
|
-
rootDir: options.rootDir,
|
|
94
|
-
configFile: options.configFile,
|
|
95
|
-
configOverrides: options.config
|
|
96
|
-
});
|
|
97
|
-
if (!options.config.rootDir) {
|
|
98
|
-
options.config.rootDir = options.rootDir;
|
|
99
|
-
}
|
|
100
|
-
if (!options.config.buildDir) {
|
|
101
|
-
const randomId = Math.random().toString(36).substr(2, 8);
|
|
102
|
-
options.config.buildDir = path.resolve(options.rootDir, ".nuxt", randomId);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
async function loadNuxtPackage(name = "nuxt") {
|
|
106
|
-
return await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(name + "-edge")); }).catch(() => Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(name)); }));
|
|
107
|
-
}
|
|
108
|
-
function getNuxt() {
|
|
109
|
-
const ctx = getContext();
|
|
110
|
-
return ctx.nuxt;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function expectModuleToBeCalledWith(method, ...args) {
|
|
114
|
-
expect(getNuxt().moduleContainer[method]).toBeCalledWith(...args);
|
|
115
|
-
}
|
|
116
|
-
function expectModuleNotToBeCalledWith(method, ...args) {
|
|
117
|
-
expect(getNuxt().moduleContainer[method]).not.toBeCalledWith(...args);
|
|
118
|
-
}
|
|
119
|
-
function expectFileToBeGenerated(path$1) {
|
|
120
|
-
expect(fs.existsSync(path.resolve(getNuxt().options.generate.dir, path$1))).toBe(true);
|
|
121
|
-
}
|
|
122
|
-
function expectFileNotToBeGenerated(path$1) {
|
|
123
|
-
expect(fs.existsSync(path.resolve(getNuxt().options.generate.dir, path$1))).toBe(false);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
async function listen() {
|
|
127
|
-
const ctx = getContext();
|
|
128
|
-
const {server} = ctx.options.config;
|
|
129
|
-
const port = await getPort__default['default']({
|
|
130
|
-
...(server == null ? void 0 : server.port) && {port: Number(server == null ? void 0 : server.port)}
|
|
131
|
-
});
|
|
132
|
-
ctx.url = "http://localhost:" + port;
|
|
133
|
-
await ctx.nuxt.listen(port);
|
|
134
|
-
}
|
|
135
|
-
function get(path, options) {
|
|
136
|
-
return got__default['default'](url(path), options);
|
|
137
|
-
}
|
|
138
|
-
function url(path) {
|
|
139
|
-
const ctx = getContext();
|
|
140
|
-
if (!ctx.url) {
|
|
141
|
-
throw new Error("server is not enabled");
|
|
142
|
-
}
|
|
143
|
-
return ctx.url + path;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
async function createBrowser() {
|
|
147
|
-
const ctx = getContext();
|
|
148
|
-
let playwright;
|
|
149
|
-
try {
|
|
150
|
-
playwright = require("playwright");
|
|
151
|
-
} catch (e) {
|
|
152
|
-
throw new Error(`
|
|
153
|
-
The dependency 'playwright' not found.
|
|
154
|
-
Please run 'yarn add --dev playwright' or 'npm install --save-dev playwright'
|
|
155
|
-
`);
|
|
156
|
-
}
|
|
157
|
-
const {type, launch} = ctx.options.browserOptions;
|
|
158
|
-
if (!playwright[type]) {
|
|
159
|
-
throw new Error(`Invalid browser '${type}'`);
|
|
160
|
-
}
|
|
161
|
-
ctx.browser = await playwright[type].launch(launch);
|
|
162
|
-
}
|
|
163
|
-
async function getBrowser() {
|
|
164
|
-
const ctx = getContext();
|
|
165
|
-
if (!ctx.browser) {
|
|
166
|
-
await createBrowser();
|
|
167
|
-
}
|
|
168
|
-
return ctx.browser;
|
|
169
|
-
}
|
|
170
|
-
async function createPage(path, options) {
|
|
171
|
-
const browser = await getBrowser();
|
|
172
|
-
const page = await browser.newPage(options);
|
|
173
|
-
if (path) {
|
|
174
|
-
await page.goto(url(path));
|
|
175
|
-
}
|
|
176
|
-
return page;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
async function build() {
|
|
180
|
-
const ctx = getContext();
|
|
181
|
-
const {Builder} = await loadNuxtPackage();
|
|
182
|
-
ctx.builder = new Builder(ctx.nuxt);
|
|
183
|
-
await ctx.builder.build();
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
async function generate() {
|
|
187
|
-
const {options} = getContext();
|
|
188
|
-
const nuxt = getNuxt();
|
|
189
|
-
const {Builder, Generator} = await loadNuxtPackage();
|
|
190
|
-
const builder = new Builder(nuxt);
|
|
191
|
-
const generator = new Generator(nuxt, builder);
|
|
192
|
-
await generator.generate(options.generate);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
function mockConsola() {
|
|
196
|
-
const mock = {};
|
|
197
|
-
consola__default['default'].mockTypes((type) => {
|
|
198
|
-
mock[type] = mock[type] || jest.fn();
|
|
199
|
-
return mock[type];
|
|
200
|
-
});
|
|
201
|
-
return mock;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
function setupTest(options) {
|
|
205
|
-
const ctx = createContext(options);
|
|
206
|
-
beforeEach(() => {
|
|
207
|
-
setContext(ctx);
|
|
208
|
-
});
|
|
209
|
-
afterEach(() => {
|
|
210
|
-
setContext(void 0);
|
|
211
|
-
});
|
|
212
|
-
afterAll(async () => {
|
|
213
|
-
if (ctx.nuxt) {
|
|
214
|
-
await ctx.nuxt.close();
|
|
215
|
-
}
|
|
216
|
-
if (ctx.browser) {
|
|
217
|
-
await ctx.browser.close();
|
|
218
|
-
}
|
|
219
|
-
});
|
|
220
|
-
test("setup nuxt", async () => {
|
|
221
|
-
if (ctx.options.fixture) {
|
|
222
|
-
await loadFixture();
|
|
223
|
-
}
|
|
224
|
-
if (!ctx.nuxt) {
|
|
225
|
-
await loadNuxt();
|
|
226
|
-
spyOnClass(ctx.nuxt.moduleContainer);
|
|
227
|
-
await ctx.nuxt.ready();
|
|
228
|
-
}
|
|
229
|
-
if (ctx.options.build) {
|
|
230
|
-
await build();
|
|
231
|
-
}
|
|
232
|
-
if (ctx.options.server) {
|
|
233
|
-
await listen();
|
|
234
|
-
}
|
|
235
|
-
if (ctx.options.generate) {
|
|
236
|
-
await generate();
|
|
237
|
-
}
|
|
238
|
-
if (ctx.options.waitFor) {
|
|
239
|
-
await new Promise((resolve) => setTimeout(resolve, ctx.options.waitFor));
|
|
240
|
-
}
|
|
241
|
-
if (ctx.options.browser) {
|
|
242
|
-
await createBrowser();
|
|
243
|
-
}
|
|
244
|
-
}, ctx.options.setupTimeout);
|
|
245
|
-
}
|
|
246
|
-
function spyOnClass(instance) {
|
|
247
|
-
const proto = Object.getPrototypeOf(instance);
|
|
248
|
-
for (const key of Object.getOwnPropertyNames(proto)) {
|
|
249
|
-
jest.spyOn(instance, key);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
exports.build = build;
|
|
254
|
-
exports.createBrowser = createBrowser;
|
|
255
|
-
exports.createContext = createContext;
|
|
256
|
-
exports.createPage = createPage;
|
|
257
|
-
exports.expectFileNotToBeGenerated = expectFileNotToBeGenerated;
|
|
258
|
-
exports.expectFileToBeGenerated = expectFileToBeGenerated;
|
|
259
|
-
exports.expectModuleNotToBeCalledWith = expectModuleNotToBeCalledWith;
|
|
260
|
-
exports.expectModuleToBeCalledWith = expectModuleToBeCalledWith;
|
|
261
|
-
exports.generate = generate;
|
|
262
|
-
exports.get = get;
|
|
263
|
-
exports.getBrowser = getBrowser;
|
|
264
|
-
exports.getContext = getContext;
|
|
265
|
-
exports.getNuxt = getNuxt;
|
|
266
|
-
exports.listen = listen;
|
|
267
|
-
exports.loadFixture = loadFixture;
|
|
268
|
-
exports.loadNuxt = loadNuxt;
|
|
269
|
-
exports.loadNuxtPackage = loadNuxtPackage;
|
|
270
|
-
exports.mockConsola = mockConsola;
|
|
271
|
-
exports.setContext = setContext;
|
|
272
|
-
exports.setupTest = setupTest;
|
|
273
|
-
exports.spyOnClass = spyOnClass;
|
|
274
|
-
exports.url = url;
|
package/jest-preset.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
collectCoverage: true,
|
|
3
|
-
testEnvironment: 'node',
|
|
4
|
-
transform: {
|
|
5
|
-
'\\.(js|ts)$': [
|
|
6
|
-
'babel-jest',
|
|
7
|
-
{
|
|
8
|
-
presets: [
|
|
9
|
-
['@babel/preset-env', { targets: { node: 'current' } }],
|
|
10
|
-
'@babel/preset-typescript'
|
|
11
|
-
],
|
|
12
|
-
plugins: ['@babel/plugin-transform-runtime']
|
|
13
|
-
}
|
|
14
|
-
]
|
|
15
|
-
}
|
|
16
|
-
}
|