microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
67.03k stars 3.68k forks source link

[Bug]: this becomes undefined when using require #32987

Closed LisandroQA closed 1 month ago

LisandroQA commented 1 month ago

Version

1.47.2

Steps to reproduce

  1. Clone my repo from https://github.com/LisandroQA/class-issue
  2. do a npm install
  3. execute the dummy test (or npx playwright test from the command line)

Expected behavior

The test should pass and display the following in the console:

Running 1 test using 1 worker
[chromium] › dummy.feature.ts:3:5 › Test
<ref *1> Object [global] {
  global: [Circular *1],
  clearImmediate: [Function: clearImmediate],
  setImmediate: [Function: setImmediate] {
    [Symbol(nodejs.util.promisify.custom)]: [Getter]
  },
  clearInterval: [Function: clearInterval],
  clearTimeout: [Function: clearTimeout],
  setInterval: [Function: setInterval],
  setTimeout: [Function: setTimeout] {
    [Symbol(nodejs.util.promisify.custom)]: [Getter]
  },
  queueMicrotask: [Function: queueMicrotask],
  structuredClone: [Getter/Setter],
  atob: [Getter/Setter],
  btoa: [Getter/Setter],
  performance: [Getter/Setter],
  fetch: [AsyncFunction: fetch],
  crypto: [Getter],
  _playwrightInstance: <ref *2> Playwright {
    _events: [Object: null prototype] {},
    _eventsCount: 0,
    _maxListeners: 0,
    _connection: Connection {
      _events: [Object: null prototype],
      _eventsCount: 1,
      _maxListeners: undefined,
      _objects: [Map],
      onmessage: [Function (anonymous)],
      _lastId: 1,
      _callbacks: [Map],
      _rootObject: [Root],
      _closedError: undefined,
      _isRemote: false,
      _localUtils: [LocalUtils],
      _rawBuffers: true,
      toImpl: [Function (anonymous)],
      _tracingCount: 0,
      _instrumentation: {},
      [Symbol(kCapture)]: false
    },
    _parent: Root {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: 0,
      _connection: [Connection],
      _parent: undefined,
      _objects: [Map],
      _type: 'Root',
      _guid: '',
      _channel: [EventEmitter],
      _initializer: {},
      _logger: undefined,
      _instrumentation: undefined,
      _eventToSubscriptionMapping: Map(0) {},
      _wasCollected: false,
      [Symbol(kCapture)]: false
    },
    _objects: Map(0) {},
    _type: 'Playwright',
    _guid: 'Playwright',
    _channel: EventEmitter {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      _object: [Circular *2],
      [Symbol(kCapture)]: false
    },
    _initializer: {
      chromium: [EventEmitter],
      firefox: [EventEmitter],
      webkit: [EventEmitter],
      android: [EventEmitter],
      electron: [EventEmitter],
      utils: [EventEmitter],
      selectors: [EventEmitter]
    },
    _logger: undefined,
    _instrumentation: {},
    _eventToSubscriptionMapping: Map(0) {},
    _wasCollected: false,
    _android: Android {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: 0,
      _connection: [Connection],
      _parent: [Root],
      _objects: Map(0) {},
      _type: 'Android',
      _guid: 'android@371758933b58b86a3652c6225a195d65',
      _channel: [EventEmitter],
      _initializer: {},
      _logger: undefined,
      _instrumentation: {},
      _eventToSubscriptionMapping: Map(0) {},
      _wasCollected: false,
      _timeoutSettings: [TimeoutSettings],
      _serverLauncher: AndroidServerLauncherImpl {},
      [Symbol(kCapture)]: false
    },
    _electron: Electron {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: 0,
      _connection: [Connection],
      _parent: [Root],
      _objects: Map(0) {},
      _type: 'Electron',
      _guid: 'electron@8d9df8111dc067190d51d2120772b4c0',
      _channel: [EventEmitter],
      _initializer: {},
      _logger: undefined,
      _instrumentation: {},
      _eventToSubscriptionMapping: Map(0) {},
      _wasCollected: false,
      [Symbol(kCapture)]: false
    },
    chromium: BrowserType {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: 0,
      _connection: [Connection],
      _parent: [Root],
      _objects: Map(0) {},
      _type: 'BrowserType',
      _guid: 'browser-type@aa4447991f417dd6066a146899c8430c',
      _channel: [EventEmitter],
      _initializer: [Object],
      _logger: undefined,
      _instrumentation: {},
      _eventToSubscriptionMapping: Map(0) {},
      _wasCollected: false,
      _serverLauncher: [BrowserServerLauncherImpl],
      _contexts: Set(0) {},
      _playwright: [Circular *2],
      _defaultContextOptions: [Object],
      _defaultContextTimeout: 0,
      _defaultContextNavigationTimeout: 0,
      _defaultLaunchOptions: [Object],
      [Symbol(kCapture)]: false
    },
    firefox: BrowserType {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: 0,
      _connection: [Connection],
      _parent: [Root],
      _objects: Map(0) {},
      _type: 'BrowserType',
      _guid: 'browser-type@9eab1be305d4a3ea0a23c047a68b6ac3',
      _channel: [EventEmitter],
      _initializer: [Object],
      _logger: undefined,
      _instrumentation: {},
      _eventToSubscriptionMapping: Map(0) {},
      _wasCollected: false,
      _serverLauncher: [BrowserServerLauncherImpl],
      _contexts: Set(0) {},
      _playwright: [Circular *2],
      _defaultContextOptions: [Object],
      _defaultContextTimeout: 0,
      _defaultContextNavigationTimeout: 0,
      _defaultLaunchOptions: [Object],
      [Symbol(kCapture)]: false
    },
    webkit: BrowserType {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: 0,
      _connection: [Connection],
      _parent: [Root],
      _objects: Map(0) {},
      _type: 'BrowserType',
      _guid: 'browser-type@370336557c8ea6965067bed645e747b3',
      _channel: [EventEmitter],
      _initializer: [Object],
      _logger: undefined,
      _instrumentation: {},
      _eventToSubscriptionMapping: Map(0) {},
      _wasCollected: false,
      _serverLauncher: [BrowserServerLauncherImpl],
      _contexts: Set(0) {},
      _playwright: [Circular *2],
      _defaultContextOptions: [Object],
      _defaultContextTimeout: 0,
      _defaultContextNavigationTimeout: 0,
      _defaultLaunchOptions: [Object],
      [Symbol(kCapture)]: false
    },
    devices: {
      'Blackberry PlayBook': [Object],
      'Blackberry PlayBook landscape': [Object],
      'BlackBerry Z30': [Object],
      'BlackBerry Z30 landscape': [Object],
      'Galaxy Note 3': [Object],
      'Galaxy Note 3 landscape': [Object],
      'Galaxy Note II': [Object],
      'Galaxy Note II landscape': [Object],
      'Galaxy S III': [Object],
      'Galaxy S III landscape': [Object],
      'Galaxy S5': [Object],
      'Galaxy S5 landscape': [Object],
      'Galaxy S8': [Object],
      'Galaxy S8 landscape': [Object],
      'Galaxy S9+': [Object],
      'Galaxy S9+ landscape': [Object],
      'Galaxy Tab S4': [Object],
      'Galaxy Tab S4 landscape': [Object],
      'iPad (gen 5)': [Object],
      'iPad (gen 5) landscape': [Object],
      'iPad (gen 6)': [Object],
      'iPad (gen 6) landscape': [Object],
      'iPad (gen 7)': [Object],
      'iPad (gen 7) landscape': [Object],
      'iPad Mini': [Object],
      'iPad Mini landscape': [Object],
      'iPad Pro 11': [Object],
      'iPad Pro 11 landscape': [Object],
      'iPhone 6': [Object],
      'iPhone 6 landscape': [Object],
      'iPhone 6 Plus': [Object],
      'iPhone 6 Plus landscape': [Object],
      'iPhone 7': [Object],
      'iPhone 7 landscape': [Object],
      'iPhone 7 Plus': [Object],
      'iPhone 7 Plus landscape': [Object],
      'iPhone 8': [Object],
      'iPhone 8 landscape': [Object],
      'iPhone 8 Plus': [Object],
      'iPhone 8 Plus landscape': [Object],
      'iPhone SE': [Object],
      'iPhone SE landscape': [Object],
      'iPhone X': [Object],
      'iPhone X landscape': [Object],
      'iPhone XR': [Object],
      'iPhone XR landscape': [Object],
      'iPhone 11': [Object],
      'iPhone 11 landscape': [Object],
      'iPhone 11 Pro': [Object],
      'iPhone 11 Pro landscape': [Object],
      'iPhone 11 Pro Max': [Object],
      'iPhone 11 Pro Max landscape': [Object],
      'iPhone 12': [Object],
      'iPhone 12 landscape': [Object],
      'iPhone 12 Pro': [Object],
      'iPhone 12 Pro landscape': [Object],
      'iPhone 12 Pro Max': [Object],
      'iPhone 12 Pro Max landscape': [Object],
      'iPhone 12 Mini': [Object],
      'iPhone 12 Mini landscape': [Object],
      'iPhone 13': [Object],
      'iPhone 13 landscape': [Object],
      'iPhone 13 Pro': [Object],
      'iPhone 13 Pro landscape': [Object],
      'iPhone 13 Pro Max': [Object],
      'iPhone 13 Pro Max landscape': [Object],
      'iPhone 13 Mini': [Object],
      'iPhone 13 Mini landscape': [Object],
      'iPhone 14': [Object],
      'iPhone 14 landscape': [Object],
      'iPhone 14 Plus': [Object],
      'iPhone 14 Plus landscape': [Object],
      'iPhone 14 Pro': [Object],
      'iPhone 14 Pro landscape': [Object],
      'iPhone 14 Pro Max': [Object],
      'iPhone 14 Pro Max landscape': [Object],
      'iPhone 15': [Object],
      'iPhone 15 landscape': [Object],
      'iPhone 15 Plus': [Object],
      'iPhone 15 Plus landscape': [Object],
      'iPhone 15 Pro': [Object],
      'iPhone 15 Pro landscape': [Object],
      'iPhone 15 Pro Max': [Object],
      'iPhone 15 Pro Max landscape': [Object],
      'Kindle Fire HDX': [Object],
      'Kindle Fire HDX landscape': [Object],
      'LG Optimus L70': [Object],
      'LG Optimus L70 landscape': [Object],
      'Microsoft Lumia 550': [Object],
      'Microsoft Lumia 550 landscape': [Object],
      'Microsoft Lumia 950': [Object],
      'Microsoft Lumia 950 landscape': [Object],
      'Nexus 10': [Object],
      'Nexus 10 landscape': [Object],
      'Nexus 4': [Object],
      'Nexus 4 landscape': [Object],
      'Nexus 5': [Object],
      'Nexus 5 landscape': [Object],
      'Nexus 5X': [Object],
      'Nexus 5X landscape': [Object],
      'Nexus 6': [Object],
      'Nexus 6 landscape': [Object],
      'Nexus 6P': [Object],
      'Nexus 6P landscape': [Object],
      'Nexus 7': [Object],
      'Nexus 7 landscape': [Object],
      'Nokia Lumia 520': [Object],
      'Nokia Lumia 520 landscape': [Object],
      'Nokia N9': [Object],
      'Nokia N9 landscape': [Object],
      'Pixel 2': [Object],
      'Pixel 2 landscape': [Object],
      'Pixel 2 XL': [Object],
      'Pixel 2 XL landscape': [Object],
      'Pixel 3': [Object],
      'Pixel 3 landscape': [Object],
      'Pixel 4': [Object],
      'Pixel 4 landscape': [Object],
      'Pixel 4a (5G)': [Object],
      'Pixel 4a (5G) landscape': [Object],
      'Pixel 5': [Object],
      'Pixel 5 landscape': [Object],
      'Pixel 7': [Object],
      'Pixel 7 landscape': [Object],
      'Moto G4': [Object],
      'Moto G4 landscape': [Object],
      'Desktop Chrome HiDPI': [Object],
      'Desktop Edge HiDPI': [Object],
      'Desktop Firefox HiDPI': [Object],
      'Desktop Safari': [Object],
      'Desktop Chrome': [Object],
      'Desktop Edge': [Object],
      'Desktop Firefox': [Object]
    },
    selectors: Selectors { _channels: [Set], _registrations: [] },
    request: APIRequest {
      _playwright: [Circular *2],
      _contexts: Set(0) {},
      _defaultContextOptions: [Object]
    },
    errors: { TimeoutError: [class TimeoutError extends Error] },
    _toImpl: [Function (anonymous)],
    [Symbol(kCapture)]: false
  }
}
{ BaseGenerator: [Function: Class] { extend: [Function (anonymous)] } }
  1 passed (289ms)

