question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

mockRequest results in TypeError: Cannot read property ‘apply’ of undefined

See original GitHub issue

What are you trying to achieve?

I’m trying to write a test with mockRequest involved. I just set up a small project and wrote a basic test like follows:

Feature('Search');

const response = [{"phrase": "test"}, {"phrase": "hello"}, {"phrase": "world"}, {"phrase": "duckduckgo"}];

Scenario('search in Duckduckgo', ({I}) => {
    I.amOnPage('https://duckduckgo.com/');
    I.mockRequest('GET', 'https://duckduckgo.com/ac/*', response);
    I.fillField('#search_form_input_homepage', 'hello');
});

What do you get instead?

when I run yarn codeceptjs run --steps --debug --verbose I’m getting the following:

❯ yarn codeceptjs run --steps --debug --verbose
CodeceptJS v3.0.2
Using test root "/home/sergey/dev/codecept-example"
Helpers: Puppeteer, MockRequestHelper
Plugins: screenshotOnFail, retryFailedStep, tryTo

Search --
    [1]  Starting recording promises
  search in Duckduckgo
    I am on page "https://duckduckgo.com/"
    › [Url] https://duckduckgo.com/
    I mock request "GET", "https://duckduckgo.com/ac/*", [{"phrase":"test"},{"phrase":"hello"},{"phras…
    [1] Retrying... Attempt #2
    [1] Retrying... Attempt #3
    [1] Retrying... Attempt #4
    [1] Retrying... Attempt #5
    [1] Retrying... Attempt #6
    [1] Error | TypeError: Cannot read property 'apply' of undefined
    [1] Error | TypeError: Cannot read property 'apply' of undefined
    [1] Error | TypeError: Cannot read property 'apply' of undefined
    [1] Starting <teardown> session
    [1] <teardown> Stopping recording promises
 › <screenshotOnFail> Test failed, try to save a screenshot
 › Screenshot is saving to /home/sergey/dev/codecept-example/output/search_in_Duckduckgo.failed.png
  ✖ FAILED in 5857ms

    [2]  Starting recording promises
    Polly was not disconnected, Puppeteer is already closed

-- FAILURES:

  1) Search
       search in Duckduckgo:
     Cannot read property 'apply' of undefined

      at PuppeteerCodeceptJSAdapter._callListenersWith (node_modules/@codeceptjs/mock-request/node_modules/@pollyjs/adapter-puppeteer/dist/cjs/pollyjs-adapter-puppeteer.js:2418:28)
      at PuppeteerCodeceptJSAdapter.attachToPageEvents (node_modules/@codeceptjs/mock-request/node_modules/@pollyjs/adapter-puppeteer/dist/cjs/pollyjs-adapter-puppeteer.js:2328:10)
      at PuppeteerCodeceptJSAdapter.onConnect (node_modules/@codeceptjs/mock-request/node_modules/@pollyjs/adapter-puppeteer/dist/cjs/pollyjs-adapter-puppeteer.js:2233:10)
      at PuppeteerCodeceptJSAdapter.connect (node_modules/@codeceptjs/mock-request/node_modules/@pollyjs/adapter/dist/cjs/pollyjs-adapter.js:1130:12)
      at Polly.connectTo (node_modules/@codeceptjs/mock-request/node_modules/@pollyjs/core/dist/cjs/pollyjs-core.js:6430:13)
      at /home/sergey/dev/codecept-example/node_modules/@codeceptjs/mock-request/node_modules/@pollyjs/core/dist/cjs/pollyjs-core.js:6315:50
      at Array.forEach (<anonymous>)
      at Polly.configure (node_modules/@codeceptjs/mock-request/node_modules/@pollyjs/core/dist/cjs/pollyjs-core.js:6315:26)
      at new Polly (node_modules/@codeceptjs/mock-request/node_modules/@pollyjs/core/dist/cjs/pollyjs-core.js:6216:10)
      at PuppeteerConnector.connect (node_modules/@codeceptjs/mock-request/connector/Puppeteer.js:43:18)

Details

  • NodeJS Version: 15.2.1

  • Operating System: Arch Linux

  • My package.json:

{
  "name": "codecept-example",
  "version": "1.0.0",
  "description": "A test automation project using CodeceptJS",
  "main": "index.js",
  "scripts": {
    "test": "yarn codeceptjs run --steps",
    "debug": "yarn codeceptjs run --debug --verbose"
  },
  "license": "ISC",
  "dependencies": {
    "codeceptjs": "^3.0.2",
    "puppeteer": "^5.5.0"
  },
  "devDependencies": {
    "@codeceptjs/mock-request": "^0.3.0",
    "@pollyjs/adapter-puppeteer": "^5.0.0",
    "@pollyjs/core": "^5.0.0"
  }
}
  • Configuration file:
const { setHeadlessWhen } = require('@codeceptjs/configure');

setHeadlessWhen(process.env.HEADLESS);

exports.config = {
  tests: './tests/*_test.js',
  output: './output',
  helpers: {
    Puppeteer: {
      show: true,
      windowSize: '1200x900'
    },
    MockRequestHelper: {
      require: '@codeceptjs/mock-request',
    }
  },
  include: {
    I: './steps_file.js'
  },
  bootstrap: null,
  mocha: {},
  name: 'codecept-example',
  plugins: {
    pauseOnFail: {},
    retryFailedStep: {
      enabled: true
    },
    tryTo: {
      enabled: true
    },
    screenshotOnFail: {
      enabled: true
    }
  }
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
serzhshakurcommented, Dec 7, 2020

It turned out this is caused by the problem at pollyjs side https://github.com/Netflix/pollyjs/issues/345

0reactions
IanHong66commented, Dec 17, 2020

@serzhshakur thank you for pointing this out again. I got what you mean now. I used npm-force-resolution and replicated what you did. Now the error is gone. But pollyjs seems not starting in browser either. When I do >npm ls, it says the version is invalid, is this normal?

PS C:\Users\ian.hong\automation\UIAutomation\UIAutomationSuite> npm ls @pollyjs/core
UIAutomationSuite@1.0.0 C:\Users\ian.hong\automation\UIAutomation\UIAutomationSuite
`-- @codeceptjs/mock-request@0.3.0
  `-- @pollyjs/core@5.1.0  invalid

npm ERR! invalid: @pollyjs/core@5.1.0 C:\Users\ian.hong\automation\UIAutomation\UIAutomationSuite\node_modules\@codeceptjs\mock-request\node_modules\@pollyjs\core
PS C:\Users\ian.hong\automation\UIAutomation\UIAutomationSuite>  npm ls @pollyjs/adapter-puppeteer
UIAutomationSuite@1.0.0 C:\Users\ian.hong\automation\UIAutomation\UIAutomationSuite
`-- @codeceptjs/mock-request@0.3.0
  `-- @pollyjs/adapter-puppeteer@5.1.0  invalid

my config file is as below now:

  "resolutions": {
    "@pollyjs/adapter-puppeteer": "^5.1.0",
    "@pollyjs/core": "^5.1.0"
  },
  "devDependencies": {
    "@codeceptjs/mock-request": "^0.3.0",
    "npm-force-resolutions": "0.0.3",
    "puppeteer": "^5.5.0",
  },

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

mockRequest results in TypeError: Cannot read property ...
Hello, I'm trying to write a test with mockRequest involved. ... mockRequest results in TypeError: Cannot read property 'apply' of undefined.
Read more >
Cannot read property 'apply' of undefined in NodeJS using ...
I am new to jest unit testing as well as NodeJS and i'm trying to test my API using the following code. const...
Read more >
Cannot read property 'apply' of undefined" - Forums - Liferay
In the console, persistent errors : "Uncaught TypeError: Cannot read property 'apply' of undefined" In what may be the reason?
Read more >
2013 - Uncaught TypeError: Cannot read property 'apply' of ...
I can see following issues in the code: In the line var listEnumerator = lists.getEnumerator();. the variable 'lists' is undefined, ...
Read more >
nock - npm
If no request headers are specified for mocking then Nock will automatically ... The body will be either a buffer, a stream, or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found