npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@stencil/playwright

v0.2.1

Published

Testing adapter to use Playwright with Stencil

Downloads

10,072

Readme

@stencil/playwright

[!NOTE] The Stencil Playwright adapter is currently an experimental package. Breaking changes may be introduced at any time.

The Stencil Playwright adapter is designed to only work with version 4.13.0 and higher of Stencil!

Stencil Playwright is a testing adapter package that allows developers to easily use the Playwright testing framework in their Stencil project.

For full documentation, please see the Playwright testing docs on the official Stencil site.

Getting Started

  1. Install the necessary dependencies:

    npm i @stencil/playwright @playwright/test --save-dev
  2. Install the Playwright browser binaries:

    npx playwright install
  3. Create a Playwright config at the root of your Stencil project:

    import { expect } from '@playwright/test';
    import { matchers, createConfig } from '@stencil/playwright';
    
    // Add custom Stencil matchers to Playwright assertions
    expect.extend(matchers);
    
    export default createConfig({
      // Overwrite Playwright config options here
    });

    The createConfig() function is a utility that will create a default Playwright configuration based on your project's Stencil config. Read more about how to use this utility in the API section.

    [!NOTE] For createConfig() to work correctly, your Stencil config must be named either stencil.config.ts or stencil.config.js.

  4. update your project's tsconfig.json to add the ESNext.Disposable option to the lib array:

    {
      lib: [
        ...,
        "ESNext.Disposable"
      ],
      ...
    }

    [!NOTE] This will resolve a build error related to Symbol.asyncDispose. If this is not added, tests may fail to run since the Stencil dev server will be unable to start due to the build error.

  5. Ensure the Stencil project has a www output target. Playwright relies on pre-compiled output running in a dev server to run tests against. When using the createConfig() helper, a configuration for the dev server will be automatically created based on the Stencil project's www output target config and dev server config. If no www output target is specified, tests will not be able to run.

  6. Add the copy option to the www output target config:

    {
       type: 'www',
       serviceWorker: null,
       copy: [{ src: '**/*.html' }, { src: '**/*.css' }]
    }

    This will clone all HTML and CSS files to the www output directory so they can be served by the dev server. If you put all testing related files in specific directory(s), you can update the copy task glob patterns to only copy those files:

    {
       type: 'www',
       serviceWorker: null,
       copy: [{ src: '**/test/*.html' }, { src: '**/test/*.css' }]
    }

    [!NOTE] If the copy property is not set, you will not be able to use the page.goto testing pattern!

  7. Test away! Check out the e2e testing page on the Stencil docs for more help getting started writing tests.

Testing Patterns

page.goto()

The goto() method allows tests to load a pre-defined HTML template. This pattern is great if a test file has many tests to execute that all use the same HTML code or if additional script or style tags need to be included in the HTML. However, with this pattern, developers are responsible for defining the necessary script tags pointing to the Stencil entry code (so all web components are correctly loaded and registered).

<!-- my-component.e2e.html -->

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf8" />

    <!-- Replace with the path to your entrypoint -->
    <script src="./build/test-app.esm.js" type="module"></script>
    <script src="./build/test-app.js" nomodule></script>
  </head>
  <body>
    <my-component first="Stencil"></my-component>
  </body>
</html>
// my-component.e2e.ts

import { expect } from '@playwright/test';
import { test } from '@stencil/playwright';

test.describe('my-component', () => {
  test('should render the correct name', async ({ page }) => {
    // The path here is the path to the www output relative to the dev server root directory
    await page.goto('/my-component/my-component.e2e.html');

    // Rest of test
  });
});

page.setContent()

The setContent() method allows tests to define their own HTML code on a test-by-test basis. This pattern is helpful if the HTML for a test is small, or to avoid affecting other tests is using the page.goto() pattern and modifying a shared HTML template file. With this pattern, the script tags pointing to Stencil entry code will be automatically injected into the generated HTML.

// my-component.e2e.ts

import { expect } from '@playwright/test';
import { test } from '@stencil/playwright';

test.describe('my-component', () => {
  test('should render the correct name', async ({ page }) => {
    await page.setContent('<my-component first="Stencil"></my-component>');

    // Rest of test
  });
});

API

createConfig(overrides?: PlaywrightTestConfig): Promise<PlaywrightTestConfig>

Returns a Playwright test configuration.

overrides, as the name implies, will overwrite the default configuration value(s) if supplied. These values can include any valid Playwright config option. Changing option values in a nested object will use a "deep merge" to combine the default and overridden values. So, creating a config like the following:

import { expect } from '@playwright/test';
import { matchers, createConfig } from '@stencil/playwright';

expect.extend(matchers);

export default createConfig({
  // Change which test files Playwright will execute
  testMatch: '*.spec.ts',

  webServer: {
    // Only wait max 30 seconds for server to start
    timeout: 30000,
  },
});

Will result in:

{
  testMatch: '*.spec.ts',
  use: {
    baseURL: 'http://localhost:3333',
  },
  webServer: {
    command: 'stencil build --dev --watch --serve --no-open',
    url: 'http://localhost:3333/ping',
    reuseExistingServer: !process.env.CI,
    // Only timeout gets overridden, not the entire object
    timeout: 30000,
  },
}

test

test designates which tests will be executed by Playwright. See the Playwright API documentation regarding usage.

This package modifies the page fixture and offers a skip utility as discussed below.

page

The page fixture is a class that allows interacting with the current test's browser tab. In addition to the default Playwright Page API, Stencil extends the class with a few additional methods:

  • waitForChanges(): Waits for Stencil components to re-hydrate before continuing.
  • spyOnEvent(): Creates a new EventSpy and listens on the window for an event to emit.

skip

The skip utility allows developers to skip tests for certain browsers or component modes:

test('my-test', ({ page, skip }) => {
    // Skip tests for certain browsers
    skip.browser('firefox', 'This behavior is not available on Firefox');

    // Skip tests for certain modes
    skip.mode('md', 'This behavior is not available in Material Design');

    ...
})