aboutsummaryrefslogtreecommitdiffstats
path: root/vanilla/node_modules/@vitest/mocker/dist/chunk-registry.js
blob: fe0e803ac5e1635e6ae6c210973ec713c0fdff38 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
class MockerRegistry {
	registryByUrl = new Map();
	registryById = new Map();
	clear() {
		this.registryByUrl.clear();
		this.registryById.clear();
	}
	keys() {
		return this.registryByUrl.keys();
	}
	add(mock) {
		this.registryByUrl.set(mock.url, mock);
		this.registryById.set(mock.id, mock);
	}
	register(typeOrEvent, raw, id, url, factoryOrRedirect) {
		const type = typeof typeOrEvent === "object" ? typeOrEvent.type : typeOrEvent;
		if (typeof typeOrEvent === "object") {
			const event = typeOrEvent;
			if (event instanceof AutomockedModule || event instanceof AutospiedModule || event instanceof ManualMockedModule || event instanceof RedirectedModule) {
				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.`);
			}
			if (event.type === "automock") {
				const module = AutomockedModule.fromJSON(event);
				this.add(module);
				return module;
			} else if (event.type === "autospy") {
				const module = AutospiedModule.fromJSON(event);
				this.add(module);
				return module;
			} else if (event.type === "redirect") {
				const module = RedirectedModule.fromJSON(event);
				this.add(module);
				return module;
			} else if (event.type === "manual") {
				throw new Error(`Cannot set serialized manual mock. Define a factory function manually with \`ManualMockedModule.fromJSON()\`.`);
			} else {
				throw new Error(`Unknown mock type: ${event.type}`);
			}
		}
		if (typeof raw !== "string") {
			throw new TypeError("[vitest] Mocks require a raw string.");
		}
		if (typeof url !== "string") {
			throw new TypeError("[vitest] Mocks require a url string.");
		}
		if (typeof id !== "string") {
			throw new TypeError("[vitest] Mocks require an id string.");
		}
		if (type === "manual") {
			if (typeof factoryOrRedirect !== "function") {
				throw new TypeError("[vitest] Manual mocks require a factory function.");
			}
			const mock = new ManualMockedModule(raw, id, url, factoryOrRedirect);
			this.add(mock);
			return mock;
		} else if (type === "automock" || type === "autospy") {
			const mock = type === "automock" ? new AutomockedModule(raw, id, url) : new AutospiedModule(raw, id, url);
			this.add(mock);
			return mock;
		} else if (type === "redirect") {
			if (typeof factoryOrRedirect !== "string") {
				throw new TypeError("[vitest] Redirect mocks require a redirect string.");
			}
			const mock = new RedirectedModule(raw, id, url, factoryOrRedirect);
			this.add(mock);
			return mock;
		} else {
			throw new Error(`[vitest] Unknown mock type: ${type}`);
		}
	}
	delete(id) {
		this.registryByUrl.delete(id);
	}
	deleteById(id) {
		this.registryById.delete(id);
	}
	get(id) {
		return this.registryByUrl.get(id);
	}
	getById(id) {
		return this.registryById.get(id);
	}
	has(id) {
		return this.registryByUrl.has(id);
	}
}
class AutomockedModule {
	type = "automock";
	constructor(raw, id, url) {
		this.raw = raw;
		this.id = id;
		this.url = url;
	}
	static fromJSON(data) {
		return new AutospiedModule(data.raw, data.id, data.url);
	}
	toJSON() {
		return {
			type: this.type,
			url: this.url,
			raw: this.raw,
			id: this.id
		};
	}
}
class AutospiedModule {
	type = "autospy";
	constructor(raw, id, url) {
		this.raw = raw;
		this.id = id;
		this.url = url;
	}
	static fromJSON(data) {
		return new AutospiedModule(data.raw, data.id, data.url);
	}
	toJSON() {
		return {
			type: this.type,
			url: this.url,
			id: this.id,
			raw: this.raw
		};
	}
}
class RedirectedModule {
	type = "redirect";
	constructor(raw, id, url, redirect) {
		this.raw = raw;
		this.id = id;
		this.url = url;
		this.redirect = redirect;
	}
	static fromJSON(data) {
		return new RedirectedModule(data.raw, data.id, data.url, data.redirect);
	}
	toJSON() {
		return {
			type: this.type,
			url: this.url,
			raw: this.raw,
			id: this.id,
			redirect: this.redirect
		};
	}
}
class ManualMockedModule {
	cache;
	type = "manual";
	constructor(raw, id, url, factory) {
		this.raw = raw;
		this.id = id;
		this.url = url;
		this.factory = factory;
	}
	async resolve() {
		if (this.cache) {
			return this.cache;
		}
		let exports$1;
		try {
			exports$1 = await this.factory();
		} catch (err) {
			const vitestError = 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");
			vitestError.cause = err;
			throw vitestError;
		}
		if (exports$1 === null || typeof exports$1 !== "object" || Array.isArray(exports$1)) {
			throw new TypeError(`[vitest] vi.mock("${this.raw}", factory?: () => unknown) is not returning an object. Did you mean to return an object with a "default" key?`);
		}
		return this.cache = exports$1;
	}
	static fromJSON(data, factory) {
		return new ManualMockedModule(data.raw, data.id, data.url, factory);
	}
	toJSON() {
		return {
			type: this.type,
			url: this.url,
			id: this.id,
			raw: this.raw
		};
	}
}

export { AutomockedModule as A, MockerRegistry as M, RedirectedModule as R, ManualMockedModule as a, AutospiedModule as b };