From e21f7feb012d35cdfd45c8c1632f6cbe6e5542ab Mon Sep 17 00:00:00 2001 From: Homa Wong Date: Wed, 10 Jan 2018 18:27:44 -0800 Subject: [PATCH] feat: expose createCallRecord (#26) * feat: expose CallRecord.create Use this function to create an instance of CallRecord in memory. On top of the data, a CallRecord instance has a `tersify()` method to ensure when stringifing it using `tersify()`, all data in the CallRecord will be preserved. * fix: CallRecord.create Change the expose to CallRecord.create. And fix tersifing it. * fix: reference --- src/CallEntry.ts | 20 ++++++++++++++++++ src/CallRecord.ts | 25 +++++++++++++++++++++++ src/createCallRecordCreator.ts | 35 ++++++++------------------------ src/createSatisfier.exec.spec.ts | 2 +- src/index.ts | 1 + src/interfaces.ts | 22 +------------------- src/spy.spec.ts | 24 ++++++++++++++-------- src/spy.ts | 2 +- 8 files changed, 73 insertions(+), 58 deletions(-) create mode 100644 src/CallEntry.ts create mode 100644 src/CallRecord.ts diff --git a/src/CallEntry.ts b/src/CallEntry.ts new file mode 100644 index 0000000..8288a0b --- /dev/null +++ b/src/CallEntry.ts @@ -0,0 +1,20 @@ +import { CallRecord } from './CallRecord' + +export interface CallEntry extends Promise { + inputs: any[], + /** + * Synchronous result. + */ + output: any, + /** + * Synchronous error got thrown. + */ + error: any, + getCallRecord(): Promise +} + +// export const CallEntry = { +// create(callRecord: CallRecord) { +// return {} +// } +// } diff --git a/src/CallRecord.ts b/src/CallRecord.ts new file mode 100644 index 0000000..853002d --- /dev/null +++ b/src/CallRecord.ts @@ -0,0 +1,25 @@ +import { tersible, tersify } from 'tersify' + +export interface CallRecord { + inputs: any[], + output: any, + error: any, + asyncOutput: any, + asyncError: any, + tersify(): string +} + +export const CallRecord = { + /** + * Creates a call record object. + */ + create({ inputs, output, error, asyncOutput, asyncError }: CallRecord) { + return tersible({ inputs, output, error, asyncOutput, asyncError }, + () => { + const obj = { inputs, output, error } as CallRecord + if (asyncError !== undefined) obj.asyncError = asyncError + if (asyncOutput !== undefined) obj.asyncOutput = asyncOutput + return tersify(obj, { maxLength: Infinity }) + }) + } +} diff --git a/src/createCallRecordCreator.ts b/src/createCallRecordCreator.ts index 066d75d..1188326 100644 --- a/src/createCallRecordCreator.ts +++ b/src/createCallRecordCreator.ts @@ -1,6 +1,5 @@ -import { tersify, tersible } from 'tersify' - -import { CallEntry } from './interfaces' +import { CallRecord } from './CallRecord' +import { CallEntry } from './CallEntry' export function createCallRecordCreator(args: any[]) { let resolve @@ -13,31 +12,13 @@ export function createCallRecordCreator(args: any[]) { inputs: args, getCallRecord() { return callEntry.then(asyncOutput => { - const { inputs, output, error } = callEntry - return tersible({ - inputs, - output, - error, - asyncOutput - }, () => tersify({ - inputs, - output, - error, - asyncOutput - }, { maxLength: Infinity })) + return CallRecord.create({ + ...callEntry, asyncOutput + }) }, asyncError => { - const { inputs, output, error } = callEntry - return tersible({ - inputs, - output, - error, - asyncError - }, () => tersify({ - inputs, - output, - error, - asyncError - }, { maxLength: Infinity })) + return CallRecord.create({ + ...callEntry, asyncError + }) }) } }) as CallEntry diff --git a/src/createSatisfier.exec.spec.ts b/src/createSatisfier.exec.spec.ts index 9ae2336..96c09dd 100644 --- a/src/createSatisfier.exec.spec.ts +++ b/src/createSatisfier.exec.spec.ts @@ -1,7 +1,7 @@ import { test } from 'ava' import { createSatisfier } from './index' -import { assertExec, assertRegExp } from './testUtil'; +import { assertExec, assertRegExp } from './testUtil' test('primitive types without specifing generic will work without issue.', t => { t.is(createSatisfier(1).exec(1), undefined) diff --git a/src/index.ts b/src/index.ts index 207aeb6..52754f5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ +export * from './CallRecord' export * from './createSatisfier' export * from './interfaces' export * from './isInRange' diff --git a/src/interfaces.ts b/src/interfaces.ts index 37145bc..7a61ae0 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -1,26 +1,6 @@ import { Tersible } from 'tersify' -export interface CallRecord { - inputs: any[], - output: any, - error: any, - asyncOutput: any, - asyncError: any, - tersify(): string -} - -export interface CallEntry extends Promise { - inputs: any[], - /** - * Synchronous result. - */ - output: any, - /** - * Synchronous error got thrown. - */ - error: any, - getCallRecord(): Promise -} +import { CallEntry } from './CallEntry' export interface Spec { spiedFn: T, diff --git a/src/spy.spec.ts b/src/spy.spec.ts index 25d0f5d..0a4fc83 100644 --- a/src/spy.spec.ts +++ b/src/spy.spec.ts @@ -1,7 +1,6 @@ import { test } from 'ava' import { spy } from './index' -import { tersify } from 'tersify'; function increment(x: number) { return ++x } @@ -32,7 +31,7 @@ test('tersify for sync call', async t => { t.is(fn(1), 2) const record = await calls[0].getCallRecord() - t.is(record.tersify(), `{ inputs: [1], output: 2, error: undefined, asyncOutput: undefined }`) + t.is(record.tersify(), `{ inputs: [1], output: 2, error: undefined }`) }) // this is not a valid test as the package is used for boundary testing. @@ -79,7 +78,9 @@ test('then() will receive result from promise', async t => { const { fn, calls } = spy(resolve) // tslint:disable-next-line fn(1) - t.is(await calls[0], 1) + return calls[0].then(actual => { + t.is(actual, 1) + }) }) test('result from promise can be retrieved from await on the call', async t => { @@ -101,16 +102,23 @@ test('catch() will receive error thrown by promise', async t => { }) }) -test('tersify for sync call', async t => { - const { fn, calls } = spy(reject) +test('tersify for resolve call', async t => { + const { fn, calls } = spy(resolve) + // tslint:disable-next-line + fn(1) + return calls[0].getCallRecord() + .then(record => { + t.is(record.tersify(), `{ inputs: [1], output: {}, error: undefined, asyncOutput: 1 }`) + }) +}) - return fn(1).catch(actualError => { - console.log(tersify(actualError)) +test('tersify for reject call', async t => { + const { fn, calls } = spy(reject) + return fn(1).catch(() => { return calls[0].getCallRecord() .then(record => { t.is(record.tersify(), `{ inputs: [1], output: {}, error: undefined, asyncError: { message: '1' } }`) }) }) }) - diff --git a/src/spy.ts b/src/spy.ts index 27a39a5..533f68c 100644 --- a/src/spy.ts +++ b/src/spy.ts @@ -1,5 +1,5 @@ +import { CallEntry } from './CallEntry' import { createCallRecordCreator } from './createCallRecordCreator' -import { CallEntry } from './interfaces' export interface Spy { calls: ReadonlyArray,