percy / percy-puppeteer

Visual testing with Puppeteer and Percy
https://percy.io
MIT License
49 stars 16 forks source link

`ReferenceError: window is not defined` with Jest 28 and jest-puppeteer #451

Closed igorpupkinable closed 2 years ago

igorpupkinable commented 2 years ago

The problem

ReferenceError: window is not defined is thrown after upgrading to Jest 28. Same setup works fine with Jest 27 though.

Environment

Details

ReferenceError: window is not defined error occurs when referencing percySnapshot method after upgrading to Jest 28. Installing JSDOM environment explicitly does not help either. https://jestjs.io/docs/upgrading-to-jest28#jsdom

Debug logs

Test suite failed to run

    The error below may be caused by using the wrong test environment, see https://jestjs.io/docs/configuration#testenvironment-string.
    Consider using the "jsdom" test environment.

    ReferenceError: window is not defined

      at Object.<anonymous> (node_modules/@percy/sdk-utils/dist/bundle.js:347:9)
      at Object.<anonymous> (node_modules/@percy/puppeteer/index.js:1:35)

Code to reproduce issue

Install Jest 28 and configure latest jest-puppeteer as per documentation https://jestjs.io/docs/28.0/puppeteer#use-jest-puppeteer-preset Run simple test:

import percySnapshot from '@percy/puppeteer';

describe('E2E test', () => {
  it('renders', async () => {
    await page.goto(`https://github.com`);

    await percySnapshot(page, 'homepage');
  });
});
igorpupkinable commented 2 years ago

https://github.com/smooth-code/jest-puppeteer/issues/295

wwilsman commented 2 years ago

Hey @igorpupkinable!

It looks like the error stack your provided is referencing the browser bundle of @percy/sdk-utils

node_modules/@percy/sdk-utils/dist/bundle.js

This should only be used in browser environments (where window would be available). For node, it should be using the main/node exports defined by the package.json: https://github.com/percy/cli/blob/master/packages/sdk-utils/package.json#L22-L28

Since you say this is only an issue when upgrading jest, it sounds like they might have changed their bespoke module loader which is not honoring native node module loading.

frixatrix commented 2 years ago

@wwilsman hi, have the same issue with stack:

"engines": { "node": ">=16.16 <17", "npm": ">=8.13.2" }, "dependencies": { "puppeteer": "^14.0.0" }, "devDependencies": { "@percy/cli": "^1.1.3", "@percy/puppeteer": "^2.0.2", "eslint": "^8.15.0", "jest": "^28.1.0", "jest-circus": "^28.1.0", "jest-puppeteer": "^6.1.0", "puppeteer-core": "^14.0.0" }

and it was working fine for last few months until few days ago. There were no changes from my side to the setup or tests.

Trying to find a way how to just return state back to normal and get running tests as before.


UPD: Resolved my issue just with downgrading above list to exact versions without updates, like "@percy/cli": "1.1.3" instead of "@percy/cli": "^1.1.3".

Would be happy to update them once resolved.

Robdel12 commented 2 years ago

Hey @frixatrix! It sounds like Jest is broken in how they're importing modules. In a later version we updated how the export works to match the spec(s): https://github.com/percy/cli/pull/962

github-actions[bot] commented 2 years ago

This issue is stale because it has been open for more than 14 days with no activity. Remove stale label or comment or this will be closed in 14 days.

github-actions[bot] commented 2 years ago

This issue was closed because it has been stalled for 28 days with no activity.