question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unit testing: 'Injection "$validator" not found'

See original GitHub issue

Versions:

  • VueJs: 2.5.13
  • Vee-Validate: 2.0.4

Description:

I have installed vee-validate with the inject property on false, and then I have a child component that has an inject: ['$validator'] and a parent with $_veeValidate.validator = 'new'. This works perfectly everywhere besides during unit testing.

Steps To Reproduce:

this is my component test and the BaseInput component it is just a component that handles the error handling and the input itself, then it passes all the validation to the parent.

import { mount, createLocalVue } from '@vue/test-utils'
import idObj from 'identity-obj-proxy';

import BaseInput from '@/components/BaseInput'
import VeeValidate from 'vee-validate'

const localVue = createLocalVue()

localVue.use(VeeValidate, {
  inject: false
})

describe('<base-input>', () => {
  it ('should render one input tag', () => {
    const wrapper = mount(BaseInput, {
      localVue,
      propsData: {
        name: 'foo'
      },
      mocks: {
        $style: idObj
      }
    })

    expect(wrapper.contains('input')).toBe(true)
  })
})

When I run the test, it passes but I have a warning in the terminal

[Vue warn]: Injection "$validator" not found

Do you know how to avoid this behavior?

Thanks for you time

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

22reactions
logaretmcommented, Mar 30, 2018

I guess that’s because there is no provider for the $validator dependency in the component tree, It works fine in production because there is a root instance that will always have its own validator and can provide it. In tests, it could be different.

Anyways you can fix that in your tests by making the root instance provide the validator instance:

const v = new VeeValidate.Validator();
const wrapper = mount(TestComponent, {
  localVue: Vue,
  provide: () => ({
    $validator: v
  })
});
1reaction
lusarzcommented, Jun 29, 2018

@logaretm I have small problem with that, but it may be also related with vue-test-utils (on beta.12 this issue didn’t occur).

In unit tests I have computed variable:

errorMessage() {
  return this.errors.first("sample-input");
}

After blur on input -> wrapper.find('input').trigger('blur') -> I expect message to be shown, but in my case it’s not.

Here are reproduction links:

  1. https://codesandbox.io/s/r73x66o1o
  2. https://codesandbox.io/s/134jzjppnl

In first case it works (just click in input, and then click outside - you’ll see error message below). Second situation is a bit different, I set inject to false:

Vue.use(VeeValidate, {
  inject: false
});

and inject inside component:

inject: ["$validator"]

and then it didn’t works. Do you have idea what may be the issue here ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dependency inside a Validator class not initialize in unit test
My question is if the Validator is init as such in unit test, how can I provide the inject of accountService during unit...
Read more >
Test Extensions — FluentValidation documentation
FluentValidation provides some extensions that can aid with testing your validator classes. We recommend treating validators as 'black boxes' - provide ...
Read more >
Testing Dependency Injection • Angular - codecraft.tv
When the component is created since it has its own injector it will resolve the AuthService itself and not forward the request to...
Read more >
How to mock dependencies when unit testing custom ...
Since we are interested in unit testing, there's no Spring application context available, hence we must manually instantiate a Validator and ...
Read more >
Basics of testing components - Angular
The component truly is the template and the class working together. ... testing the component class alone, without DOM involvement, can validate much...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found