版博士V2.0程序
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

387 行
14 KiB

  1. import { pathToFileURL } from 'node:url';
  2. import { ModuleCacheMap, ViteNodeRunner } from 'vite-node/client';
  3. import { isInternalRequest, isPrimitive } from 'vite-node/utils';
  4. import { isAbsolute, dirname, join, basename, extname, resolve, normalize, relative } from 'pathe';
  5. import { i as isNodeBuiltin } from './vendor-index.bdee400f.js';
  6. import { processError } from '@vitest/runner/utils';
  7. import { g as getWorkerState, a as getCurrentEnvironment } from './chunk-utils-global.fd174983.js';
  8. import { d as distDir } from './chunk-paths.e36446b4.js';
  9. import { existsSync, readdirSync } from 'node:fs';
  10. import { getColors, getType } from '@vitest/utils';
  11. import { f as getAllMockableProperties } from './chunk-utils-base.b5ddfcc9.js';
  12. import { spyOn } from '@vitest/spy';
  13. import { r as rpc } from './chunk-runtime-rpc.d6aa57f8.js';
  14. const filterPublicKeys = ["__esModule", Symbol.asyncIterator, Symbol.hasInstance, Symbol.isConcatSpreadable, Symbol.iterator, Symbol.match, Symbol.matchAll, Symbol.replace, Symbol.search, Symbol.split, Symbol.species, Symbol.toPrimitive, Symbol.toStringTag, Symbol.unscopables];
  15. class RefTracker {
  16. constructor() {
  17. this.idMap = /* @__PURE__ */ new Map();
  18. this.mockedValueMap = /* @__PURE__ */ new Map();
  19. }
  20. getId(value) {
  21. return this.idMap.get(value);
  22. }
  23. getMockedValue(id) {
  24. return this.mockedValueMap.get(id);
  25. }
  26. track(originalValue, mockedValue) {
  27. const newId = this.idMap.size;
  28. this.idMap.set(originalValue, newId);
  29. this.mockedValueMap.set(newId, mockedValue);
  30. return newId;
  31. }
  32. }
  33. function isSpecialProp(prop, parentType) {
  34. return parentType.includes("Function") && typeof prop === "string" && ["arguments", "callee", "caller", "length", "name"].includes(prop);
  35. }
  36. const _VitestMocker = class {
  37. constructor(executor) {
  38. this.executor = executor;
  39. this.resolveCache = /* @__PURE__ */ new Map();
  40. }
  41. get root() {
  42. return this.executor.options.root;
  43. }
  44. get base() {
  45. return this.executor.options.base;
  46. }
  47. get mockMap() {
  48. return this.executor.options.mockMap;
  49. }
  50. get moduleCache() {
  51. return this.executor.moduleCache;
  52. }
  53. deleteCachedItem(id) {
  54. const mockId = this.getMockPath(id);
  55. if (this.moduleCache.has(mockId))
  56. this.moduleCache.delete(mockId);
  57. }
  58. getSuiteFilepath() {
  59. return getWorkerState().filepath || "global";
  60. }
  61. getMocks() {
  62. const suite = this.getSuiteFilepath();
  63. const suiteMocks = this.mockMap.get(suite);
  64. const globalMocks = this.mockMap.get("global");
  65. return {
  66. ...globalMocks,
  67. ...suiteMocks
  68. };
  69. }
  70. async resolvePath(rawId, importer) {
  71. const [id, fsPath] = await this.executor.resolveUrl(rawId, importer);
  72. const external = !isAbsolute(fsPath) || fsPath.includes("/node_modules/") ? rawId : null;
  73. return {
  74. id,
  75. fsPath,
  76. external
  77. };
  78. }
  79. async resolveMocks() {
  80. await Promise.all(_VitestMocker.pendingIds.map(async (mock) => {
  81. const { fsPath, external } = await this.resolvePath(mock.id, mock.importer);
  82. if (mock.type === "unmock")
  83. this.unmockPath(fsPath);
  84. if (mock.type === "mock")
  85. this.mockPath(mock.id, fsPath, external, mock.factory);
  86. }));
  87. _VitestMocker.pendingIds = [];
  88. }
  89. async callFunctionMock(dep, mock) {
  90. var _a, _b;
  91. const cached = (_a = this.moduleCache.get(dep)) == null ? void 0 : _a.exports;
  92. if (cached)
  93. return cached;
  94. let exports;
  95. try {
  96. exports = await mock();
  97. } catch (err) {
  98. const vitestError = new Error(
  99. '[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-mock'
  100. );
  101. vitestError.cause = err;
  102. throw vitestError;
  103. }
  104. const filepath = dep.slice(5);
  105. const mockpath = ((_b = this.resolveCache.get(this.getSuiteFilepath())) == null ? void 0 : _b[filepath]) || filepath;
  106. if (exports === null || typeof exports !== "object")
  107. throw new Error(`[vitest] vi.mock("${mockpath}", factory?: () => unknown) is not returning an object. Did you mean to return an object with a "default" key?`);
  108. const moduleExports = new Proxy(exports, {
  109. get(target, prop) {
  110. const val = target[prop];
  111. if (prop === "then") {
  112. if (target instanceof Promise)
  113. return target.then.bind(target);
  114. } else if (!(prop in target)) {
  115. if (filterPublicKeys.includes(prop))
  116. return void 0;
  117. const c = getColors();
  118. throw new Error(
  119. `[vitest] No "${String(prop)}" export is defined on the "${mockpath}" mock. Did you forget to return it from "vi.mock"?
  120. If you need to partially mock a module, you can use "vi.importActual" inside:
  121. ${c.green(`vi.mock("${mockpath}", async () => {
  122. const actual = await vi.importActual("${mockpath}")
  123. return {
  124. ...actual,
  125. // your mocked methods
  126. },
  127. })`)}
  128. `
  129. );
  130. }
  131. return val;
  132. }
  133. });
  134. this.moduleCache.set(dep, { exports: moduleExports });
  135. return moduleExports;
  136. }
  137. getMockPath(dep) {
  138. return `mock:${dep}`;
  139. }
  140. getDependencyMock(id) {
  141. return this.getMocks()[id];
  142. }
  143. normalizePath(path) {
  144. return this.moduleCache.normalizePath(path);
  145. }
  146. resolveMockPath(mockPath, external) {
  147. const path = external || mockPath;
  148. if (external || isNodeBuiltin(mockPath) || !existsSync(mockPath)) {
  149. const mockDirname = dirname(path);
  150. const mockFolder = join(this.root, "__mocks__", mockDirname);
  151. if (!existsSync(mockFolder))
  152. return null;
  153. const files = readdirSync(mockFolder);
  154. const baseOriginal = basename(path);
  155. for (const file of files) {
  156. const baseFile = basename(file, extname(file));
  157. if (baseFile === baseOriginal)
  158. return resolve(mockFolder, file);
  159. }
  160. return null;
  161. }
  162. const dir = dirname(path);
  163. const baseId = basename(path);
  164. const fullPath = resolve(dir, "__mocks__", baseId);
  165. return existsSync(fullPath) ? fullPath : null;
  166. }
  167. mockObject(object, mockExports = {}) {
  168. const finalizers = new Array();
  169. const refs = new RefTracker();
  170. const define = (container, key, value) => {
  171. try {
  172. container[key] = value;
  173. return true;
  174. } catch {
  175. return false;
  176. }
  177. };
  178. const mockPropertiesOf = (container, newContainer) => {
  179. const containerType = getType(container);
  180. const isModule = containerType === "Module" || !!container.__esModule;
  181. for (const { key: property, descriptor } of getAllMockableProperties(container, isModule)) {
  182. if (!isModule && descriptor.get) {
  183. try {
  184. Object.defineProperty(newContainer, property, descriptor);
  185. } catch (error) {
  186. }
  187. continue;
  188. }
  189. if (isSpecialProp(property, containerType))
  190. continue;
  191. const value = container[property];
  192. const refId = refs.getId(value);
  193. if (refId !== void 0) {
  194. finalizers.push(() => define(newContainer, property, refs.getMockedValue(refId)));
  195. continue;
  196. }
  197. const type = getType(value);
  198. if (Array.isArray(value)) {
  199. define(newContainer, property, []);
  200. continue;
  201. }
  202. const isFunction = type.includes("Function") && typeof value === "function";
  203. if ((!isFunction || value.__isMockFunction) && type !== "Object" && type !== "Module") {
  204. define(newContainer, property, value);
  205. continue;
  206. }
  207. if (!define(newContainer, property, isFunction ? value : {}))
  208. continue;
  209. if (isFunction) {
  210. spyOn(newContainer, property).mockImplementation(() => void 0);
  211. Object.defineProperty(newContainer[property], "length", { value: 0 });
  212. }
  213. refs.track(value, newContainer[property]);
  214. mockPropertiesOf(value, newContainer[property]);
  215. }
  216. };
  217. const mockedObject = mockExports;
  218. mockPropertiesOf(object, mockedObject);
  219. for (const finalizer of finalizers)
  220. finalizer();
  221. return mockedObject;
  222. }
  223. unmockPath(path) {
  224. const suitefile = this.getSuiteFilepath();
  225. const id = this.normalizePath(path);
  226. const mock = this.mockMap.get(suitefile);
  227. if (mock && id in mock)
  228. delete mock[id];
  229. this.deleteCachedItem(id);
  230. }
  231. mockPath(originalId, path, external, factory) {
  232. const suitefile = this.getSuiteFilepath();
  233. const id = this.normalizePath(path);
  234. const mocks = this.mockMap.get(suitefile) || {};
  235. const resolves = this.resolveCache.get(suitefile) || {};
  236. mocks[id] = factory || this.resolveMockPath(path, external);
  237. resolves[id] = originalId;
  238. this.mockMap.set(suitefile, mocks);
  239. this.resolveCache.set(suitefile, resolves);
  240. this.deleteCachedItem(id);
  241. }
  242. async importActual(rawId, importee) {
  243. const { id, fsPath } = await this.resolvePath(rawId, importee);
  244. const result = await this.executor.cachedRequest(id, fsPath, [importee]);
  245. return result;
  246. }
  247. async importMock(rawId, importee) {
  248. const { id, fsPath, external } = await this.resolvePath(rawId, importee);
  249. const normalizedId = this.normalizePath(fsPath);
  250. let mock = this.getDependencyMock(normalizedId);
  251. if (mock === void 0)
  252. mock = this.resolveMockPath(fsPath, external);
  253. if (mock === null) {
  254. const mod = await this.executor.cachedRequest(id, fsPath, [importee]);
  255. return this.mockObject(mod);
  256. }
  257. if (typeof mock === "function")
  258. return this.callFunctionMock(fsPath, mock);
  259. return this.executor.dependencyRequest(mock, mock, [importee]);
  260. }
  261. async requestWithMock(url, callstack) {
  262. if (_VitestMocker.pendingIds.length)
  263. await this.resolveMocks();
  264. const id = this.normalizePath(url);
  265. const mock = this.getDependencyMock(id);
  266. const mockPath = this.getMockPath(id);
  267. if (mock === null) {
  268. const cache = this.moduleCache.get(mockPath);
  269. if (cache.exports)
  270. return cache.exports;
  271. const exports = {};
  272. this.moduleCache.set(mockPath, { exports });
  273. const mod = await this.executor.directRequest(url, url, callstack);
  274. this.mockObject(mod, exports);
  275. return exports;
  276. }
  277. if (typeof mock === "function" && !callstack.includes(mockPath) && !callstack.includes(url)) {
  278. callstack.push(mockPath);
  279. const result = await this.callFunctionMock(mockPath, mock);
  280. const indexMock = callstack.indexOf(mockPath);
  281. callstack.splice(indexMock, 1);
  282. return result;
  283. }
  284. if (typeof mock === "string" && !callstack.includes(mock))
  285. return mock;
  286. }
  287. queueMock(id, importer, factory) {
  288. _VitestMocker.pendingIds.push({ type: "mock", id, importer, factory });
  289. }
  290. queueUnmock(id, importer) {
  291. _VitestMocker.pendingIds.push({ type: "unmock", id, importer });
  292. }
  293. };
  294. let VitestMocker = _VitestMocker;
  295. VitestMocker.pendingIds = [];
  296. async function createVitestExecutor(options) {
  297. const runner = new VitestExecutor(options);
  298. await runner.executeId("/@vite/env");
  299. return runner;
  300. }
  301. let _viteNode;
  302. const moduleCache = new ModuleCacheMap();
  303. const mockMap = /* @__PURE__ */ new Map();
  304. async function startViteNode(ctx) {
  305. if (_viteNode)
  306. return _viteNode;
  307. const { config } = ctx;
  308. const processExit = process.exit;
  309. process.exit = (code = process.exitCode || 0) => {
  310. const error = new Error(`process.exit called with "${code}"`);
  311. rpc().onWorkerExit(error, code);
  312. return processExit(code);
  313. };
  314. function catchError(err, type) {
  315. var _a;
  316. const worker = getWorkerState();
  317. const error = processError(err);
  318. if (worker.filepath && !isPrimitive(error)) {
  319. error.VITEST_TEST_NAME = (_a = worker.current) == null ? void 0 : _a.name;
  320. error.VITEST_TEST_PATH = relative(config.root, worker.filepath);
  321. }
  322. error.VITEST_AFTER_ENV_TEARDOWN = worker.environmentTeardownRun;
  323. rpc().onUnhandledError(error, type);
  324. }
  325. process.on("uncaughtException", (e) => catchError(e, "Uncaught Exception"));
  326. process.on("unhandledRejection", (e) => catchError(e, "Unhandled Rejection"));
  327. const executor = await createVitestExecutor({
  328. fetchModule(id) {
  329. return rpc().fetch(id, ctx.environment.name);
  330. },
  331. resolveId(id, importer) {
  332. return rpc().resolveId(id, importer, ctx.environment.name);
  333. },
  334. moduleCache,
  335. mockMap,
  336. interopDefault: config.deps.interopDefault,
  337. root: config.root,
  338. base: config.base
  339. });
  340. const { run } = await import(pathToFileURL(resolve(distDir, "entry.js")).href);
  341. _viteNode = { run, executor };
  342. return _viteNode;
  343. }
  344. class VitestExecutor extends ViteNodeRunner {
  345. constructor(options) {
  346. super(options);
  347. this.options = options;
  348. this.mocker = new VitestMocker(this);
  349. Object.defineProperty(globalThis, "__vitest_mocker__", {
  350. value: this.mocker,
  351. writable: true,
  352. configurable: true
  353. });
  354. }
  355. shouldResolveId(id, _importee) {
  356. if (isInternalRequest(id))
  357. return false;
  358. const environment = getCurrentEnvironment();
  359. return environment === "node" ? !isNodeBuiltin(id) : !id.startsWith("node:");
  360. }
  361. async resolveUrl(id, importer) {
  362. if (importer && importer.startsWith("mock:"))
  363. importer = importer.slice(5);
  364. return super.resolveUrl(id, importer);
  365. }
  366. async dependencyRequest(id, fsPath, callstack) {
  367. const mocked = await this.mocker.requestWithMock(fsPath, callstack);
  368. if (typeof mocked === "string")
  369. return super.dependencyRequest(mocked, mocked, callstack);
  370. if (mocked && typeof mocked === "object")
  371. return mocked;
  372. return super.dependencyRequest(id, fsPath, callstack);
  373. }
  374. prepareContext(context) {
  375. const workerState = getWorkerState();
  376. if (workerState.filepath && normalize(workerState.filepath) === normalize(context.__filename)) {
  377. Object.defineProperty(context.__vite_ssr_import_meta__, "vitest", { get: () => globalThis.__vitest_index__ });
  378. }
  379. return context;
  380. }
  381. }
  382. export { VitestExecutor as V, mockMap as a, moduleCache as m, startViteNode as s };