Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Question] Importing static assets (.png) in component tests #22657

@ysulyma

Description

@ysulyma

System info

  • Playwright Version: 1.32.3
  • Operating System: All
  • Browser: All
  • Other info:

Source code

I'm using @playwright/experimental-ct-react to test components. Some of these take images as props. In tests, importing static assets, e.g.

import thumbnail from "../assets/thumbnail.png";

gives the error SyntaxError: Invalid or unexpected token on the import line. I believe Vite handles these imports with no config, so I'm not sure what to add to ctViteConfig.

  • I provided exact source code that allows reproducing the issue locally.

Config file

// playwright.config.ts
import {defineConfig, devices} from "@playwright/experimental-ct-react";

/**
 * See https://playwright.dev/docs/test-configuration.
 */
export default defineConfig({
  testDir: "./ct",
  /* The base directory, relative to the config file, for snapshot files created with toMatchSnapshot and toHaveScreenshot. */
  snapshotDir: "./__snapshots__",
  /* Maximum time one test can run for. */
  timeout: 1 * 1000,
  /* Run tests in files in parallel */
  fullyParallel: true,
  /* Fail the build on CI if you accidentally left test.only in the source code. */
  forbidOnly: !!process.env.CI,
  /* Retry on CI only */
  retries: process.env.CI ? 2 : 0,
  /* Opt out of parallel tests on CI. */
  workers: process.env.CI ? 1 : undefined,
  /* Reporter to use. See https://playwright.dev/docs/test-reporters */
  reporter: "html",
  /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
  use: {
    /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
    trace: "on-first-retry",

    /* Port to use for Playwright component endpoint. */
    ctPort: 3100,
  },

  /* Configure projects for major browsers */
  projects: [
    {
      name: "chromium",
      use: {...devices["Desktop Chrome"]},
    },
    {
      name: "firefox",
      use: {...devices["Desktop Firefox"]},
    },
    {
      name: "webkit",
      use: {...devices["Desktop Safari"]},
    },
  ],
});

Test file (self-contained)

import {test} from "@playwright/experimental-ct-react";

import thumbnail from "../assets/thumbnail.png";

test.use({viewport: {width: 500, height: 500}});

test("should work", async ({mount}) => {
  await mount(<img src={thumbnail} />);
});

Steps

  • Run the test

Expected

Playwright should render the thumbnail

Actual

SyntaxError: Invalid or unexpected token

   at App.spec.tsx:3

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions