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

[Feature]: Vue generate hooksConfig boilerplate #31217

Open
sand4rt opened this issue Jun 9, 2024 · 0 comments
Open

[Feature]: Vue generate hooksConfig boilerplate #31217

sand4rt opened this issue Jun 9, 2024 · 0 comments

Comments

@sand4rt
Copy link
Collaborator

sand4rt commented Jun 9, 2024

🚀 Feature Request

Allow create-playwright to generate the boilerplate code of the common Vue mount options. These APIs are also in Vue Test Utils and Testing Library, but it's not that obvious that this is also possible through the hooksConfig in one way or the other.

Providing those mount options directly inside the hooksConfig depends on #30269 tho and is desired for Angular as well: #27783 (comment).

Example

// playwright/index.ts
import type {
  Component,
  DefineComponent,
  Plugin,
  ComponentOptions,
  Directive,
} from 'vue';

export type HooksConfig = {
  components?: Record<string, Component | DefineComponent>;
  plugins?: (Plugin | [Plugin, ...any[]])[];
  directives?: Record<string, Directive>;
  provide?: Record<string, any>;
  mixins?: ComponentOptions[];
};

beforeMount<HooksConfig>(async ({ app, hooksConfig }) => {
  for (const [name, component] of Object.entries(hooksConfig?.components || {}))
    app.component(name, component);

  for (const plugin of hooksConfig?.plugins || []) {
    if (Array.isArray(plugin))
      app.use(plugin[0], ...plugin.slice(1))
    else 
      app.use(plugin)
  }

  for (const [name, directive] of Object.entries(hooksConfig?.directives || {}))
    app.directive(name, directive);

  for (const [key, value] of Object.entries(hooksConfig?.provide || {}))
    app.provide(key, value);

  for (const mixin of hooksConfig?.mixins || [])
    app.mixin(mixin);
});
// component.test.ts
import { router } from './router'; // note: `router` is not defined in a .vue file
import { Component } from './Component.vue';
import { Provide } from './provide'; // note: `Provide` is not defined in a .vue file
import { Directive } from './directive'; // note: `Directive` is not defined in a .vue file

await mount(App, {
  hooksConfig: { 
    plugins: [router],
    components: { Component },
    mixins: [Mixin],
    provide: { Provide },
    directives: { Directive },
  },
});

Motivation

These are not documented anywhere, even though most users needs this

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

No branches or pull requests

2 participants