UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

139 lines (135 loc) 4.57 kB
import path from "path"; import fs from "fs"; import { afterAll, describe, expect, it } from "vitest"; import { withOne } from "./withOne.native.js"; function _type_of(obj) { "@swc/helpers - typeof"; return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; } var projectRoot = path.resolve(__dirname, "../../"); var tmpDirs = []; function createOneFixtureProject() { var workspaceRoot = path.resolve(__dirname, "../../../../"); var tmpDir = fs.mkdtempSync(path.join(workspaceRoot, ".tmp-with-one-")); tmpDirs.push(tmpDir); fs.writeFileSync(path.join(tmpDir, "package.json"), JSON.stringify({ name: "tmp-with-one", private: true })); fs.writeFileSync(path.join(tmpDir, "tsconfig.json"), JSON.stringify({ compilerOptions: { paths: {} } })); fs.mkdirSync(path.join(tmpDir, "app")); fs.writeFileSync(path.join(tmpDir, "app", "index.tsx"), "export default null\n"); fs.writeFileSync(path.join(tmpDir, "vite.config.ts"), ` const defaultConfigOverrides = (config) => ({ ...config, watchFolders: [ ...(config.watchFolders || []), ${JSON.stringify(path.join(tmpDir, "shared"))}, ], resolver: { ...config.resolver, extraNodeModules: { ...config.resolver?.extraNodeModules, 'fixture-singleton': ${JSON.stringify(tmpDir)}, }, }, }) globalThis.__oneOptions = { setupFile: { native: './src/setup-native.ts', }, native: { bundler: 'metro', bundlerOptions: { argv: { projectRoot: ${JSON.stringify(tmpDir)}, }, defaultConfigOverrides, }, }, } globalThis.__vxrnMetroOptions__ = { argv: { projectRoot: ${JSON.stringify(tmpDir)}, }, defaultConfigOverrides, } export default { root: ${JSON.stringify(tmpDir)}, } `); return tmpDir; } afterAll(function () { var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0; try { for (var _iterator = tmpDirs[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var tmpDir = _step.value; fs.rmSync(tmpDir, { recursive: true, force: true }); } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return != null) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } }); describe("withOne", function () { it("returns a config produced by the production native bundle pipeline", async function () { var config = await withOne(projectRoot, { loadViteConfig: false }); expect(config).toBeTruthy(); expect(config.resolver).toBeTruthy(); expect(_type_of(config.resolver.resolveRequest)).toBe("function"); expect(config.transformer).toBeTruthy(); expect(config.transformer.babelTransformerPath).toMatch(/vite-plugin-metro.*babel-transformer/); }); it("orders sourceExts so .js wins over .mjs (the proven One fix)", async function () { var config = await withOne(projectRoot, { loadViteConfig: false }); var exts = config.resolver.sourceExts; expect(exts).toContain("mjs"); expect(exts).toContain("js"); expect(exts.indexOf("js")).toBeLessThan(exts.indexOf("mjs")); }); it("accepts a project root as the first arg", async function () { var config = await withOne(projectRoot, { loadViteConfig: false }); expect(config).toBeTruthy(); }); it("loads vite.config by default and applies the real native Metro options", async function () { var fixtureRoot = createOneFixtureProject(); var config = await withOne(fixtureRoot); expect(config.resolver.extraNodeModules["fixture-singleton"]).toBe(fixtureRoot); expect(config.watchFolders).toContain(path.join(fixtureRoot, "shared")); }); it("rewrites native default index bundle requests to the One entry", async function () { var config = await withOne(projectRoot, { loadViteConfig: false }); var rewriteRequestUrl = config.server.rewriteRequestUrl; expect(rewriteRequestUrl("/.expo/.virtual-metro-entry.bundle?platform=ios&dev=true")).toContain("/packages/one/metro-entry.bundle?platform=ios&dev=true"); expect(rewriteRequestUrl("/index.bundle?platform=ios&dev=true&hot=true&minify=false")).toContain("/packages/one/metro-entry.bundle?platform=ios&dev=true&hot=true&minify=false"); }); }); //# sourceMappingURL=withOne.test.native.js.map