playwright-community / jest-playwright

Running tests using Jest & Playwright
MIT License
532 stars 75 forks source link

jestPlaywright is not defined #788

Closed trusktr closed 2 years ago

trusktr commented 2 years ago

Describe the bug

jestPlaywright is not defined

on v1.6.0

To Reproduce

Expected behavior

Screenshots

Desktop (please complete the following information):

Jest configuration (Either in the package.json > jest or in the jest.config.js):

Is there a config option that makes this not be global?

As a workaround, can I just import it?

trusktr commented 2 years ago

Here's the jest config:

const { TEST_HOST } = require('./test/config/server.js');

const sharedConfig = {
  errorOnDeprecated: true,
  globals: {
    TEST_HOST,
  },
  globalSetup: './test/config/jest.setup.js',
  globalTeardown: './test/config/jest.teardown.js',
  resetModules: true,
  restoreMocks: true,
  testEnvironment: 'jsdom',
};

module.exports = {
  // Adding globals to config root for easier importing into .eslint.js, but
  // as of Jest 26.4.2 these globals need to be added to each project config
  // as well.
  globals: sharedConfig.globals,
  projects: [
    // Unit Tests (Jest)
    {
      ...sharedConfig,
      displayName: 'unit',
      setupFilesAfterEnv: ['<rootDir>/test/config/jest.setup-tests.js'],
      testMatch: ['<rootDir>/test/unit/*.test.js'],
      testURL: `${TEST_HOST}/_blank.html`,
    },
    // Integration Tests (Jest)
    {
      ...sharedConfig,
      displayName: 'integration',
      setupFilesAfterEnv: ['<rootDir>/test/config/jest.setup-tests.js'],
      testMatch: ['<rootDir>/test/integration/*.test.js'],
      testURL: `${TEST_HOST}/_blank.html`,
    },
    // E2E Tests (Jest + Playwright)
    {
      ...sharedConfig,
      displayName: 'e2e',
      preset: 'jest-playwright-preset',
      setupFilesAfterEnv: [
        '<rootDir>/test/config/jest-playwright.setup-tests.js',
      ],
      testEnvironmentOptions: {
        'jest-playwright': {
          // prettier-ignore
          browsers: [
            'chromium',
            'firefox',
            'webkit',
          ],
          launchOptions: {
            // headless: false,
            // devtools: true,
          },
        },
      },
      testMatch: ['<rootDir>/test/e2e/*.test.js'],
    },
  ],
};
// jest-playwright.setup-tests.js',
/* global browserName page */
const { configureToMatchImageSnapshot } = require('jest-image-snapshot');

// Lifecycle Hooks
// -----------------------------------------------------------------------------
beforeAll(async () => {
  // Storing separate image comparison configurations for easy switching while
  // evaluating results. Once more e2e tests are in place, we'll settle on a
  // configuration, allowing us to safely remove the multi-config object below.
  const config = {
    // Pixel-based image comparisons
    // https://github.com/mapbox/pixelmatch#pixelmatchimg1-img2-output-width-height-options
    pixelCompare: {
      customDiffConfig: {
        threshold: 0.3,
      },
      failureThreshold: 0.04,
    },
    // Structural Similarity Index Measure (SSIM) comparisons
    // https://github.com/obartra/ssim
    ssimCompare: {
      comparisonMethod: 'ssim',
      failureThreshold: 0.15,
    },
  };

  const toMatchImageSnapshot = configureToMatchImageSnapshot({
    allowSizeMismatch: true, // Windows CI fix
    customSnapshotIdentifier(data) {
      return `${data.defaultIdentifier}-${browserName}`;
    },
    diffDirection: 'vertical',
    failureThresholdType: 'percent',
    noColors: true,
    runInProcess: true, // macOS CI fix
    // pixel- or ssim-based configuration
    ...config.pixelCompare,
  });

  expect.extend({ toMatchImageSnapshot });
});

beforeEach(async () => {
  await jestPlaywright.resetContext(); // <-------------- HERE: jestPlaywright is not defined

  // Goto URL ()
  // https://playwright.dev/#path=docs%2Fapi.md&q=pagegotourl-options
  // NOTE: Tests typically begin by navigating to a page for testing. When
  // this doesn't happen, Playwright operates on the "about:blank" page which
  // will cause operations that require the window location to be a valid URL
  // to fail (e.g. AJAX requests). To avoid these issues, this hook ensures
  // that each tests begins by a blank HTML page.
  await page.goto(`${TEST_HOST}/_blank.html`);
});
trusktr commented 2 years ago

The repo in question is https://github.com/docsifyjs/docsify. Playwright won't run for me locally, and I encountered this issue when updating jest-playwright-preset (and related packages) to try and make it work on latest Ubuntu. Fiddling with it I keep ending with one error or another between supposedly-compatible peer libs.

mxschmitt commented 2 years ago

Sorry for the late response, since you seem to have migrated to the official test-runner, I'm closing this issue here.