Skip to content
This repository has been archived by the owner on Feb 10, 2024. It is now read-only.

Commit

Permalink
style: apply prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Feb 8, 2020
1 parent 37a7846 commit 4bef026
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 80 deletions.
29 changes: 14 additions & 15 deletions src/fs.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import fs from 'fs';
import { dirname } from 'path';
import fs from "fs";
import { dirname } from "path";

// TODO: fs.mkdirSync
const mkdirpSync =
(path: string, _options: { recursive: true; }): void => {
try {
fs.mkdirSync(path);
} catch (_) {
const stat = fs.statSync(path);
if (stat.isDirectory()) return;
const dir = dirname(path);
if (dir === path) throw new Error('/');
mkdirpSync(dir, { recursive: true });
fs.mkdirSync(path);
}
};
const mkdirpSync = (path: string, _options: { recursive: true }): void => {
try {
fs.mkdirSync(path);
} catch (_) {
const stat = fs.statSync(path);
if (stat.isDirectory()) return;
const dir = dirname(path);
if (dir === path) throw new Error("/");
mkdirpSync(dir, { recursive: true });
fs.mkdirSync(path);
}
};

export { mkdirpSync };
61 changes: 25 additions & 36 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { deepStrictEqual } from 'assert';
import fs from 'fs';
import path from 'path';
import { mkdirpSync } from './fs';
import { deepStrictEqual } from "assert";
import fs from "fs";
import path from "path";
import { mkdirpSync } from "./fs";

type Snapshot = (name: string, o: any) => void;

Expand All @@ -18,40 +18,34 @@ type SnapshotKey = string;

type SnapshotValue = string;

const formatKey =
(name: string): SnapshotKey =>
name.replace(/[^-_a-zA-Z0-9\/]/g, '_') + '.json';
const formatKey = (name: string): SnapshotKey =>
name.replace(/[^-_a-zA-Z0-9\/]/g, "_") + ".json";

const defaultAssert =
(expected: string, actual: string) =>
deepStrictEqual(JSON.parse(expected), JSON.parse(actual));
const defaultAssert = (expected: string, actual: string) =>
deepStrictEqual(JSON.parse(expected), JSON.parse(actual));

const defaultLoad =
(p: string): string =>
fs.readFileSync(p, { encoding: 'utf8' });
const defaultLoad = (p: string): string =>
fs.readFileSync(p, { encoding: "utf8" });

const defaultSave =
(p: string, value: string): void => {
const dir = path.dirname(p);
const stat = fs.statSync(dir);
if (!stat.isDirectory())
mkdirpSync(dir, { recursive: true });
fs.writeFileSync(p, value, { encoding: 'utf8' });
};
const defaultSave = (p: string, value: string): void => {
const dir = path.dirname(p);
const stat = fs.statSync(dir);
if (!stat.isDirectory()) mkdirpSync(dir, { recursive: true });
fs.writeFileSync(p, value, { encoding: "utf8" });
};

const defaultStringify =
(o: any): SnapshotValue => {
if (o === void 0) throw new Error('actual is not supported value');
return JSON.stringify(o, null, 2);
};
const defaultStringify = (o: any): SnapshotValue => {
if (o === void 0) throw new Error("actual is not supported value");
return JSON.stringify(o, null, 2);
};

const ensureOptions = (options?: Partial<SnapshotOptions>): SnapshotOptions => {
const assert = options?.assert ?? defaultAssert;
const directory = options?.directory ?? '__snapshots__';
const directory = options?.directory ?? "__snapshots__";
const load = options?.load ?? defaultLoad;
const save = options?.save ?? defaultSave;
const stringify = options?.stringify ?? defaultStringify;
const update = options?.update ?? process.env.UPDATE_SNAPSHOT === 'true';
const update = options?.update ?? process.env.UPDATE_SNAPSHOT === "true";
return {
assert,
directory,
Expand All @@ -63,14 +57,9 @@ const ensureOptions = (options?: Partial<SnapshotOptions>): SnapshotOptions => {
};

const init = (options?: Partial<SnapshotOptions>): Snapshot => {
const {
assert,
directory,
load,
save,
stringify,
update
} = ensureOptions(options);
const { assert, directory, load, save, stringify, update } = ensureOptions(
options
);
return (name: string, o: any): void => {
const p = path.join(directory, formatKey(name));
const actual = stringify(o);
Expand Down
19 changes: 9 additions & 10 deletions test/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
import assert from 'assert';
import { Test, run } from 'beater';
import { name, named as testOriginal } from 'beater-helpers';
import path from 'path';
import { Snapshot, init } from '../src'; // import ... from 'beater-snapshot';
import assert from "assert";
import { Test, run } from "beater";
import { name, named as testOriginal } from "beater-helpers";
import path from "path";
import { Snapshot, init } from "../src"; // import ... from 'beater-snapshot';

const matchSnapshot: Snapshot = init({
// you can use any assert function
// parse -> compare -> assert
assert: (expected: string, actual: string): void =>
assert.deepStrictEqual(JSON.parse(expected), JSON.parse(actual)),

directory: path.resolve('__snapshots__'),
directory: path.resolve("__snapshots__"),

// test data to snapshot data converter
stringify: (o: any): string =>
JSON.stringify(o, null, 2),
stringify: (o: any): string => JSON.stringify(o, null, 2),

// update snapshot if update option is true
update: process.env.UPDATE_SNAPSHOT === 'true'
update: process.env.UPDATE_SNAPSHOT === "true"
});

// (option) custom beater test function
const test = (testName: string, fn: (name: string) => Promise<void>): Test => {
const test1 = testOriginal(testName, () => fn(name(test1) ?? ''));
const test1 = testOriginal(testName, () => fn(name(test1) ?? ""));
return test1;
};

Expand Down
10 changes: 4 additions & 6 deletions test/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { Test, run } from './helpers';
import { tests as jsonTests } from './json';
import { Test, run } from "./helpers";
import { tests as jsonTests } from "./json";

const tests: Test[] = [
...jsonTests
];
const tests: Test[] = [...jsonTests];

run(tests).catch((e) => {
run(tests).catch(e => {
console.error(e);
process.exit(1);
});
26 changes: 13 additions & 13 deletions test/json.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
import { matchSnapshot, test } from './helpers';
import { matchSnapshot, test } from "./helpers";

const tests = [
test('null', async (name) => {
test("null", async name => {
const x = null;
matchSnapshot(name, x);
}),
test('boolean (true)', async (name) => {
test("boolean (true)", async name => {
const x = true;
matchSnapshot(name, x);
}),
test('boolean (false)', async (name) => {
test("boolean (false)", async name => {
const x = false;
matchSnapshot(name, x);
}),
test('number', async (name) => {
test("number", async name => {
const x = 1;
matchSnapshot(name, x);
}),
test('string', async (name) => {
const x = 'foo';
test("string", async name => {
const x = "foo";
matchSnapshot(name, x);
}),
test('array of null', async (name) => {
test("array of null", async name => {
const x = [null, null];
matchSnapshot(name, x);
}),
test('array of number', async (name) => {
test("array of number", async name => {
const x = [1, 2];
matchSnapshot(name, x);
}),
test('array of string', async (name) => {
const x = ['foo', 'bar'];
test("array of string", async name => {
const x = ["foo", "bar"];
matchSnapshot(name, x);
}),
test('object', async (name) => {
const x = { a: 'apple', b: 'banana' };
test("object", async name => {
const x = { a: "apple", b: "banana" };
matchSnapshot(name, x);
})
];
Expand Down

0 comments on commit 4bef026

Please sign in to comment.