diff --git a/spec/regression/issues/138.test.ts b/spec/regression/issues/138.test.ts new file mode 100644 index 0000000..ed6a8c0 --- /dev/null +++ b/spec/regression/issues/138.test.ts @@ -0,0 +1,13 @@ +import test from 'ava' + +import { Substitute } from '../../../src' + +interface Library { } + +test('issue 138: serializes to JSON compatible data', t => { + const lib = Substitute.for() + const result = JSON.stringify(lib) + + t.true(typeof result === 'string') + t.is(result, '"@Substitute {\\n}"') +}) diff --git a/spec/regression/issues/27.test.ts b/spec/regression/issues/27.test.ts new file mode 100644 index 0000000..9bb1b75 --- /dev/null +++ b/spec/regression/issues/27.test.ts @@ -0,0 +1,39 @@ +import test from 'ava' +import { types } from 'util' + +import { Substitute } from '../../../src' + +interface Library { + subSection: () => string +} + +// Adapted snipped extracted from https://github.com/angular/angular/blob/main/packages/compiler/src/parse_util.ts#L176 +// This is to reproduce the behavior of the Angular compiler. This function tries to extract the id from a reference. +const identifierName = (compileIdentifier: { reference: any } | null | undefined): string | null => { + if (!compileIdentifier || !compileIdentifier.reference) { + return null + } + const ref = compileIdentifier.reference + if (ref['__anonymousType']) { + return ref['__anonymousType'] + } +} + +test('issue 27: mocks should work with Angular TestBed', t => { + const lib = Substitute.for() + lib.subSection().returns('This is the mocked value') + const result = identifierName({ reference: lib }) + + t.not(result, null) + t.true(types.isProxy(result)) + + const jitId = `jit_${result}` + t.is(jitId, 'jit_property<__anonymousType>: ') +}) + +test('issue 27: subsitute node can be coerced to a primitive value', t => { + const lib = Substitute.for() + t.true(typeof `${lib}` === 'string') + t.true(typeof (lib + '') === 'string') + t.is(`${lib}`, '@Substitute {\n}') +}) diff --git a/src/SubstituteNode.ts b/src/SubstituteNode.ts index 8f76d2e..3abc044 100644 --- a/src/SubstituteNode.ts +++ b/src/SubstituteNode.ts @@ -14,7 +14,7 @@ const clearTypeToFilterMap: Record is.CONTEXT.substitution(node.context) } -type SpecialProperty = typeof instance | typeof inspect.custom | 'then' +type SpecialProperty = typeof instance | typeof inspect.custom | typeof Symbol.toPrimitive | 'then' | 'toJSON' type RootContext = { substituteMethodsEnabled: boolean } export class SubstituteNode extends SubstituteNodeBase implements ObjectSubstitute, SubstituteNodeModel { @@ -259,14 +259,16 @@ export class SubstituteNode extends SubstituteNodeBase implements ObjectSubstitu } private isSpecialProperty(property: PropertyKey): property is SpecialProperty { - return property === SubstituteNode.instance || property === inspect.custom || property === 'then' + return property === SubstituteNode.instance || property === inspect.custom || property === Symbol.toPrimitive || property === 'then' || property === 'toJSON' } private evaluateSpecialProperty(property: SpecialProperty) { switch (property) { case SubstituteNode.instance: return this + case 'toJSON': case inspect.custom: + case Symbol.toPrimitive: return this.printableForm.bind(this) case 'then': return