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.

unable to add functions to `this` (Context)

See original GitHub issue

Current behavior

Adding functions to this (which is a Mocha Context) yields an error.

Desired behavior

I want to add functions to have them on this in tests, so I can do e.g.

describe('my spec', () => {
  it('should work', function () {
    this.sayHelloWorld();
  })
})

Test code to reproduce

In an empty folder I call

yarn add cypress
yarn cypress open

so that default config files are created. It also creates a sample test in cypress/e2e/spec.cy.js.

I can run the sample test without problem. But if I add

import { Context } from "mocha";

to cypress/support/e2e.js I get

Error: Webpack Compilation Error
./cypress/support/e2e.js
Module not found: Error: Can't resolve 'mocha' in '...\support'
resolve 'mocha' in '...\support'
  Parsed request is a module

So I installed Mocha, the same version which is in devDependencies of Cypress (see package.json):

yarn add mocha@3.5.3

My package.json now looks like this:

{
  "dependencies": {
    "cypress": "^10.4.0",
    "mocha": "3.5.3"
  }
}

Now that import line passes. But this fails:

import { Context } from "mocha";

Context.prototype.sayHelloWorld = () => console.log("hello world");

Error:

> Cannot read properties of undefined (reading 'prototype')

Why is that? In comparison adding something to String.prototype works.

And it also works in Mocha without Cypress. In another empty folder:

yarn add mocha@3.5.3

And in test/test.js:

const { Context } = require("mocha");
it("works", function () {
  Context.prototype.sayHelloWorld = () => console.log("hello world");
  this.sayHelloWorld();
});

Now yarn mocha works (says hello world).

Why does this work in Mocha but not in Cypress?

Cypress Version

10.4.0

Other

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
iomedico-beyercommented, Aug 12, 2022

But simply adding my stuff to this in before did the trick. Thank you very much! 💚 Looks like I still have a lot to learn about JavaScript and TypeScript …

I found another solution:

Mocha.Context.prototype.sayHelloWorld = function () {
    cy.log('hello world');
};

(no import)

1reaction
rockindahizzycommented, Aug 11, 2022

Hey @iomedico-beyer, thanks for the additional info!

I made a small repo showcasing how you might accomplish this pattern here. The specific changes I made can be seen in this commit.

Let me know if that helps!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to create an object of type 'Context' - Stack Overflow
Function.Infrastructure. When I try to generate a migration, I get the following error: Unable to create an object of type 'Context'.
Read more >
Error: Function definition not supported in this context. Create ...
I was trying to create a simple function on matlab, I already have a file named "AddOne.m", so it should work, however at...
Read more >
this - JavaScript | MDN
In most cases, the value of this is determined by how a function is called (runtime binding). It can't be set by assignment...
Read more >
Configure function app settings in Azure Functions
To add a setting in the portal, select New application setting and add the new key-value pair. Function app settings in the Azure...
Read more >
Fatal error: Can't use function return value in write context
It was performed by IWP on all of my sites. The WordPress requirements for this plugin are listed as anything 3.8 to 5.3....
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