marchaos / jest-mock-extended

Type safe mocking extensions for Jest https://www.npmjs.com/package/jest-mock-extended
MIT License
845 stars 59 forks source link

Mock as iterable object #9

Closed iiSergey closed 4 years ago

iiSergey commented 4 years ago

Hello I had problem with expect(mock).toEqual(mock), because in a method toEqual be call iterator for the mock and own mock return not correct data for an iterator. You can see more information in https://medium.com/front-end-weekly/thank-u-symbol-iterator-next-aef9f09ff78. I used expect(mock.property).toEqual(mock.property)

interface SomeObject {
  name: string;
}
it('should work', async () => {
  const mockObject= mock<SomeObject>();
  mockObject.name = 'mock';
  expect(mockObject.name ).toEqual(name ); // that work correct
  expect(mockObject).toEqual(mockObject); // Error
});

Example Error:

TypeError: Cannot read property 'next' of undefined

    at Object.test (src\Mock.spec.ts:271:37)
    at Object.asyncJestTest (node_modules\jest-jasmine2\build\jasmineAsyncInstall.js:102:37)
    at resolve (node_modules\jest-jasmine2\build\queueRunner.js:43:12)
    at new Promise (<anonymous>)
    at mapper (node_modules\jest-jasmine2\build\queueRunner.js:26:19)
    at promise.then (node_modules\jest-jasmine2\build\queueRunner.js:73:41)
    at process.internalTickCallback (internal/process/next_tick.js:77:7)
viceice commented 4 years ago

Same here with an angular 8.x app:

 TypeError: function is not iterable (cannot read property Symbol(Symbol.iterator))
        at new Set (<anonymous>)

      at _ValueOutputAstTransformer.visitStringMap (../packages/compiler/src/output/value_util.ts:29:23)
      at visitValue (../packages/compiler/src/util.ts:40:20)
      at ../packages/compiler/src/output/value_util.ts:32:38
          at Array.forEach (<anonymous>)
      at _ValueOutputAstTransformer.visitStringMap (../packages/compiler/src/output/value_util.ts:30:22)
      at visitValue (../packages/compiler/src/util.ts:40:20)
      at convertValueToOutputAst (../packages/compiler/src/output/value_util.ts:18:10)
      at singleProviderDef (../packages/compiler/src/view_compiler/provider_compiler.ts:116:22)
      at providerDef (node_modules/@angular/compiler/bundles/compiler.umd.js:22024:13)
      at ../packages/compiler/src/ng_module_compiler.ts:38:62
          at Array.map (<anonymous>)
      at NgModuleCompiler.compile (../packages/compiler/src/ng_module_compiler.ts:38:44)
      at JitCompiler._compileModule (../packages/compiler/src/jit/compiler.ts:153:52)
      at ../packages/compiler/src/jit/compiler.ts:119:31
      at Object.then (../packages/compiler/src/util.ts:89:74)
      at JitCompiler._compileModuleAndAllComponents (../packages/compiler/src/jit/compiler.ts:115:22)
      at JitCompiler.compileModuleAndAllComponentsSync (../packages/compiler/src/jit/compiler.ts:65:38)
      at CompilerImpl.compileModuleAndAllComponentsSync (../packages/platform-browser-dynamic/src/compiler_factory.ts:61:35)
      at TestingCompilerImpl.compileModuleAndAllComponentsSync (../../packages/platform-browser-dynamic/testing/src/compiler_factory.ts:52:27)
      at TestBedViewEngine._initIfNeeded (../../packages/core/testing/src/test_bed.ts:376:28)
      at TestBedViewEngine.createComponent (../../packages/core/testing/src/test_bed.ts:570:10)
      at Function.TestBedViewEngine.createComponent (../../packages/core/testing/src/test_bed.ts:232:36)
marchaos commented 4 years ago

Hey Think this should be possible to fix, given https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler/ownKeys

Can you tell me what it was doing here - at _ValueOutputAstTransformer.visitStringMap (../packages/compiler/src/output/value_util.ts:29:23) ?

I.e for i in or Object.keys() ?

viceice commented 4 years ago

I think he is here: https://github.com/angular/angular/blob/8.2.x/packages/compiler/src/output/value_util.ts#L29

it is happening, when i mock an angular service implementation. plain objects are working fine

marchaos commented 4 years ago

This should be fixed in https://github.com/marchaos/jest-mock-extended/pull/13. Anyone able to try this?

jfemia commented 4 years ago

This still errors in 1.0.8:

interface SomeObject {
    name: string;
}
const mockObject= mock<SomeObject>();
expect(mockObject).toEqual(mockObject); // Error
TypeError: Cannot read property 'next' of undefined
rlugge commented 4 years ago

@jfemia that's the exact error I'm getting. And I really, really, really would like to find a way to mock my interfaces rather than being forced to create a dummy object and then mocking that.

tpischke-bedag commented 4 years ago

Angular services still not working in 1.0.9

TypeError: function is not iterable (cannot read property Symbol(Symbol.iterator)) at new Set ()

Created #43 to track this problem explicitly

ciekawy commented 3 years ago

I'm getting similar behavior with axios while passing mocked data object