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

Component Testing - Option for loading plugins and modules in NuxtJS #22518

Open
rklos opened this issue Jun 24, 2022 · 10 comments
Open

Component Testing - Option for loading plugins and modules in NuxtJS #22518

rklos opened this issue Jun 24, 2022 · 10 comments
Labels
CT Issue related to component testing Triaged Issue has been routed to backlog. This is not a commitment to have it prioritized by the team. type: enhancement Requested enhancement of existing feature

Comments

@rklos
Copy link

rklos commented Jun 24, 2022

What would you like?

For now, component testing in cypress doesn't load up plugins and modules listed in nuxt.config.js file when used with NuxtJS framework.

It will be nice to have an option to include NuxtJS plugin or module inside tests.

There is a way to load "pure Vue" plugins in @cypress/vue package, but NuxtJS modules and plugins have different interface and approach.

  • Modules can import many plugins, change runtime configuration and do some other stuff in meantime
  • Plugins can be made for server side, client side or both. It can can extend Vue, register global components, but it can also do other things, such as adding HTML code to the DOM tree, sending a request to the server, deciding to redirect the user to another URL and many other stuff not necessarily related to Vue.

So we need dedicated option for loading extensions made for NuxtJS.

Why is this needed?

Most of components in my project depend on many different modules imported into NuxtJS in nuxt.config.js.

For example, my component uses dayjs from this module: https://www.npmjs.com/package/@nuxtjs/dayjs. For now component testing throws errors because of undefined this.$dayjs.

Another example: almost every of my component contains at least one child component from Vuetify module (https://www.npmjs.com/package/@nuxtjs/vuetify). Now in each test, part of HTML template is not rendered due to missing components from the Vuetify module. I use nearly all of them, so mocking almost the entire library seems hard and impossible to maintain.

Some of NuxtJS modules/plugins has their equivalents in "pure Vue" (and can be used as a normal plugin inside @cypress/vue, but this sometimes requires installing a separate package just for testing purposes). But there are some extensions that are made especially for Nuxt, and we will have to do some odd hacky workarounds (that are sometimes almost impossible without forking and exporting some stuff outside the module).

Anyway, if cypress have an option to select Nuxt as a framework inside Component Testing settings, I will expect that as basic stuff as plugins and modules in Nuxt will work without any advanced hacks and workarounds.

Other

Steps to reproduction:

  1. Use official Nuxt2+Cypress sample app (https://github.com/cypress-io/cypress-component-testing-apps/tree/main/vue2-nuxt2-js)
  2. Install any module (for example https://www.npmjs.com/package/@nuxtjs/dayjs)
  3. Use that module inside component
  4. See error at component test
@mjhenkes mjhenkes added CT Issue related to component testing type: enhancement Requested enhancement of existing feature labels Jun 24, 2022
@bademiya5306
Copy link

has anyone cracked this?

@rklos
Copy link
Author

rklos commented Aug 4, 2022

Can anyone investigate this topic?

@bbialas
Copy link

bbialas commented Oct 24, 2022

+1

@JasonLandbridge
Copy link

Bump!

I'm having the same issue where @nuxtjs/i18n isn't loaded which messes up components that use translations

@alexanderop
Copy link

Bump!

I also have the same problem with a nuxt 2 project that is using

'@nuxtjs/composition-api/module',

@limtaegeun
Copy link

limtaegeun commented Feb 8, 2023

Me too!
same problem using @nuxtjs/composition-api/module in nuxt 2 project

@nagash77 nagash77 added routed-to-ct Triaged Issue has been routed to backlog. This is not a commitment to have it prioritized by the team. and removed routed-to-ct labels Apr 13, 2023
@Jicmou
Copy link

Jicmou commented Apr 24, 2023

+1 here. Trying to install cypress on an existing project with multiple plugins.

@mcmc4519
Copy link

mcmc4519 commented May 2, 2023

+1 there.

Realy need this function

@EtiennePASSOT
Copy link

+1

@mcmc4519
Copy link

mcmc4519 commented May 9, 2023

I wanna to share example of my code

I use vuetify, axios and event bus


import './commands';

import axios from 'axios';
import { mount } from 'cypress/vue2';

import Vuetify from 'vuetify';
import Vue from 'vue';
Vue.use(Vuetify);
import '../../../client/assets/app.css';
import 'vuetify/dist/vuetify.min.css';
import { VApp } from 'vuetify/lib/components/VApp';
const vuetifyOptions = {};
import applyConverters from 'axios-case-converter';

Cypress.Commands.add('mount', (component, args) => {
	const axiosPlugin = {
		install(app) {
			app.prototype.$axios = axios;
		},
	};

	const busPlugin = {
		install(app) {
			app.prototype.$bus = new Vue();
		},
	};

	args = args || {};
	applyConverters(axios, {
		ignoreHeaders: true,
	});

	args.extensions = args.extensions || {};
	args.extensions.plugins = args.extensions.plugins || [];
	args.extensions.plugins.push(axiosPlugin);
	args.extensions.plugins.push(busPlugin);

	args.vuetify = new Vuetify(vuetifyOptions);

	// return mount(component, args);

	return mount({ render: h => h(VApp, [h(component, args)]) }, args);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CT Issue related to component testing Triaged Issue has been routed to backlog. This is not a commitment to have it prioritized by the team. type: enhancement Requested enhancement of existing feature
Projects
None yet
Development

No branches or pull requests