Skip to content

Commit 26d93f4

Browse files
committed
update tests to use inline snapshots
1 parent 670e22f commit 26d93f4

File tree

9 files changed

+120
-55
lines changed

9 files changed

+120
-55
lines changed

__tests__/catch-invalid-plist.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import plist from "../src/index";
22

33
it("Throws an error on improperly formatted plist", () => {
4-
function doIt() {
5-
return plist.readFileSync(`${__dirname}/test-xml1-invalid.plist`);
6-
}
4+
const doIt = () => plist.readFileSync(`${__dirname}/test-xml1-invalid.plist`);
75
expect(doIt).toThrow();
86
});
97

108
it("returns an empty object when the file is zero bytes", () => {
119
const obj = plist.readFileSync(`${__dirname}/test-xml1-invalid-2.plist`);
12-
expect(obj).toEqual({});
10+
expect(obj).toMatchInlineSnapshot(`Object {}`);
1311
});

__tests__/parse.test.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
import plist from "../src/index";
2+
import { DemoFile } from "./utils/types";
23

34
describe("String parsing", () => {
45
it("can parse a string", () => {
5-
const doc = plist.readFileSync(`${__dirname}/test-binary1.plist`);
6+
const doc = plist.readFileSync<DemoFile>(`${__dirname}/test-binary1.plist`);
67
const plistString = plist.stringify(doc);
78
const parsedDoc = plist.parse(plistString);
89

9-
return expect(parsedDoc).toEqual(doc);
10+
return expect(parsedDoc).toMatchInlineSnapshot(`
11+
Object {
12+
"Birth Year": 1942,
13+
"Name": "John Doe",
14+
"Travel Log": Array [
15+
"Tokyo, Honshu, Japan",
16+
"Philadelphia, PA",
17+
"Recife, Pernambuco, Brazil",
18+
],
19+
}
20+
`);
1021
});
1122
});

__tests__/readFile-binary1.test.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
11
import plist from "../src/index";
22

33
const filePath = `${__dirname}/test-binary1.plist`;
4-
const reference = {
5-
"Travel Log": [
6-
"Tokyo, Honshu, Japan",
7-
"Philadelphia, PA",
8-
"Recife, Pernambuco, Brazil",
9-
],
10-
"Birth Year": 1942,
11-
Name: "John Doe",
12-
};
134

145
describe("readFileSync can properly load and read a binary file", () => {
156
it("has the proper values", () => {
16-
expect(plist.readFileSync(filePath)).toMatchObject(reference);
7+
expect(plist.readFileSync(filePath)).toMatchInlineSnapshot(`
8+
Object {
9+
"Birth Year": 1942,
10+
"Name": "John Doe",
11+
"Travel Log": Array [
12+
"Tokyo, Honshu, Japan",
13+
"Philadelphia, PA",
14+
"Recife, Pernambuco, Brazil",
15+
],
16+
}
17+
`);
1718
});
1819
});
1920

2021
describe("readFile works asynchronously", () => {
2122
it("has the proper values", (done) => {
2223
plist.readFile(filePath, (error, contents) => {
23-
expect(contents).toMatchObject(reference);
24+
expect(contents).toMatchInlineSnapshot(`
25+
Object {
26+
"Birth Year": 1942,
27+
"Name": "John Doe",
28+
"Travel Log": Array [
29+
"Tokyo, Honshu, Japan",
30+
"Philadelphia, PA",
31+
"Recife, Pernambuco, Brazil",
32+
],
33+
}
34+
`);
2435
done();
2536
});
2637
});

__tests__/readFile-xml1.test.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
import plist from "../src/index";
2+
import { DemoFile } from "./utils/types";
23

34
const filePath = `${__dirname}/test-xml1.plist`;
45

5-
type TestXml1 = {
6-
"Birth Year": number;
7-
Name: string;
8-
"Empty String": string;
9-
"Travel Log": string[];
10-
};
6+
type TestXml1 = DemoFile & { "Empty String": string };
117

128
describe("readFileSync can properly load and read a file", () => {
139
const contents = plist.readFileSync<TestXml1>(filePath);
1410
it("has the proper values", () => {
1511
if (!contents["Name"]) {
1612
fail(`Failed to parse ${filePath}`);
1713
}
18-
expect(contents.Name).toBe("John Doe");
19-
expect(contents["Birth Year"]).toBe(1942);
20-
expect(contents["Empty String"]).toBe("");
21-
expect(contents["Travel Log"]).toEqual([
22-
"Tokyo, Honshu, Japan",
23-
"Philadelphia, PA",
24-
"Recife, Pernambuco, Brazil",
25-
]);
14+
expect(contents).toMatchInlineSnapshot(`
15+
Object {
16+
"Birth Year": 1942,
17+
"Empty String": "",
18+
"Name": "John Doe",
19+
"Travel Log": Array [
20+
"Tokyo, Honshu, Japan",
21+
"Philadelphia, PA",
22+
"Recife, Pernambuco, Brazil",
23+
],
24+
}
25+
`);
2626
});
2727
});
2828

@@ -32,14 +32,18 @@ describe("readFile works asynchronously", () => {
3232
if (!contents) {
3333
fail(`Failed to parse ${filePath}`);
3434
}
35-
expect(contents.Name).toBe("John Doe");
36-
expect(contents["Birth Year"]).toBe(1942);
37-
expect(contents["Empty String"]).toBe("");
38-
expect(contents["Travel Log"]).toEqual([
39-
"Tokyo, Honshu, Japan",
40-
"Philadelphia, PA",
41-
"Recife, Pernambuco, Brazil",
42-
]);
35+
expect(contents).toMatchInlineSnapshot(`
36+
Object {
37+
"Birth Year": 1942,
38+
"Empty String": "",
39+
"Name": "John Doe",
40+
"Travel Log": Array [
41+
"Tokyo, Honshu, Japan",
42+
"Philadelphia, PA",
43+
"Recife, Pernambuco, Brazil",
44+
],
45+
}
46+
`);
4347
done();
4448
});
4549
});

