Skip to content

TypeError: Cannot read property '$apolloProvider' of undefined #532

Closed
@agosto-chrisbartling

Description

@agosto-chrisbartling
Contributor

I'm getting a TypeError when running my mocha test suite. It looks like a simple fix in vue-apollo.esm.js:

TypeError: Cannot read property '$apolloProvider' of undefined
      at VueComponent.initProvider (dist/webpack:/node_modules/vue-apollo/dist/vue-apollo.esm.js:1789:1)
      at VueComponent.beforeCreate (dist/webpack:/node_modules/vue-apollo/dist/vue-apollo.esm.js:1919:1)
      at invokeWithErrorHandling (dist/webpack:/node_modules/vue/dist/vue.runtime.esm.js:1854:1)
      at callHook (dist/webpack:/node_modules/vue/dist/vue.runtime.esm.js:4190:1)
      at VueComponent.Vue._init (dist/webpack:/node_modules/vue/dist/vue.runtime.esm.js:4965:1)
      at new VueComponent (dist/webpack:/node_modules/vue/dist/vue.runtime.esm.js:5115:1)
      at createInstance (dist/webpack:/node_modules/@vue/test-utils/dist/vue-test-utils.js:4475:1)
      at mount (dist/webpack:/node_modules/@vue/test-utils/dist/vue-test-utils.js:8646:1)
      at shallowMount (dist/webpack:/node_modules/@vue/test-utils/dist/vue-test-utils.js:8677:1)
      at Context.it (dist/webpack:/tests/unit/components/PreviewPlayerStepper.spec.ts:48:1)

The relevant function that is causing the issue...

function initProvider() {
  var options = this.$options; // ApolloProvider injection

  var optionValue = options.apolloProvider;

  if (optionValue) {
    this.$apolloProvider = typeof optionValue === 'function' ? optionValue() : optionValue;
  } else if (options.parent && options.parent.$apolloProvider) {
    this.$apolloProvider = options.parent.$apolloProvider;
  } else if (options.provide) {
    // TODO remove
    // Temporary retro-compatibility
    var provided = typeof options.provide === 'function' ? options.provide.call(this) : options.provide;

    if (provided.$apolloProvider) { // <=== Error occurs here
      this.$apolloProvider = provided.$apolloProvider;
    }
  }
}

For whatever reason, provided can be undefined in some cases. Probably need test to ensure provided is actually set to something before asking if provided.$apolloProvider is truthy.

Activity

agosto-chrisbartling

agosto-chrisbartling commented on Feb 27, 2019

@agosto-chrisbartling
ContributorAuthor

Made a PR to fix this issue: #533

sambigelow

sambigelow commented on Mar 4, 2019

@sambigelow

@agosto-chrisbartling thanks for fixing this!

I am actually running into the problem here on GitLab in our test suite. If I modify the node_module to check if provided exists, the error doesn't happen. We are newly using vue-apollo, but this isn't the first commit to use it and it wasn't happening before and its not causing any problems before. Is there something we could mock out while we wait for this to be published?

sambigelow

sambigelow commented on Mar 4, 2019

@sambigelow

@Akryum when do you think you will be releasing a version that has this fix?

jayporta

jayporta commented on May 2, 2019

@jayporta

I ran into a similar problem but it was in vue-apollo.umd.js. I'll also submit a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @jayporta@agosto-chrisbartling@sambigelow

        Issue actions

          TypeError: Cannot read property '$apolloProvider' of undefined · Issue #532 · vuejs/apollo