Actual behavior

When using playwright 1.47.x the value of this is undefined while in previous version it has a lot of information.

I don't know if maybe it is something related with required 🤔

I tried to debug it, but I couldn't find much. So this is the console error so far:

 1) [chromium] › dummy.feature.ts:3:5 › Test ──────────────────────────────────────────────────────

    TypeError: Cannot set properties of undefined (setting 'Class')

       at ../legacy_server/fixtures/factory/_class.js:13

      11 |   // The base Class implementation (does nothing)
      12 |   console.log(this);
    > 13 |   this.Class = function () {};
         |              ^
      14 |
      15 |   // Create a new Class that inherits from this class
      16 |   Class.extend = function (prop) {

        at /Users/<user>/Repositories/class-issue/legacy_server/fixtures/factory/_class.js:13:14
        at Object.<anonymous> (/Users/<user>//Repositories/class-issue/legacy_server/fixtures/factory/_class.js:61:3)
        at Object.<anonymous> (/Users/<user>//Repositories/class-issue/legacy_server/fixtures/factory/fixture_generator.js:3:19)
        at Object.<anonymous> (/Users/<user>//Repositories/class-issue/legacy_server/fixtures/index.js:3:26)
        at Object.Fixture (/Users/<user>//Repositories/class-issue/legacy_server/app.js:10:18)
        at Object.<anonymous> (/Users/<user>//Repositories/class-issue/legacy_server/app.js:12:17)
        at /Users/<user>//Repositories/class-issue/tests/dummy.feature.ts:4:15

Thank you so much!

Additional context

This is not happening in versions lower than 1.47 (1.46.1 is working fine).

Environment

System:
    OS: macOS 14.3
    CPU: (10) arm64 Apple M1 Pro
    Memory: 106.02 MB / 16.00 GB
  Binaries:
    Node: 20.9.0 - ~/.nvm/versions/node/v20.9.0/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 10.7.0 - ~/.nvm/versions/node/v20.9.0/bin/npm
  IDEs:
    VSCode: 1.93.1 - /usr/local/bin/code
  Languages:
    Bash: 3.2.57 - /bin/bash
  npmPackages:
    @playwright/test: 1.47 => 1.47.2
pavelfeldman commented 1 month ago

I'm not sure what you are trying to do and what your expectations are. I see the same error, it looks reasonable, you can't set properties of undefined.

pavelfeldman commented 1 month ago

In either case, does not look related to Playwright, so closing.

LisandroQA commented 1 month ago

It is related to playwright, until version 1.47 (you can test the code with 1.46 for example) it was working fine and now with 1.47.x it is giving undefined, nothing in our code changed.

For more context, what I'm doing is executing some code to populate the db for the tests.