Coder Social home page Coder Social logo

playwright-ct-vue-example's Introduction

Component testing with Playwright and Vue3

Recommended IDE Setup

VSCode + Volar (and disable Vetur) + Playwright Test for VSCode.

Project Setup

npm i

Compile and Hot-Reload for Development

npm run dev

Test project

Run tests from your VS Code, or execute

npm run test

How to add component tests using Playwright

  • npm init vue@latest was used to create a default project.

    npm init vue@latest
  • Install Playwright Text w/ component testing as dev dependencies.

    npm i --save-dev @playwright/test @playwright/experimental-ct-vue
  • tests.js file was added that registers all the components to be tested.

    import register from '@playwright/experimental-ct-vue/register'
    
    import Counter from './src/components/Counter.vue'
    import DocumentationIcon from './src/components/icons/IconDocumentation.vue'
    import HelloWorld from './src/components/HelloWorld.vue'
    import NamedSlots from './src/components/NamedSlots.vue'
    import TheWelcome from './src/components/TheWelcome.vue'
    import WelcomeItem from './src/components/WelcomeItem.vue'
    
    register({
      Counter,
      DocumentationIcon,
      HelloWorld,
      NamedSlots,
      TheWelcome,
      WelcomeItem,
    })
  • tests.html file was added that defines theming for the components and references tests.js.

    <html>
    <head>
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <style>@import '/src/assets/base.css';</style>
    </head>
    <body>
      <div id="app"></div>
      <script type="module" src="/tests.js"></script>
    </body>
    </html>
  • playwright.config.ts was added that executes npm run dev before running tests if it is not already running.

    import { PlaywrightTestConfig, devices } from '@playwright/test';
    
    const config: PlaywrightTestConfig = {
      testDir: 'src',
      forbidOnly: !!process.env.CI,
      retries: process.env.CI ? 2 : 0,
      reporter: process.env.CI ? [
        ['html', { open: 'never' }],
      ] : [
        ['html', { open: 'on-failure' }]
      ],
      webServer: {
        url: 'http://localhost:3000/tests.html',
        command: 'npm run dev',
        reuseExistingServer: !process.env.CI,
      },
      use: {
        baseURL: 'http://localhost:3000/tests.html',
        trace: 'on-first-retry',
      },
      projects: [
        {
          name: 'chromium',
          use: { ...devices['Desktop Chrome'] },
        },
        {
          name: 'firefox',
          use: { ...devices['Desktop Firefox'] },
        },
        {
          name: 'webkit',
          use: { ...devices['Desktop Safari'] },
        },
      ],
    };
    
    export default config;
  • A bunch of .spec.ts and .spec.tsx files were added to src that demonstrate Vue3 component testing with and without the use of JSX syntax.

playwright-ct-vue-example's People

Contributors

pavelfeldman avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.