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

Improve error handling of stubs option #465

Closed
iztsv opened this issue Mar 12, 2018 · 6 comments
Closed

Improve error handling of stubs option #465

iztsv opened this issue Mar 12, 2018 · 6 comments

Comments

@iztsv
Copy link
Contributor

iztsv commented Mar 12, 2018

Version

1.0.0-beta.12

Reproduction link

https://github.com/ilyaztsv/jest-and-vue-test-utils-stubs

Steps to reproduce

  • npm i
  • npm run test

What is expected?

tests should end successfully and terminal console should contain html of rendered MyComponent:

./component.spec.js
  MyComponent
    ✓ should create (15ms)
    ✓ snapshot (27ms)

  console.log component.spec.js:27
    <div class="component"><child-component></child-component></div>

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        2.423s

What is actually happening?

MyComponent
    ✓ should create (12ms)
    ✕ snapshot (27ms)

  ● MyComponent › snapshot

    RangeError: Maximum call stack size exceeded

      at VueComponent.Vue._render (node_modules/vue/dist/vue.runtime.common.js:4540:7)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 passed, 2 total
Snapshots:   0 total
Time:        2.172s
@eddyerburgh
Copy link
Member

eddyerburgh commented Mar 12, 2018

The problem is you're stubbing the stub with itself, which leads to a circular reference.

You can fix your tests by passing an element, or true in the stubs object:

stubs: {
  'child-component': '<div />',
  'child-component': true
}

We should throw a more useful error, and make it clear in the docs that you need to pass either string HTML tag, Component object, or Boolean. I'll keep this issue open to make sure we improve the error handling and docs.

@eddyerburgh eddyerburgh changed the title "Maximum call stack size exceeded" for jest vue-test-utils stubs Improve error handling of stubs option Mar 13, 2018
@iztsv
Copy link
Contributor Author

iztsv commented Mar 13, 2018

@eddyerburgh thank you for the replay.

I've found out one interesting thing that if I use Component decorator from vue-class-component for my components, stubs works fine even if I'am stubbing the stub with a stub.

Please see the repo above:

Reproduction link

https://github.com/ilyaztsv/jest-and-vue-test-utils-stubs

Steps to reproduce

  • git checkout with-vue-class-component
  • npm i
  • npm run test

Result

 FAIL  ./my-component.spec.js
  ● MyComponent › snapshot

    RangeError: Maximum call stack size exceeded

      at VueComponent.Vue._render (node_modules/vue/dist/vue.runtime.common.js:4540:7)

 PASS  ./my-component-class.spec.js
  ● Console

    console.log my-component-class.spec.js:24
      <div><h1>MyComponentClass</h1> <child-component-class></child-component-class></div>

@iztsv
Copy link
Contributor Author

iztsv commented Mar 18, 2018

@eddyerburgh Hello!

Do you have any news about the message above? 😃

@eddyerburgh
Copy link
Member

So the problem you posted is that your registering a stub component that should render the component you're trying to stub. When Vue tries to resolve the component it gets stuck in an infinite loop:

child-component renders child-component which renders child-component, which renders child-component etc.

I'm not sure why because I haven't seen the code, but in your second example, Vue will not be getting stuck in the loop because it can resolve the component without getting caught in a loop.

@iztsv
Copy link
Contributor Author

iztsv commented Mar 19, 2018

Thanks!
So, I close the issue and will try to send PR for #410 in order to get functionality for my goals 😄

@iztsv iztsv closed this as completed Mar 19, 2018
@iztsv
Copy link
Contributor Author

iztsv commented Mar 19, 2018

Sorry. I reopen the issue because:

I'll keep this issue open to make sure we improve the error handling and docs.

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

No branches or pull requests

2 participants