Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock as iterable object #9

Closed
iiSergey opened this issue Dec 28, 2019 · 8 comments · Fixed by #41
Closed

Mock as iterable object #9

iiSergey opened this issue Dec 28, 2019 · 8 comments · Fixed by #41
Assignees

Comments

@iiSergey
Copy link
Contributor

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)
@marchaos marchaos self-assigned this Jan 2, 2020
@viceice
Copy link

viceice commented Jan 14, 2020

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
Copy link
Owner

marchaos commented Jan 15, 2020

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
Copy link

viceice commented Jan 15, 2020

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
Copy link
Owner

This should be fixed in #13. Anyone able to try this?

@jfemia
Copy link

jfemia commented Feb 19, 2020

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
Copy link

rlugge commented Mar 12, 2020

@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
Copy link

tpischke-bedag commented Jul 1, 2020

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
Copy link

ciekawy commented Jun 23, 2021

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

Beraliv pushed a commit to Beraliv/jest-mock-extended that referenced this issue May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants