@mpcore/vitest 0.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 ice breaker
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/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # @mpcore/vitest
2
+
3
+ Vitest integration for `@mpcore/test`. Add `mpcoreTest()` to the Vitest plugin list, then use `createMpcoreTest()` for an isolated mini-program project fixture per test.
4
+
5
+ ```ts
6
+ import { createMpcoreTest } from '@mpcore/vitest'
7
+
8
+ const test = createMpcoreTest({
9
+ artifact: { projectPath: process.cwd() },
10
+ })
11
+
12
+ test('renders a page', async ({ mpcore, expect }) => {
13
+ const result = await mpcore.renderPage('/pages/index/index')
14
+ expect(result.screen.getByText('ready')).toBeInTheMiniProgram()
15
+ })
16
+ ```
@@ -0,0 +1,35 @@
1
+ import { CreateTestProjectOptions, MiniProgramEmissionSource, MiniProgramNode, MiniProgramTestProject } from "@mpcore/test";
2
+ export * from "@mpcore/test";
3
+ //#region src/index.d.ts
4
+ interface MpcoreVitestFixture {
5
+ mpcore: MiniProgramTestProject;
6
+ }
7
+ interface MpcoreVitestPlugin {
8
+ config: (config: {
9
+ test?: {
10
+ setupFiles?: string | string[];
11
+ };
12
+ }) => {
13
+ test: {
14
+ setupFiles: string[];
15
+ };
16
+ };
17
+ name: string;
18
+ }
19
+ declare function mpcoreTest(): MpcoreVitestPlugin;
20
+ declare function createMpcoreTest(options: CreateTestProjectOptions): import("vitest").TestAPI<{
21
+ mpcore: MiniProgramTestProject;
22
+ }>;
23
+ declare function createVitestProject(options: CreateTestProjectOptions): MiniProgramTestProject;
24
+ declare function registerMpcoreMatchers(): void;
25
+ declare module 'vitest' {
26
+ interface Assertion<T = any> {
27
+ toBeInTheMiniProgram: T extends MiniProgramNode ? () => void : never;
28
+ toHaveAttribute: T extends MiniProgramNode ? (name: string, value?: string) => void : never;
29
+ toHaveDataset: T extends MiniProgramNode ? (dataset: Record<string, unknown>) => void : never;
30
+ toHaveEmitted: T extends MiniProgramEmissionSource ? (eventName: string, detail?: unknown) => void : never;
31
+ toHaveTextContent: T extends MiniProgramNode ? (value: string | RegExp) => void : never;
32
+ }
33
+ }
34
+ //#endregion
35
+ export { MpcoreVitestFixture, MpcoreVitestPlugin, createMpcoreTest, createVitestProject, mpcoreTest, registerMpcoreMatchers };
package/dist/index.mjs ADDED
@@ -0,0 +1,40 @@
1
+ import { fileURLToPath } from "node:url";
2
+ import { createTestProject, mpcoreMatchers } from "@mpcore/test";
3
+ import { expect, onTestFinished, test } from "vitest";
4
+ export * from "@mpcore/test";
5
+ //#region src/index.ts
6
+ function toList(value) {
7
+ if (!value) return [];
8
+ return Array.isArray(value) ? value : [value];
9
+ }
10
+ function mpcoreTest() {
11
+ const setupFile = fileURLToPath(new URL("./setup.mjs", import.meta.url));
12
+ return {
13
+ name: "mpcore:vitest",
14
+ config(config) {
15
+ return { test: { setupFiles: [...toList(config.test?.setupFiles), setupFile] } };
16
+ }
17
+ };
18
+ }
19
+ function createMpcoreTest(options) {
20
+ return test.extend({ mpcore: async ({}, use) => {
21
+ const project = createTestProject(options);
22
+ try {
23
+ await use(project);
24
+ } finally {
25
+ await project.close();
26
+ }
27
+ } });
28
+ }
29
+ function createVitestProject(options) {
30
+ const project = createTestProject(options);
31
+ onTestFinished(async () => {
32
+ await project.close();
33
+ });
34
+ return project;
35
+ }
36
+ function registerMpcoreMatchers() {
37
+ expect.extend(mpcoreMatchers);
38
+ }
39
+ //#endregion
40
+ export { createMpcoreTest, createVitestProject, mpcoreTest, registerMpcoreMatchers };
@@ -0,0 +1 @@
1
+ export {}
package/dist/setup.mjs ADDED
@@ -0,0 +1,6 @@
1
+ import { mpcoreMatchers } from "@mpcore/test";
2
+ import { expect } from "vitest";
3
+ //#region src/setup.ts
4
+ expect.extend(mpcoreMatchers);
5
+ //#endregion
6
+ export {};
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@mpcore/vitest",
3
+ "type": "module",
4
+ "version": "0.0.0",
5
+ "description": "Vitest integration for @mpcore/test",
6
+ "author": "ice breaker <1324318532@qq.com>",
7
+ "license": "MIT",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/weapp-vite/weapp-vite.git",
11
+ "directory": "mpcore/packages/vitest"
12
+ },
13
+ "sideEffects": false,
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.mts",
17
+ "import": "./dist/index.mjs"
18
+ },
19
+ "./setup": {
20
+ "types": "./dist/setup.d.mts",
21
+ "import": "./dist/setup.mjs"
22
+ }
23
+ },
24
+ "main": "./dist/index.mjs",
25
+ "module": "./dist/index.mjs",
26
+ "types": "./dist/index.d.mts",
27
+ "files": [
28
+ "README.md",
29
+ "dist"
30
+ ],
31
+ "engines": {
32
+ "node": "^20.19.0 || >=22.12.0"
33
+ },
34
+ "peerDependencies": {
35
+ "vitest": ">=4.0.0"
36
+ },
37
+ "dependencies": {
38
+ "@mpcore/test": "0.0.0"
39
+ },
40
+ "devDependencies": {
41
+ "vitest": "~4.1.10"
42
+ },
43
+ "publishConfig": {
44
+ "access": "public",
45
+ "registry": "https://registry.npmjs.org"
46
+ },
47
+ "scripts": {
48
+ "build": "tsdown",
49
+ "dev": "tsdown -w",
50
+ "test": "vitest run",
51
+ "test:types": "pnpm build && tsd",
52
+ "typecheck": "tsc --noEmit"
53
+ }
54
+ }