From a669c0210ff26755836e3b2b53d32e5122ffd1a3 Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Tue, 29 Aug 2023 13:32:03 +0000 Subject: [PATCH] fix(lazy-index): update error messages (#2638) --- .changeset/funny-snakes-visit.md | 5 +++++ .../src/experiences.js | 8 ++------ packages/react-native-lazy-index/src/index.js | 8 ++++++++ .../test/experiences.test.ts | 19 ++++--------------- 4 files changed, 19 insertions(+), 21 deletions(-) create mode 100644 .changeset/funny-snakes-visit.md diff --git a/.changeset/funny-snakes-visit.md b/.changeset/funny-snakes-visit.md new file mode 100644 index 000000000..5c4d8eb65 --- /dev/null +++ b/.changeset/funny-snakes-visit.md @@ -0,0 +1,5 @@ +--- +"@rnx-kit/react-native-lazy-index": patch +--- + +Update error messages to reflect the latest changes diff --git a/packages/react-native-lazy-index/src/experiences.js b/packages/react-native-lazy-index/src/experiences.js index c9ba20a85..35220a9bb 100644 --- a/packages/react-native-lazy-index/src/experiences.js +++ b/packages/react-native-lazy-index/src/experiences.js @@ -34,12 +34,8 @@ function getFlightedModule(moduleId) { * @returns {Record} */ function parseExperiences(experiences) { - if (!experiences) { - throw new Error("Missing `experiences` section in `package.json`"); - } - - if (typeof experiences !== "object") { - throw new Error("Invalid `experiences` section in `package.json`"); + if (!experiences || typeof experiences !== "object") { + throw new Error(`Invalid experiences map; got '${typeof experiences}'`); } const flights = process.env["RN_LAZY_INDEX_FLIGHTS"]?.split(","); diff --git a/packages/react-native-lazy-index/src/index.js b/packages/react-native-lazy-index/src/index.js index 0fdbfe128..86d7f897e 100644 --- a/packages/react-native-lazy-index/src/index.js +++ b/packages/react-native-lazy-index/src/index.js @@ -64,6 +64,14 @@ function readExperiencesFromManifest() { const manifest = fs.readFileSync(manifestPath, { encoding: "utf-8" }); const { experiences } = JSON.parse(manifest); + if (!experiences) { + throw new Error("Missing `experiences` section in `package.json`"); + } + + if (typeof experiences !== "object") { + throw new Error("Invalid `experiences` section in `package.json`"); + } + return experiences; } diff --git a/packages/react-native-lazy-index/test/experiences.test.ts b/packages/react-native-lazy-index/test/experiences.test.ts index 22647da70..effb7e1a4 100644 --- a/packages/react-native-lazy-index/test/experiences.test.ts +++ b/packages/react-native-lazy-index/test/experiences.test.ts @@ -1,6 +1,4 @@ -import fs from "fs"; import { getFlightedModule, parseExperiences } from "../src/experiences"; -import { resolveModule } from "../src/module"; describe("parseExperiences()", () => { afterEach(() => { @@ -8,24 +6,15 @@ describe("parseExperiences()", () => { }); test("missing experiences section", () => { - const packageManifest = resolveModule("./package.json"); - const { experiences } = JSON.parse( - fs.readFileSync(packageManifest, "utf-8") - ); - const result = () => { - parseExperiences(experiences); - }; + const result = () => parseExperiences(undefined); expect(result).toThrow(Error); - expect(result).toThrow("Missing `experiences` section in `package.json`"); + expect(result).toThrow("Invalid experiences map; got 'undefined'"); }); test("invalid experiences section", () => { - const experiences = "MyAwesomeApp"; - const result = () => { - parseExperiences(experiences); - }; + const result = () => parseExperiences("MyAwesomeApp"); expect(result).toThrow(Error); - expect(result).toThrow("Invalid `experiences` section in `package.json`"); + expect(result).toThrow("Invalid experiences map; got 'string'"); }); test("object experiences section", () => {