I am writing some unit test but I got trouble with component that have inject
property.
I am using shallowMount
. I did some research about this.And there is way to create fake data for provide
https://vue-test-utils.vuejs.org/api/options.html#provide. But, I didn't see any information or hints about inject
.
So, I need some advices about How to do unit test with inject
in Vuejs?
We recommend using @testing-library/vue for testing components in applications, as its focus aligns better with the testing priorities of applications. Use @vue/test-utils only if you are building advanced components that require testing Vue-specific internals.
What you set in the provide property is what is used to inject into the mounted component.
In my unit test I have
metadataModule = sandbox.createStubInstance(MetadataModule);
metadataService = sandbox.createStubInstance(MetadataService);
wrapper = shallowMount(MoveFileElement, {
provide: {
[SYMBOLS.METADATAMODULE]: metadataModule,
[SYMBOLS.METADATASERVICE]: metadataService,
},
....
Then in my component I have
export default class MoveFileElement extends Mixins(Utilities) {
@Inject(SYMBOLS.METADATAMODULE) public metadataModule!: IMetadataModule;
@Inject(SYMBOLS.METADATASERVICE) public metadataService!: MetadataService;
Now the component has access to the fake versions of the metadata module that I prepared in the unit test.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With