From 4bef026cfddab7a5d84142a5b31186db83679dd6 Mon Sep 17 00:00:00 2001 From: bouzuya Date: Sat, 8 Feb 2020 09:23:46 +0900 Subject: [PATCH] style: apply prettier --- src/fs.ts | 29 ++++++++++++----------- src/index.ts | 61 ++++++++++++++++++++----------------------------- test/helpers.ts | 19 ++++++++------- test/index.ts | 10 ++++---- test/json.ts | 26 ++++++++++----------- 5 files changed, 65 insertions(+), 80 deletions(-) diff --git a/src/fs.ts b/src/fs.ts index 39f4671..a85ffac 100644 --- a/src/fs.ts +++ b/src/fs.ts @@ -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 }; diff --git a/src/index.ts b/src/index.ts index 0761f75..1d60c6b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; @@ -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 => { 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, @@ -63,14 +57,9 @@ const ensureOptions = (options?: Partial): SnapshotOptions => { }; const init = (options?: Partial): 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); diff --git a/test/helpers.ts b/test/helpers.ts index 2c0aca5..9af6c85 100644 --- a/test/helpers.ts +++ b/test/helpers.ts @@ -1,8 +1,8 @@ -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 @@ -10,19 +10,18 @@ const matchSnapshot: Snapshot = init({ 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): Test => { - const test1 = testOriginal(testName, () => fn(name(test1) ?? '')); + const test1 = testOriginal(testName, () => fn(name(test1) ?? "")); return test1; }; diff --git a/test/index.ts b/test/index.ts index 288b38a..46df794 100644 --- a/test/index.ts +++ b/test/index.ts @@ -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); }); diff --git a/test/json.ts b/test/json.ts index 73d9272..68cf275 100644 --- a/test/json.ts +++ b/test/json.ts @@ -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); }) ];