@cedarjs/testing 0.0.4
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 +3 -0
- package/api/index.js +2 -0
- package/api/package.json +4 -0
- package/cache/index.js +2 -0
- package/cache/package.json +4 -0
- package/config/jest/api/RedwoodApiJestEnv.js +31 -0
- package/config/jest/api/apiBabelConfig.js +18 -0
- package/config/jest/api/globalSetup.js +46 -0
- package/config/jest/api/index.js +2 -0
- package/config/jest/api/jest-preset.js +67 -0
- package/config/jest/api/jest.setup.js +326 -0
- package/config/jest/jest-serial-runner.js +13 -0
- package/config/jest/web/RedwoodWebJestEnv.js +17 -0
- package/config/jest/web/index.js +2 -0
- package/config/jest/web/jest-preset.js +89 -0
- package/config/jest/web/jest.setup.js +41 -0
- package/config/jest/web/resolver.js +37 -0
- package/config/jest/web/webBabelConfig.js +3 -0
- package/dist/api/apiFunction.d.ts +37 -0
- package/dist/api/apiFunction.d.ts.map +1 -0
- package/dist/api/apiFunction.js +89 -0
- package/dist/api/directUrlHelpers.d.ts +3 -0
- package/dist/api/directUrlHelpers.d.ts.map +1 -0
- package/dist/api/directUrlHelpers.js +60 -0
- package/dist/api/directive.d.ts +51 -0
- package/dist/api/directive.d.ts.map +1 -0
- package/dist/api/directive.js +77 -0
- package/dist/api/index.d.ts +4 -0
- package/dist/api/index.d.ts.map +1 -0
- package/dist/api/index.js +26 -0
- package/dist/api/scenario.d.ts +108 -0
- package/dist/api/scenario.d.ts.map +1 -0
- package/dist/api/scenario.js +30 -0
- package/dist/cache/index.d.ts +50 -0
- package/dist/cache/index.d.ts.map +1 -0
- package/dist/cache/index.js +104 -0
- package/dist/web/MockParamsProvider.d.ts +7 -0
- package/dist/web/MockParamsProvider.d.ts.map +1 -0
- package/dist/web/MockParamsProvider.js +44 -0
- package/dist/web/MockProviders.d.ts +9 -0
- package/dist/web/MockProviders.d.ts.map +1 -0
- package/dist/web/MockProviders.js +53 -0
- package/dist/web/MockRouter.d.ts +14 -0
- package/dist/web/MockRouter.d.ts.map +1 -0
- package/dist/web/MockRouter.js +48 -0
- package/dist/web/customRender.d.ts +6 -0
- package/dist/web/customRender.d.ts.map +1 -0
- package/dist/web/customRender.js +54 -0
- package/dist/web/fileMock.d.ts +9 -0
- package/dist/web/fileMock.d.ts.map +1 -0
- package/dist/web/fileMock.js +24 -0
- package/dist/web/findCellMocks.d.ts +2 -0
- package/dist/web/findCellMocks.d.ts.map +1 -0
- package/dist/web/findCellMocks.js +45 -0
- package/dist/web/global.d.ts +6 -0
- package/dist/web/global.d.ts.map +1 -0
- package/dist/web/global.js +1 -0
- package/dist/web/index.d.ts +7 -0
- package/dist/web/index.d.ts.map +1 -0
- package/dist/web/index.js +42 -0
- package/dist/web/mockAuth.d.ts +29 -0
- package/dist/web/mockAuth.d.ts.map +1 -0
- package/dist/web/mockAuth.js +89 -0
- package/dist/web/mockRequests.d.ts +30 -0
- package/dist/web/mockRequests.d.ts.map +1 -0
- package/dist/web/mockRequests.js +129 -0
- package/package.json +67 -0
- package/web/index.js +2 -0
- package/web/package.json +4 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { StartOptions as StartMSWWorkerOptions, SharedOptions as SharedMSWOptions, MockedResponse, DefaultBodyType, RequestHandler, GraphQLContext, GraphQLRequest, ResponseTransformer } from 'msw';
|
|
2
|
+
/**
|
|
3
|
+
* Plugs fetch for the correct target in order to capture requests.
|
|
4
|
+
*
|
|
5
|
+
* Request handlers can be registered lazily (via `mockGraphQL<Query|Mutation>`),
|
|
6
|
+
* the queue will be drained and used.
|
|
7
|
+
*/
|
|
8
|
+
type StartOptions<Target> = Target extends 'browsers' ? StartMSWWorkerOptions : SharedMSWOptions;
|
|
9
|
+
export declare const startMSW: <Target extends "node" | "browsers">(target: Target, options?: StartOptions<Target>) => Promise<any>;
|
|
10
|
+
export declare const setupRequestHandlers: () => void;
|
|
11
|
+
export declare const closeServer: () => void;
|
|
12
|
+
export declare const registerHandler: (handler: RequestHandler) => void;
|
|
13
|
+
export type DataFunction<Query extends Record<string, unknown> = Record<string, unknown>, QueryVariables = Record<string, any>> = (variables: QueryVariables, { req, ctx, }: {
|
|
14
|
+
req: GraphQLRequest<any>;
|
|
15
|
+
ctx: GraphQLContext<Record<string, any>>;
|
|
16
|
+
}) => Query | void;
|
|
17
|
+
type ResponseFunction<BodyType extends DefaultBodyType = any> = (...transformers: ResponseTransformer<BodyType>[]) => MockedResponse<BodyType>;
|
|
18
|
+
type ResponseEnhancers = {
|
|
19
|
+
once: ResponseFunction<any>;
|
|
20
|
+
networkError: (message: string) => void;
|
|
21
|
+
};
|
|
22
|
+
type ResponseEnhancer = keyof ResponseEnhancers;
|
|
23
|
+
export declare const mockGraphQLQuery: <Query extends Record<string, unknown> = Record<string, unknown>, QueryVariables = Record<string, any>>(operation: string, data: DataFunction<Query, QueryVariables> | Query, responseEnhancer?: ResponseEnhancer) => Record<string, any> | DataFunction<Record<string, unknown>, Record<string, any>>;
|
|
24
|
+
export declare const mockGraphQLMutation: <Query extends Record<string, unknown> = Record<string, unknown>, QueryVariables = Record<string, any>>(operation: string, data: DataFunction<Query, QueryVariables> | Query, responseEnhancer?: ResponseEnhancer) => Record<string, any> | DataFunction<Record<string, unknown>, Record<string, any>>;
|
|
25
|
+
export declare const mockedUserMeta: {
|
|
26
|
+
currentUser: Record<string, unknown> | null;
|
|
27
|
+
};
|
|
28
|
+
export declare const mockCurrentUser: (user: Record<string, unknown> | null) => void;
|
|
29
|
+
export {};
|
|
30
|
+
//# sourceMappingURL=mockRequests.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mockRequests.d.ts","sourceRoot":"","sources":["../../src/web/mockRequests.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,YAAY,IAAI,qBAAqB,EACrC,aAAa,IAAI,gBAAgB,EACjC,cAAc,EACd,eAAe,EACf,cAAc,EACd,cAAc,EACd,cAAc,EACd,mB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C,cAAc,mCAEH,MAAM,QACX,YAAY,CAAC,KAAK,EAAE,cAAc,CAAC,GAAG,KAAK,qBAC9B,gBAAgB,qFAGpC,CAAA;AAED,eAAO,MAAM,mBAAmB,GAC9B,KAAK,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,4BACrC,cAAc,mCAEH,MAAM,QACX,YAAY,CAAC,KAAK,EAAE,cAAc,CAAC,GAAG,KAAK,qBAC9B,gBAAgB,qFAGpC,CAAA;AAED,eAAO,MAAM,cAAc,EAAE;IAAE,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAA;CAEzE,CAAA;AAED,eAAO,MAAM,eAAe,SAAU,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,SASnE,CAAA"}
|
|
@@ -0,0 +1,129 @@
|
|
|
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
|
+
var mockRequests_exports = {};
|
|
20
|
+
__export(mockRequests_exports, {
|
|
21
|
+
closeServer: () => closeServer,
|
|
22
|
+
mockCurrentUser: () => mockCurrentUser,
|
|
23
|
+
mockGraphQLMutation: () => mockGraphQLMutation,
|
|
24
|
+
mockGraphQLQuery: () => mockGraphQLQuery,
|
|
25
|
+
mockedUserMeta: () => mockedUserMeta,
|
|
26
|
+
registerHandler: () => registerHandler,
|
|
27
|
+
setupRequestHandlers: () => setupRequestHandlers,
|
|
28
|
+
startMSW: () => startMSW
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(mockRequests_exports);
|
|
31
|
+
var import_msw = require("msw");
|
|
32
|
+
let REQUEST_HANDLER_QUEUE = [];
|
|
33
|
+
let SERVER_INSTANCE;
|
|
34
|
+
const startMSW = async (target, options) => {
|
|
35
|
+
if (SERVER_INSTANCE) {
|
|
36
|
+
return SERVER_INSTANCE;
|
|
37
|
+
}
|
|
38
|
+
if (target === "browsers") {
|
|
39
|
+
SERVER_INSTANCE = (0, import_msw.setupWorker)();
|
|
40
|
+
await SERVER_INSTANCE.start(options);
|
|
41
|
+
} else {
|
|
42
|
+
const { setupServer } = require("msw/node");
|
|
43
|
+
SERVER_INSTANCE = setupServer();
|
|
44
|
+
await SERVER_INSTANCE.listen(options);
|
|
45
|
+
}
|
|
46
|
+
return SERVER_INSTANCE;
|
|
47
|
+
};
|
|
48
|
+
const setupRequestHandlers = () => {
|
|
49
|
+
SERVER_INSTANCE.resetHandlers();
|
|
50
|
+
for (const handler of REQUEST_HANDLER_QUEUE) {
|
|
51
|
+
SERVER_INSTANCE.use(handler);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
const closeServer = () => {
|
|
55
|
+
SERVER_INSTANCE.close();
|
|
56
|
+
};
|
|
57
|
+
const registerHandler = (handler) => {
|
|
58
|
+
if (!SERVER_INSTANCE) {
|
|
59
|
+
REQUEST_HANDLER_QUEUE = [...REQUEST_HANDLER_QUEUE, handler];
|
|
60
|
+
} else {
|
|
61
|
+
SERVER_INSTANCE.use(handler);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
const mockGraphQL = (type, operation, data, responseEnhancer) => {
|
|
65
|
+
const resolver = (req, res, ctx) => {
|
|
66
|
+
let d = data;
|
|
67
|
+
let responseTransforms = [];
|
|
68
|
+
if (typeof data === "function") {
|
|
69
|
+
const captureTransform = (fn) => {
|
|
70
|
+
return (...args) => {
|
|
71
|
+
const resTransform = fn(...args);
|
|
72
|
+
responseTransforms = [...responseTransforms, resTransform];
|
|
73
|
+
return resTransform;
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
const newCtx = {
|
|
77
|
+
status: captureTransform(ctx.status),
|
|
78
|
+
delay: captureTransform(ctx.delay),
|
|
79
|
+
errors: captureTransform(ctx.errors),
|
|
80
|
+
set: captureTransform(ctx.set),
|
|
81
|
+
fetch: captureTransform(ctx.fetch),
|
|
82
|
+
data: captureTransform(ctx.data),
|
|
83
|
+
extensions: captureTransform(ctx.extensions),
|
|
84
|
+
cookie: captureTransform(ctx.cookie),
|
|
85
|
+
field: captureTransform(ctx.field)
|
|
86
|
+
};
|
|
87
|
+
d = data(req.variables, {
|
|
88
|
+
req,
|
|
89
|
+
ctx: newCtx
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
return (responseEnhancer ? res[responseEnhancer] : res)(
|
|
93
|
+
ctx.data(d),
|
|
94
|
+
...responseTransforms
|
|
95
|
+
);
|
|
96
|
+
};
|
|
97
|
+
registerHandler(import_msw.graphql[type](operation, resolver));
|
|
98
|
+
return data;
|
|
99
|
+
};
|
|
100
|
+
const mockGraphQLQuery = (operation, data, responseEnhancer) => {
|
|
101
|
+
return mockGraphQL("query", operation, data, responseEnhancer);
|
|
102
|
+
};
|
|
103
|
+
const mockGraphQLMutation = (operation, data, responseEnhancer) => {
|
|
104
|
+
return mockGraphQL("mutation", operation, data, responseEnhancer);
|
|
105
|
+
};
|
|
106
|
+
const mockedUserMeta = {
|
|
107
|
+
currentUser: null
|
|
108
|
+
};
|
|
109
|
+
const mockCurrentUser = (user) => {
|
|
110
|
+
mockedUserMeta.currentUser = user;
|
|
111
|
+
mockGraphQLQuery("__REDWOOD__AUTH_GET_CURRENT_USER", () => {
|
|
112
|
+
return {
|
|
113
|
+
redwood: {
|
|
114
|
+
currentUser: user
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
});
|
|
118
|
+
};
|
|
119
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
120
|
+
0 && (module.exports = {
|
|
121
|
+
closeServer,
|
|
122
|
+
mockCurrentUser,
|
|
123
|
+
mockGraphQLMutation,
|
|
124
|
+
mockGraphQLQuery,
|
|
125
|
+
mockedUserMeta,
|
|
126
|
+
registerHandler,
|
|
127
|
+
setupRequestHandlers,
|
|
128
|
+
startMSW
|
|
129
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cedarjs/testing",
|
|
3
|
+
"version": "0.0.4",
|
|
4
|
+
"description": "Tools, wrappers and configuration for testing a CedarJS project.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/cedarjs/cedar.git",
|
|
8
|
+
"directory": "packages/testing"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"type": "commonjs",
|
|
12
|
+
"files": [
|
|
13
|
+
"config",
|
|
14
|
+
"web",
|
|
15
|
+
"api",
|
|
16
|
+
"cache",
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsx ./build.mts && yarn build:types",
|
|
21
|
+
"build:pack": "yarn pack -o cedar-testing.tgz",
|
|
22
|
+
"build:types": "tsc --build --verbose ./tsconfig.build.json",
|
|
23
|
+
"build:watch": "nodemon --watch src --ext 'js,jsx,ts,tsx' --ignore dist --exec 'yarn build'",
|
|
24
|
+
"check:attw": "yarn rw-fwtools-attw",
|
|
25
|
+
"check:package": "concurrently npm:check:attw yarn:publint",
|
|
26
|
+
"prepublishOnly": "NODE_ENV=production yarn build",
|
|
27
|
+
"test": "vitest run",
|
|
28
|
+
"test:watch": "vitest watch"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@cedarjs/auth": "0.0.4",
|
|
32
|
+
"@cedarjs/babel-config": "0.0.4",
|
|
33
|
+
"@cedarjs/context": "0.0.4",
|
|
34
|
+
"@cedarjs/graphql-server": "0.0.4",
|
|
35
|
+
"@cedarjs/project-config": "0.0.4",
|
|
36
|
+
"@cedarjs/router": "0.0.4",
|
|
37
|
+
"@cedarjs/web": "0.0.4",
|
|
38
|
+
"@testing-library/jest-dom": "6.5.0",
|
|
39
|
+
"@testing-library/react": "14.3.1",
|
|
40
|
+
"@testing-library/user-event": "14.5.2",
|
|
41
|
+
"@types/aws-lambda": "8.10.145",
|
|
42
|
+
"@types/babel-core": "6.25.10",
|
|
43
|
+
"@types/jest": "29.5.14",
|
|
44
|
+
"@types/node": "20.17.10",
|
|
45
|
+
"babel-jest": "^29.7.0",
|
|
46
|
+
"fast-glob": "3.3.2",
|
|
47
|
+
"jest": "29.7.0",
|
|
48
|
+
"jest-environment-jsdom": "29.7.0",
|
|
49
|
+
"jest-watch-typeahead": "2.2.2",
|
|
50
|
+
"msw": "1.3.4",
|
|
51
|
+
"ts-toolbelt": "9.6.0",
|
|
52
|
+
"whatwg-fetch": "3.6.20"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@cedarjs/framework-tools": "0.0.4",
|
|
56
|
+
"concurrently": "8.2.2",
|
|
57
|
+
"jsdom": "24.1.3",
|
|
58
|
+
"publint": "0.3.11",
|
|
59
|
+
"tsx": "4.19.3",
|
|
60
|
+
"typescript": "5.6.2",
|
|
61
|
+
"vitest": "2.1.9"
|
|
62
|
+
},
|
|
63
|
+
"publishConfig": {
|
|
64
|
+
"access": "public"
|
|
65
|
+
},
|
|
66
|
+
"gitHead": "5b4f77f985bd86ee31ee7338312627accf0cb85b"
|
|
67
|
+
}
|
package/web/index.js
ADDED
package/web/package.json
ADDED