From 26d93f4f6721d404af00e3152d77bf83e3b54043 Mon Sep 17 00:00:00 2001 From: Joe Wollard Date: Thu, 31 Mar 2022 01:17:56 +0000 Subject: [PATCH] update tests to use inline snapshots --- __tests__/catch-invalid-plist.ts | 6 ++-- __tests__/parse.test.ts | 15 +++++++-- __tests__/readFile-binary1.test.ts | 33 +++++++++++++------- __tests__/readFile-xml1.test.ts | 48 ++++++++++++++++------------- __tests__/utils/types.ts | 5 +++ __tests__/writeFile-binary1.test.ts | 24 +++++++++++++-- __tests__/writeFile-xml1.test.ts | 24 +++++++++++++-- jest.config.js | 1 + src/index.ts | 19 +++++------- 9 files changed, 120 insertions(+), 55 deletions(-) create mode 100644 __tests__/utils/types.ts diff --git a/__tests__/catch-invalid-plist.ts b/__tests__/catch-invalid-plist.ts index 49940d1..dee33e8 100644 --- a/__tests__/catch-invalid-plist.ts +++ b/__tests__/catch-invalid-plist.ts @@ -1,13 +1,11 @@ import plist from "../src/index"; it("Throws an error on improperly formatted plist", () => { - function doIt() { - return plist.readFileSync(`${__dirname}/test-xml1-invalid.plist`); - } + const doIt = () => plist.readFileSync(`${__dirname}/test-xml1-invalid.plist`); expect(doIt).toThrow(); }); it("returns an empty object when the file is zero bytes", () => { const obj = plist.readFileSync(`${__dirname}/test-xml1-invalid-2.plist`); - expect(obj).toEqual({}); + expect(obj).toMatchInlineSnapshot(`Object {}`); }); diff --git a/__tests__/parse.test.ts b/__tests__/parse.test.ts index 14e5bd3..175f8be 100644 --- a/__tests__/parse.test.ts +++ b/__tests__/parse.test.ts @@ -1,11 +1,22 @@ import plist from "../src/index"; +import { DemoFile } from "./utils/types"; describe("String parsing", () => { it("can parse a string", () => { - const doc = plist.readFileSync(`${__dirname}/test-binary1.plist`); + const doc = plist.readFileSync(`${__dirname}/test-binary1.plist`); const plistString = plist.stringify(doc); const parsedDoc = plist.parse(plistString); - return expect(parsedDoc).toEqual(doc); + return expect(parsedDoc).toMatchInlineSnapshot(` + Object { + "Birth Year": 1942, + "Name": "John Doe", + "Travel Log": Array [ + "Tokyo, Honshu, Japan", + "Philadelphia, PA", + "Recife, Pernambuco, Brazil", + ], + } + `); }); }); diff --git a/__tests__/readFile-binary1.test.ts b/__tests__/readFile-binary1.test.ts index 6c784f2..5253a65 100644 --- a/__tests__/readFile-binary1.test.ts +++ b/__tests__/readFile-binary1.test.ts @@ -1,26 +1,37 @@ import plist from "../src/index"; const filePath = `${__dirname}/test-binary1.plist`; -const reference = { - "Travel Log": [ - "Tokyo, Honshu, Japan", - "Philadelphia, PA", - "Recife, Pernambuco, Brazil", - ], - "Birth Year": 1942, - Name: "John Doe", -}; describe("readFileSync can properly load and read a binary file", () => { it("has the proper values", () => { - expect(plist.readFileSync(filePath)).toMatchObject(reference); + expect(plist.readFileSync(filePath)).toMatchInlineSnapshot(` + Object { + "Birth Year": 1942, + "Name": "John Doe", + "Travel Log": Array [ + "Tokyo, Honshu, Japan", + "Philadelphia, PA", + "Recife, Pernambuco, Brazil", + ], + } + `); }); }); describe("readFile works asynchronously", () => { it("has the proper values", (done) => { plist.readFile(filePath, (error, contents) => { - expect(contents).toMatchObject(reference); + expect(contents).toMatchInlineSnapshot(` + Object { + "Birth Year": 1942, + "Name": "John Doe", + "Travel Log": Array [ + "Tokyo, Honshu, Japan", + "Philadelphia, PA", + "Recife, Pernambuco, Brazil", + ], + } + `); done(); }); }); diff --git a/__tests__/readFile-xml1.test.ts b/__tests__/readFile-xml1.test.ts index 28f0b76..461b196 100644 --- a/__tests__/readFile-xml1.test.ts +++ b/__tests__/readFile-xml1.test.ts @@ -1,13 +1,9 @@ import plist from "../src/index"; +import { DemoFile } from "./utils/types"; const filePath = `${__dirname}/test-xml1.plist`; -type TestXml1 = { - "Birth Year": number; - Name: string; - "Empty String": string; - "Travel Log": string[]; -}; +type TestXml1 = DemoFile & { "Empty String": string }; describe("readFileSync can properly load and read a file", () => { const contents = plist.readFileSync(filePath); @@ -15,14 +11,18 @@ describe("readFileSync can properly load and read a file", () => { if (!contents["Name"]) { fail(`Failed to parse ${filePath}`); } - expect(contents.Name).toBe("John Doe"); - expect(contents["Birth Year"]).toBe(1942); - expect(contents["Empty String"]).toBe(""); - expect(contents["Travel Log"]).toEqual([ - "Tokyo, Honshu, Japan", - "Philadelphia, PA", - "Recife, Pernambuco, Brazil", - ]); + expect(contents).toMatchInlineSnapshot(` + Object { + "Birth Year": 1942, + "Empty String": "", + "Name": "John Doe", + "Travel Log": Array [ + "Tokyo, Honshu, Japan", + "Philadelphia, PA", + "Recife, Pernambuco, Brazil", + ], + } + `); }); }); @@ -32,14 +32,18 @@ describe("readFile works asynchronously", () => { if (!contents) { fail(`Failed to parse ${filePath}`); } - expect(contents.Name).toBe("John Doe"); - expect(contents["Birth Year"]).toBe(1942); - expect(contents["Empty String"]).toBe(""); - expect(contents["Travel Log"]).toEqual([ - "Tokyo, Honshu, Japan", - "Philadelphia, PA", - "Recife, Pernambuco, Brazil", - ]); + expect(contents).toMatchInlineSnapshot(` + Object { + "Birth Year": 1942, + "Empty String": "", + "Name": "John Doe", + "Travel Log": Array [ + "Tokyo, Honshu, Japan", + "Philadelphia, PA", + "Recife, Pernambuco, Brazil", + ], + } + `); done(); }); }); diff --git a/__tests__/utils/types.ts b/__tests__/utils/types.ts new file mode 100644 index 0000000..fc5a134 --- /dev/null +++ b/__tests__/utils/types.ts @@ -0,0 +1,5 @@ +export type DemoFile = { + "Birth Year": number; + Name: string; + "Travel Log": string[]; +}; diff --git a/__tests__/writeFile-binary1.test.ts b/__tests__/writeFile-binary1.test.ts index 60de43c..01dcb09 100644 --- a/__tests__/writeFile-binary1.test.ts +++ b/__tests__/writeFile-binary1.test.ts @@ -15,7 +15,17 @@ describe("writeBinaryFileSync can properly load and read a file", () => { it("has the proper values", (done) => { plist.writeBinaryFileSync(filePath, testObj); plist.readFile(filePath, (error, contents) => { - expect(contents).toMatchObject(testObj); + expect(contents).toMatchInlineSnapshot(` + Object { + "Birth Year": 1942, + "Name": "John Doe", + "Travel Log": Array [ + "Tokyo, Honshu, Japan", + "Philadelphia, PA", + "Recife, Pernambuco, Brazil", + ], + } + `); done(); }); }); @@ -25,7 +35,17 @@ describe("writeBinaryFile works asynchronously", () => { it("has the proper values", (done) => { plist.writeBinaryFile(filePath, testObj, () => { plist.readFile(filePath, (error, contents) => { - expect(contents).toMatchObject(testObj); + expect(contents).toMatchInlineSnapshot(` + Object { + "Birth Year": 1942, + "Name": "John Doe", + "Travel Log": Array [ + "Tokyo, Honshu, Japan", + "Philadelphia, PA", + "Recife, Pernambuco, Brazil", + ], + } + `); done(); }); }); diff --git a/__tests__/writeFile-xml1.test.ts b/__tests__/writeFile-xml1.test.ts index d4b668f..f99cbf6 100644 --- a/__tests__/writeFile-xml1.test.ts +++ b/__tests__/writeFile-xml1.test.ts @@ -15,7 +15,17 @@ describe("writeFileSync can produce a valid file", () => { it("has the proper values", (done) => { plist.writeFileSync(filePath, testObj); plist.readFile(filePath, (error, contents) => { - expect(contents).toMatchObject(testObj); + expect(contents).toMatchInlineSnapshot(` + Object { + "Birth Year": 1942, + "Name": "John Doe", + "Travel Log": Array [ + "Tokyo, Honshu, Japan", + "Philadelphia, PA", + "Recife, Pernambuco, Brazil", + ], + } + `); done(); }); }); @@ -25,7 +35,17 @@ describe("writeFile works asynchronously", () => { it("has the proper values", (done) => { plist.writeFile(filePath, testObj, () => { plist.readFile(filePath, (error, contents) => { - expect(contents).toMatchObject(testObj); + expect(contents).toMatchInlineSnapshot(` + Object { + "Birth Year": 1942, + "Name": "John Doe", + "Travel Log": Array [ + "Tokyo, Honshu, Japan", + "Philadelphia, PA", + "Recife, Pernambuco, Brazil", + ], + } + `); done(); }); }); diff --git a/jest.config.js b/jest.config.js index 21a1e97..ea0c505 100644 --- a/jest.config.js +++ b/jest.config.js @@ -2,4 +2,5 @@ module.exports = { preset: "ts-jest", testEnvironment: "node", + modulePathIgnorePatterns: ["src", "__tests__/utils"], }; diff --git a/src/index.ts b/src/index.ts index 28cab48..74bc4e7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,18 +10,7 @@ import { writeFile } from "./writeFile"; import { writeFileSync } from "./writeFileSync"; // "modern" named exports -export { parse } from "./parse"; -export { readFile } from "./readFile"; -export { readFileSync } from "./readFileSync"; -export { stringify } from "./stringify"; -export type { callbackFn, PlistJsObj, StringOrBuffer } from "./types"; -export { writeBinaryFile } from "./writeBinaryFile"; -export { writeBinaryFileSync } from "./writeBinaryFileSync"; -export { writeFile } from "./writeFile"; -export { writeFileSync } from "./writeFileSync"; - -// preserve backwards compatibility -module.exports = { +const SimplePlist = { bplistCreator, bplistParser, parse, @@ -33,3 +22,9 @@ module.exports = { writeFile, writeFileSync, }; + +export default SimplePlist; +export type { callbackFn, PlistJsObj, StringOrBuffer } from "./types"; + +// preserve backwards compatibility +module.exports = SimplePlist;