__tests__/utils/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export type DemoFile = {
2+
"Birth Year": number;
3+
Name: string;
4+
"Travel Log": string[];
5+
};

__tests__/writeFile-binary1.test.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,17 @@ describe("writeBinaryFileSync can properly load and read a file", () => {
1515
it("has the proper values", (done) => {
1616
plist.writeBinaryFileSync(filePath, testObj);
1717
plist.readFile(filePath, (error, contents) => {
18-
expect(contents).toMatchObject(testObj);
18+
expect(contents).toMatchInlineSnapshot(`
19+
Object {
20+
"Birth Year": 1942,
21+
"Name": "John Doe",
22+
"Travel Log": Array [
23+
"Tokyo, Honshu, Japan",
24+
"Philadelphia, PA",
25+
"Recife, Pernambuco, Brazil",
26+
],
27+
}
28+
`);
1929
done();
2030
});
2131
});
@@ -25,7 +35,17 @@ describe("writeBinaryFile works asynchronously", () => {
2535
it("has the proper values", (done) => {
2636
plist.writeBinaryFile(filePath, testObj, () => {
2737
plist.readFile(filePath, (error, contents) => {
28-
expect(contents).toMatchObject(testObj);
38+
expect(contents).toMatchInlineSnapshot(`
39+
Object {
40+
"Birth Year": 1942,
41+
"Name": "John Doe",
42+
"Travel Log": Array [
43+
"Tokyo, Honshu, Japan",
44+
"Philadelphia, PA",
45+
"Recife, Pernambuco, Brazil",
46+
],
47+
}
48+
`);
2949
done();
3050
});
3151
});

__tests__/writeFile-xml1.test.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,17 @@ describe("writeFileSync can produce a valid file", () => {
1515
it("has the proper values", (done) => {
1616
plist.writeFileSync(filePath, testObj);
1717
plist.readFile(filePath, (error, contents) => {
18-
expect(contents).toMatchObject(testObj);
18+
expect(contents).toMatchInlineSnapshot(`
19+
Object {
20+
"Birth Year": 1942,
21+
"Name": "John Doe",
22+
"Travel Log": Array [
23+
"Tokyo, Honshu, Japan",
24+
"Philadelphia, PA",
25+
"Recife, Pernambuco, Brazil",
26+
],
27+
}
28+
`);
1929
done();
2030
});
2131
});
@@ -25,7 +35,17 @@ describe("writeFile works asynchronously", () => {
2535
it("has the proper values", (done) => {
2636
plist.writeFile(filePath, testObj, () => {
2737
plist.readFile(filePath, (error, contents) => {
28-
expect(contents).toMatchObject(testObj);
38+
expect(contents).toMatchInlineSnapshot(`
39+
Object {
40+
"Birth Year": 1942,
41+
"Name": "John Doe",
42+
"Travel Log": Array [
43+
"Tokyo, Honshu, Japan",
44+
"Philadelphia, PA",
45+
"Recife, Pernambuco, Brazil",
46+
],
47+
}
48+
`);
2949
done();
3050
});
3151
});

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
module.exports = {
33
preset: "ts-jest",
44
testEnvironment: "node",
5+
modulePathIgnorePatterns: ["src", "__tests__/utils"],
56
};

src/index.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,7 @@ import { writeFile } from "./writeFile";
1010
import { writeFileSync } from "./writeFileSync";
1111

1212
// "modern" named exports
13-
export { parse } from "./parse";
14-
export { readFile } from "./readFile";
15-
export { readFileSync } from "./readFileSync";
16-
export { stringify } from "./stringify";
17-
export type { callbackFn, PlistJsObj, StringOrBuffer } from "./types";
18-
export { writeBinaryFile } from "./writeBinaryFile";
19-
export { writeBinaryFileSync } from "./writeBinaryFileSync";
20-
export { writeFile } from "./writeFile";
21-
export { writeFileSync } from "./writeFileSync";
22-
23-
// preserve backwards compatibility
24-
module.exports = {
13+
const SimplePlist = {
2514
bplistCreator,
2615
bplistParser,
2716
parse,
@@ -33,3 +22,9 @@ module.exports = {
3322
writeFile,
3423
writeFileSync,
3524
};
25+
26+
export default SimplePlist;
27+
export type { callbackFn, PlistJsObj, StringOrBuffer } from "./types";
28+
29+
// preserve backwards compatibility
30+
module.exports = SimplePlist;

0 commit comments

Comments
 (0)