@spoosh/test-utils 0.1.0-beta.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) 2025 Spoosh
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,71 @@
1
+ # @spoosh/test-utils
2
+
3
+ Test utilities for Spoosh plugins.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install -D @spoosh/test-utils
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```typescript
14
+ import {
15
+ createMockContext,
16
+ createMockResponse,
17
+ createMockNext,
18
+ createState,
19
+ createStateManager,
20
+ createEventEmitter,
21
+ } from "@spoosh/test-utils";
22
+
23
+ describe("myPlugin", () => {
24
+ it("should work", async () => {
25
+ const plugin = myPlugin();
26
+ const context = createMockContext({
27
+ queryKey: "test-key",
28
+ pluginOptions: { myOption: true },
29
+ });
30
+ const next = createMockNext({ data: { id: 1 } });
31
+
32
+ const result = await plugin.middleware!(context, next);
33
+
34
+ expect(result.data).toEqual({ id: 1 });
35
+ });
36
+ });
37
+ ```
38
+
39
+ ## API
40
+
41
+ ### `createMockContext(options?)`
42
+
43
+ Creates a mock `PluginContext` for testing plugins.
44
+
45
+ ### `createMockResponse(overrides?)`
46
+
47
+ Creates a mock `SpooshResponse`.
48
+
49
+ ### `createMockNext(response?)`
50
+
51
+ Creates a mock next function that returns a successful response.
52
+
53
+ ### `createMockNextError(error)`
54
+
55
+ Creates a mock next function that throws an error.
56
+
57
+ ### `createMockNextWithError(error, status?)`
58
+
59
+ Creates a mock next function that returns an error response.
60
+
61
+ ### `createState(overrides?)`
62
+
63
+ Creates a default `OperationState` with optional overrides.
64
+
65
+ ### `createStateManager()`
66
+
67
+ Re-exported from `@spoosh/core` for convenience.
68
+
69
+ ### `createEventEmitter()`
70
+
71
+ Re-exported from `@spoosh/core` for convenience.
package/dist/index.cjs ADDED
@@ -0,0 +1,115 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ createEventEmitter: () => import_core.createEventEmitter,
24
+ createMockContext: () => createMockContext,
25
+ createMockNext: () => createMockNext,
26
+ createMockNextError: () => createMockNextError,
27
+ createMockNextWithError: () => createMockNextWithError,
28
+ createMockResponse: () => createMockResponse,
29
+ createState: () => createState,
30
+ createStateManager: () => import_core.createStateManager
31
+ });
32
+ module.exports = __toCommonJS(src_exports);
33
+ var import_core = require("@spoosh/core");
34
+ var import_vitest = require("vitest");
35
+ function createState(overrides = {}) {
36
+ return {
37
+ data: void 0,
38
+ error: void 0,
39
+ timestamp: 0,
40
+ ...overrides
41
+ };
42
+ }
43
+ function createMockContext(options = {}) {
44
+ const {
45
+ stateManager = (0, import_core.createStateManager)(),
46
+ eventEmitter = (0, import_core.createEventEmitter)(),
47
+ queryKey = '{"method":"GET","path":["test"]}',
48
+ path = ["test"],
49
+ method = "GET",
50
+ tags = ["test"],
51
+ operationType = "read",
52
+ pluginOptions,
53
+ forceRefetch,
54
+ hookId,
55
+ state: stateOverrides,
56
+ requestOptions = {},
57
+ metadata = /* @__PURE__ */ new Map(),
58
+ plugins = { get: import_vitest.vi.fn() }
59
+ } = options;
60
+ const state = createState(stateOverrides);
61
+ return {
62
+ operationType,
63
+ path,
64
+ method,
65
+ queryKey,
66
+ tags,
67
+ requestTimestamp: Date.now(),
68
+ requestOptions,
69
+ state,
70
+ metadata,
71
+ abort: import_vitest.vi.fn(),
72
+ stateManager,
73
+ eventEmitter,
74
+ headers: {},
75
+ setHeaders: import_vitest.vi.fn(),
76
+ plugins,
77
+ pluginOptions,
78
+ forceRefetch,
79
+ hookId
80
+ };
81
+ }
82
+ function createMockResponse(overrides = {}) {
83
+ return {
84
+ status: 200,
85
+ data: void 0,
86
+ error: void 0,
87
+ ...overrides
88
+ };
89
+ }
90
+ function createMockNext(response) {
91
+ return import_vitest.vi.fn().mockResolvedValue(createMockResponse(response));
92
+ }
93
+ function createMockNextError(error) {
94
+ return import_vitest.vi.fn().mockRejectedValue(error);
95
+ }
96
+ function createMockNextWithError(error, status = 500) {
97
+ return import_vitest.vi.fn().mockResolvedValue(
98
+ createMockResponse({
99
+ status,
100
+ data: void 0,
101
+ error
102
+ })
103
+ );
104
+ }
105
+ // Annotate the CommonJS export names for ESM import in node:
106
+ 0 && (module.exports = {
107
+ createEventEmitter,
108
+ createMockContext,
109
+ createMockNext,
110
+ createMockNextError,
111
+ createMockNextWithError,
112
+ createMockResponse,
113
+ createState,
114
+ createStateManager
115
+ });
@@ -0,0 +1,51 @@
1
+ import { OperationState, StateManager, EventEmitter, PluginContext, SpooshResponse } from '@spoosh/core';
2
+ export { createEventEmitter, createStateManager } from '@spoosh/core';
3
+ import { vi, Mock } from 'vitest';
4
+
5
+ /**
6
+ * Creates a default OperationState with optional overrides.
7
+ */
8
+ declare function createState<TData = unknown, TError = unknown>(overrides?: Partial<OperationState<TData, TError>>): OperationState<TData, TError>;
9
+ type MockContextOptions<TData = unknown, TError = unknown> = {
10
+ stateManager?: StateManager;
11
+ eventEmitter?: EventEmitter;
12
+ queryKey?: string;
13
+ path?: string[];
14
+ method?: string;
15
+ tags?: string[];
16
+ operationType?: "read" | "write" | "infiniteRead";
17
+ pluginOptions?: unknown;
18
+ forceRefetch?: boolean;
19
+ hookId?: string;
20
+ state?: Partial<OperationState<TData, TError>>;
21
+ requestOptions?: Record<string, unknown>;
22
+ /** Custom metadata map for the context */
23
+ metadata?: Map<string, unknown>;
24
+ /** Custom plugins object with get function */
25
+ plugins?: {
26
+ get: ReturnType<typeof vi.fn>;
27
+ };
28
+ };
29
+ /**
30
+ * Creates a mock PluginContext for testing plugins.
31
+ */
32
+ declare function createMockContext<TData = unknown, TError = unknown>(options?: MockContextOptions<TData, TError>): PluginContext<TData, TError>;
33
+ /**
34
+ * Creates a mock SpooshResponse.
35
+ */
36
+ declare function createMockResponse<TData = unknown, TError = unknown>(overrides?: Partial<SpooshResponse<TData, TError>>): SpooshResponse<TData, TError>;
37
+ type MockNextFn<TData = unknown, TError = unknown> = Mock<() => Promise<SpooshResponse<TData, TError>>>;
38
+ /**
39
+ * Creates a mock next function for middleware testing.
40
+ */
41
+ declare function createMockNext<TData = unknown, TError = unknown>(response?: Partial<SpooshResponse<TData, TError>>): MockNextFn<TData, TError>;
42
+ /**
43
+ * Creates a mock next function that throws an error.
44
+ */
45
+ declare function createMockNextError<TError = Error>(error: TError): MockNextFn<never, TError>;
46
+ /**
47
+ * Creates a mock next function that returns an error response.
48
+ */
49
+ declare function createMockNextWithError<TData = unknown, TError = unknown>(error: TError, status?: number): MockNextFn<TData, TError>;
50
+
51
+ export { type MockContextOptions, type MockNextFn, createMockContext, createMockNext, createMockNextError, createMockNextWithError, createMockResponse, createState };
@@ -0,0 +1,51 @@
1
+ import { OperationState, StateManager, EventEmitter, PluginContext, SpooshResponse } from '@spoosh/core';
2
+ export { createEventEmitter, createStateManager } from '@spoosh/core';
3
+ import { vi, Mock } from 'vitest';
4
+
5
+ /**
6
+ * Creates a default OperationState with optional overrides.
7
+ */
8
+ declare function createState<TData = unknown, TError = unknown>(overrides?: Partial<OperationState<TData, TError>>): OperationState<TData, TError>;
9
+ type MockContextOptions<TData = unknown, TError = unknown> = {
10
+ stateManager?: StateManager;
11
+ eventEmitter?: EventEmitter;
12
+ queryKey?: string;
13
+ path?: string[];
14
+ method?: string;
15
+ tags?: string[];
16
+ operationType?: "read" | "write" | "infiniteRead";
17
+ pluginOptions?: unknown;
18
+ forceRefetch?: boolean;
19
+ hookId?: string;
20
+ state?: Partial<OperationState<TData, TError>>;
21
+ requestOptions?: Record<string, unknown>;
22
+ /** Custom metadata map for the context */
23
+ metadata?: Map<string, unknown>;
24
+ /** Custom plugins object with get function */
25
+ plugins?: {
26
+ get: ReturnType<typeof vi.fn>;
27
+ };
28
+ };
29
+ /**
30
+ * Creates a mock PluginContext for testing plugins.
31
+ */
32
+ declare function createMockContext<TData = unknown, TError = unknown>(options?: MockContextOptions<TData, TError>): PluginContext<TData, TError>;
33
+ /**
34
+ * Creates a mock SpooshResponse.
35
+ */
36
+ declare function createMockResponse<TData = unknown, TError = unknown>(overrides?: Partial<SpooshResponse<TData, TError>>): SpooshResponse<TData, TError>;
37
+ type MockNextFn<TData = unknown, TError = unknown> = Mock<() => Promise<SpooshResponse<TData, TError>>>;
38
+ /**
39
+ * Creates a mock next function for middleware testing.
40
+ */
41
+ declare function createMockNext<TData = unknown, TError = unknown>(response?: Partial<SpooshResponse<TData, TError>>): MockNextFn<TData, TError>;
42
+ /**
43
+ * Creates a mock next function that throws an error.
44
+ */
45
+ declare function createMockNextError<TError = Error>(error: TError): MockNextFn<never, TError>;
46
+ /**
47
+ * Creates a mock next function that returns an error response.
48
+ */
49
+ declare function createMockNextWithError<TData = unknown, TError = unknown>(error: TError, status?: number): MockNextFn<TData, TError>;
50
+
51
+ export { type MockContextOptions, type MockNextFn, createMockContext, createMockNext, createMockNextError, createMockNextWithError, createMockResponse, createState };
package/dist/index.js ADDED
@@ -0,0 +1,86 @@
1
+ // src/index.ts
2
+ import {
3
+ createStateManager,
4
+ createEventEmitter
5
+ } from "@spoosh/core";
6
+ import { vi } from "vitest";
7
+ function createState(overrides = {}) {
8
+ return {
9
+ data: void 0,
10
+ error: void 0,
11
+ timestamp: 0,
12
+ ...overrides
13
+ };
14
+ }
15
+ function createMockContext(options = {}) {
16
+ const {
17
+ stateManager = createStateManager(),
18
+ eventEmitter = createEventEmitter(),
19
+ queryKey = '{"method":"GET","path":["test"]}',
20
+ path = ["test"],
21
+ method = "GET",
22
+ tags = ["test"],
23
+ operationType = "read",
24
+ pluginOptions,
25
+ forceRefetch,
26
+ hookId,
27
+ state: stateOverrides,
28
+ requestOptions = {},
29
+ metadata = /* @__PURE__ */ new Map(),
30
+ plugins = { get: vi.fn() }
31
+ } = options;
32
+ const state = createState(stateOverrides);
33
+ return {
34
+ operationType,
35
+ path,
36
+ method,
37
+ queryKey,
38
+ tags,
39
+ requestTimestamp: Date.now(),
40
+ requestOptions,
41
+ state,
42
+ metadata,
43
+ abort: vi.fn(),
44
+ stateManager,
45
+ eventEmitter,
46
+ headers: {},
47
+ setHeaders: vi.fn(),
48
+ plugins,
49
+ pluginOptions,
50
+ forceRefetch,
51
+ hookId
52
+ };
53
+ }
54
+ function createMockResponse(overrides = {}) {
55
+ return {
56
+ status: 200,
57
+ data: void 0,
58
+ error: void 0,
59
+ ...overrides
60
+ };
61
+ }
62
+ function createMockNext(response) {
63
+ return vi.fn().mockResolvedValue(createMockResponse(response));
64
+ }
65
+ function createMockNextError(error) {
66
+ return vi.fn().mockRejectedValue(error);
67
+ }
68
+ function createMockNextWithError(error, status = 500) {
69
+ return vi.fn().mockResolvedValue(
70
+ createMockResponse({
71
+ status,
72
+ data: void 0,
73
+ error
74
+ })
75
+ );
76
+ }
77
+ export {
78
+ createEventEmitter,
79
+ createMockContext,
80
+ createMockNext,
81
+ createMockNextError,
82
+ createMockNextWithError,
83
+ createMockResponse,
84
+ createState,
85
+ createStateManager
86
+ };
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@spoosh/test-utils",
3
+ "version": "0.1.0-beta.0",
4
+ "description": "Test utilities for Spoosh plugins",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/nxnom/spoosh.git",
9
+ "directory": "packages/test-utils"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/nxnom/spoosh/issues"
13
+ },
14
+ "homepage": "https://spoosh.dev",
15
+ "keywords": [
16
+ "spoosh",
17
+ "test-utils",
18
+ "testing",
19
+ "vitest",
20
+ "mock"
21
+ ],
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "type": "module",
26
+ "main": "./dist/index.cjs",
27
+ "module": "./dist/index.js",
28
+ "types": "./dist/index.d.ts",
29
+ "exports": {
30
+ ".": {
31
+ "import": {
32
+ "types": "./dist/index.d.ts",
33
+ "default": "./dist/index.js"
34
+ },
35
+ "require": {
36
+ "types": "./dist/index.d.cts",
37
+ "default": "./dist/index.cjs"
38
+ }
39
+ }
40
+ },
41
+ "files": [
42
+ "dist"
43
+ ],
44
+ "dependencies": {
45
+ "@spoosh/core": "0.1.0-beta.0"
46
+ },
47
+ "peerDependencies": {
48
+ "vitest": ">=1.0.0"
49
+ },
50
+ "devDependencies": {
51
+ "vitest": "^4.0.17"
52
+ },
53
+ "scripts": {
54
+ "build": "tsup",
55
+ "typecheck": "tsc --noEmit",
56
+ "lint": "eslint src --max-warnings 0"
57
+ }
58
+ }