@depup/vitest__mocker 4.1.0-depup.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 +21 -0
- package/README.md +25 -0
- package/changes.json +5 -0
- package/dist/auto-register.d.ts +2 -0
- package/dist/auto-register.js +10 -0
- package/dist/automock.d.ts +13 -0
- package/dist/automock.js +8 -0
- package/dist/browser.d.ts +53 -0
- package/dist/browser.js +92 -0
- package/dist/chunk-automock.js +522 -0
- package/dist/chunk-helpers.js +44 -0
- package/dist/chunk-hoistMocks.js +659 -0
- package/dist/chunk-interceptor-native.js +15 -0
- package/dist/chunk-mocker.js +532 -0
- package/dist/chunk-pathe.M-eThtNZ.js +174 -0
- package/dist/chunk-registry.js +199 -0
- package/dist/chunk-utils.js +27 -0
- package/dist/hoistMocks.d-w2ILr1dG.d.ts +739 -0
- package/dist/index.d-B41z0AuW.d.ts +25 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +200 -0
- package/dist/mocker.d-QEntlm6J.d.ts +86 -0
- package/dist/node.d.ts +71 -0
- package/dist/node.js +409 -0
- package/dist/redirect.d.ts +3 -0
- package/dist/redirect.js +79 -0
- package/dist/register.d.ts +9 -0
- package/dist/register.js +42 -0
- package/dist/transforms.d.ts +8 -0
- package/dist/transforms.js +10 -0
- package/dist/types.d-BjI5eAwu.d.ts +123 -0
- package/package.json +111 -0
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
class MockerRegistry {
|
|
2
|
+
registryByUrl = new Map();
|
|
3
|
+
registryById = new Map();
|
|
4
|
+
clear() {
|
|
5
|
+
this.registryByUrl.clear();
|
|
6
|
+
this.registryById.clear();
|
|
7
|
+
}
|
|
8
|
+
keys() {
|
|
9
|
+
return this.registryByUrl.keys();
|
|
10
|
+
}
|
|
11
|
+
add(mock) {
|
|
12
|
+
this.registryByUrl.set(mock.url, mock);
|
|
13
|
+
this.registryById.set(mock.id, mock);
|
|
14
|
+
}
|
|
15
|
+
register(typeOrEvent, raw, id, url, factoryOrRedirect) {
|
|
16
|
+
const type = typeof typeOrEvent === "object" ? typeOrEvent.type : typeOrEvent;
|
|
17
|
+
if (typeof typeOrEvent === "object") {
|
|
18
|
+
const event = typeOrEvent;
|
|
19
|
+
if (event instanceof AutomockedModule || event instanceof AutospiedModule || event instanceof ManualMockedModule || event instanceof RedirectedModule) {
|
|
20
|
+
throw new TypeError(`[vitest] Cannot register a mock that is already defined. ` + `Expected a JSON representation from \`MockedModule.toJSON\`, instead got "${event.type}". ` + `Use "registry.add()" to update a mock instead.`);
|
|
21
|
+
}
|
|
22
|
+
if (event.type === "automock") {
|
|
23
|
+
const module = AutomockedModule.fromJSON(event);
|
|
24
|
+
this.add(module);
|
|
25
|
+
return module;
|
|
26
|
+
} else if (event.type === "autospy") {
|
|
27
|
+
const module = AutospiedModule.fromJSON(event);
|
|
28
|
+
this.add(module);
|
|
29
|
+
return module;
|
|
30
|
+
} else if (event.type === "redirect") {
|
|
31
|
+
const module = RedirectedModule.fromJSON(event);
|
|
32
|
+
this.add(module);
|
|
33
|
+
return module;
|
|
34
|
+
} else if (event.type === "manual") {
|
|
35
|
+
throw new Error(`Cannot set serialized manual mock. Define a factory function manually with \`ManualMockedModule.fromJSON()\`.`);
|
|
36
|
+
} else {
|
|
37
|
+
throw new Error(`Unknown mock type: ${event.type}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (typeof raw !== "string") {
|
|
41
|
+
throw new TypeError("[vitest] Mocks require a raw string.");
|
|
42
|
+
}
|
|
43
|
+
if (typeof url !== "string") {
|
|
44
|
+
throw new TypeError("[vitest] Mocks require a url string.");
|
|
45
|
+
}
|
|
46
|
+
if (typeof id !== "string") {
|
|
47
|
+
throw new TypeError("[vitest] Mocks require an id string.");
|
|
48
|
+
}
|
|
49
|
+
if (type === "manual") {
|
|
50
|
+
if (typeof factoryOrRedirect !== "function") {
|
|
51
|
+
throw new TypeError("[vitest] Manual mocks require a factory function.");
|
|
52
|
+
}
|
|
53
|
+
const mock = new ManualMockedModule(raw, id, url, factoryOrRedirect);
|
|
54
|
+
this.add(mock);
|
|
55
|
+
return mock;
|
|
56
|
+
} else if (type === "automock" || type === "autospy") {
|
|
57
|
+
const mock = type === "automock" ? new AutomockedModule(raw, id, url) : new AutospiedModule(raw, id, url);
|
|
58
|
+
this.add(mock);
|
|
59
|
+
return mock;
|
|
60
|
+
} else if (type === "redirect") {
|
|
61
|
+
if (typeof factoryOrRedirect !== "string") {
|
|
62
|
+
throw new TypeError("[vitest] Redirect mocks require a redirect string.");
|
|
63
|
+
}
|
|
64
|
+
const mock = new RedirectedModule(raw, id, url, factoryOrRedirect);
|
|
65
|
+
this.add(mock);
|
|
66
|
+
return mock;
|
|
67
|
+
} else {
|
|
68
|
+
throw new Error(`[vitest] Unknown mock type: ${type}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
delete(id) {
|
|
72
|
+
this.registryByUrl.delete(id);
|
|
73
|
+
}
|
|
74
|
+
deleteById(id) {
|
|
75
|
+
this.registryById.delete(id);
|
|
76
|
+
}
|
|
77
|
+
get(id) {
|
|
78
|
+
return this.registryByUrl.get(id);
|
|
79
|
+
}
|
|
80
|
+
getById(id) {
|
|
81
|
+
return this.registryById.get(id);
|
|
82
|
+
}
|
|
83
|
+
has(id) {
|
|
84
|
+
return this.registryByUrl.has(id);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
class AutomockedModule {
|
|
88
|
+
type = "automock";
|
|
89
|
+
constructor(raw, id, url) {
|
|
90
|
+
this.raw = raw;
|
|
91
|
+
this.id = id;
|
|
92
|
+
this.url = url;
|
|
93
|
+
}
|
|
94
|
+
static fromJSON(data) {
|
|
95
|
+
return new AutospiedModule(data.raw, data.id, data.url);
|
|
96
|
+
}
|
|
97
|
+
toJSON() {
|
|
98
|
+
return {
|
|
99
|
+
type: this.type,
|
|
100
|
+
url: this.url,
|
|
101
|
+
raw: this.raw,
|
|
102
|
+
id: this.id
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
class AutospiedModule {
|
|
107
|
+
type = "autospy";
|
|
108
|
+
constructor(raw, id, url) {
|
|
109
|
+
this.raw = raw;
|
|
110
|
+
this.id = id;
|
|
111
|
+
this.url = url;
|
|
112
|
+
}
|
|
113
|
+
static fromJSON(data) {
|
|
114
|
+
return new AutospiedModule(data.raw, data.id, data.url);
|
|
115
|
+
}
|
|
116
|
+
toJSON() {
|
|
117
|
+
return {
|
|
118
|
+
type: this.type,
|
|
119
|
+
url: this.url,
|
|
120
|
+
id: this.id,
|
|
121
|
+
raw: this.raw
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
class RedirectedModule {
|
|
126
|
+
type = "redirect";
|
|
127
|
+
constructor(raw, id, url, redirect) {
|
|
128
|
+
this.raw = raw;
|
|
129
|
+
this.id = id;
|
|
130
|
+
this.url = url;
|
|
131
|
+
this.redirect = redirect;
|
|
132
|
+
}
|
|
133
|
+
static fromJSON(data) {
|
|
134
|
+
return new RedirectedModule(data.raw, data.id, data.url, data.redirect);
|
|
135
|
+
}
|
|
136
|
+
toJSON() {
|
|
137
|
+
return {
|
|
138
|
+
type: this.type,
|
|
139
|
+
url: this.url,
|
|
140
|
+
raw: this.raw,
|
|
141
|
+
id: this.id,
|
|
142
|
+
redirect: this.redirect
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
class ManualMockedModule {
|
|
147
|
+
cache;
|
|
148
|
+
type = "manual";
|
|
149
|
+
constructor(raw, id, url, factory) {
|
|
150
|
+
this.raw = raw;
|
|
151
|
+
this.id = id;
|
|
152
|
+
this.url = url;
|
|
153
|
+
this.factory = factory;
|
|
154
|
+
}
|
|
155
|
+
resolve() {
|
|
156
|
+
if (this.cache) {
|
|
157
|
+
return this.cache;
|
|
158
|
+
}
|
|
159
|
+
let exports$1;
|
|
160
|
+
try {
|
|
161
|
+
exports$1 = this.factory();
|
|
162
|
+
} catch (err) {
|
|
163
|
+
throw createHelpfulError(err);
|
|
164
|
+
}
|
|
165
|
+
if (typeof exports$1 === "object" && typeof exports$1?.then === "function") {
|
|
166
|
+
return exports$1.then((result) => {
|
|
167
|
+
assertValidExports(this.raw, result);
|
|
168
|
+
return this.cache = result;
|
|
169
|
+
}, (error) => {
|
|
170
|
+
throw createHelpfulError(error);
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
assertValidExports(this.raw, exports$1);
|
|
174
|
+
return this.cache = exports$1;
|
|
175
|
+
}
|
|
176
|
+
static fromJSON(data, factory) {
|
|
177
|
+
return new ManualMockedModule(data.raw, data.id, data.url, factory);
|
|
178
|
+
}
|
|
179
|
+
toJSON() {
|
|
180
|
+
return {
|
|
181
|
+
type: this.type,
|
|
182
|
+
url: this.url,
|
|
183
|
+
id: this.id,
|
|
184
|
+
raw: this.raw
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
function createHelpfulError(cause) {
|
|
189
|
+
const error = new Error("[vitest] There was an error when mocking a module. " + "If you are using \"vi.mock\" factory, make sure there are no top level variables inside, since this call is hoisted to top of the file. " + "Read more: https://vitest.dev/api/vi.html#vi-mock");
|
|
190
|
+
error.cause = cause;
|
|
191
|
+
return error;
|
|
192
|
+
}
|
|
193
|
+
function assertValidExports(raw, exports$1) {
|
|
194
|
+
if (exports$1 === null || typeof exports$1 !== "object" || Array.isArray(exports$1)) {
|
|
195
|
+
throw new TypeError(`[vitest] vi.mock("${raw}", factory?: () => unknown) is not returning an object. Did you mean to return an object with a "default" key?`);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export { AutomockedModule as A, MockerRegistry as M, RedirectedModule as R, ManualMockedModule as a, AutospiedModule as b };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const postfixRE = /[?#].*$/;
|
|
2
|
+
function cleanUrl(url) {
|
|
3
|
+
return url.replace(postfixRE, "");
|
|
4
|
+
}
|
|
5
|
+
function createManualModuleSource(moduleUrl, exports$1, globalAccessor = "\"__vitest_mocker__\"") {
|
|
6
|
+
const source = `
|
|
7
|
+
const __factoryModule__ = await globalThis[${globalAccessor}].getFactoryModule("${moduleUrl}");
|
|
8
|
+
`;
|
|
9
|
+
const keys = exports$1.map((name, index) => {
|
|
10
|
+
return `let __${index} = __factoryModule__["${name}"]
|
|
11
|
+
export { __${index} as "${name}" }`;
|
|
12
|
+
}).join("\n");
|
|
13
|
+
let code = `${source}\n${keys}`;
|
|
14
|
+
// this prevents recursion
|
|
15
|
+
code += `
|
|
16
|
+
if (__factoryModule__.__factoryPromise != null) {
|
|
17
|
+
__factoryModule__.__factoryPromise.then((resolvedModule) => {
|
|
18
|
+
${exports$1.map((name, index) => {
|
|
19
|
+
return `__${index} = resolvedModule["${name}"];`;
|
|
20
|
+
}).join("\n")}
|
|
21
|
+
})
|
|
22
|
+
}
|
|
23
|
+
`;
|
|
24
|
+
return code;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { cleanUrl as a, createManualModuleSource as c };
|