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.

Stubbing methods doesn't work

See original GitHub issue

Current behavior

My component is as follows

import {myMethod} from "./myFile"

function MyComponent(){
   const output = myMethod();

   return <div>{output}</div>    
}

And i am stubbing myMethod because I want it to return a specific value like so

import * as Parent from "./myFile"
cy.stub(Parent, "myMethod", () => "i was stubbed")

cy.visit("/my_component")  # Does not use the stubbed function 

but then i am finding that the component still uses the original function and not the stubbed function, does anyone know what i am doing wrong?

Desired behavior

No response

Test code to reproduce

# Component file
import {myMethod} from "./myFile"

function MyComponent(){
   const output = myMethod();

   return <div>{output}</div>    
}
# Test file
import * as Parent from "./myFile"
cy.stub(Parent, "myMethod", () => "i was stubbed")

cy.visit("/my_component")  # Does not use the stubbed function 

Cypress Version

8.7.0

Other

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
web-bertcommented, Oct 29, 2021

We used CRA to create our project and didn’t want to eject. I had a similar issue and I fixed it when I found a fix from another issue, I also wanted to add code coverage output so my config also includes that - you can remove if it’s not required in your setup

/plugins/index.js

const { startDevServer } = require('@cypress/webpack-dev-server');
const findReactScriptsWebpackConfig = require('@cypress/react/plugins/react-scripts/findReactScriptsWebpackConfig');

// taken from https://github.com/cypress-io/code-coverage/issues/461#issuecomment-859292331
function customDevServer(
	on,
	config,
	{ webpackConfigPath } = {
		webpackConfigPath: 'react-scripts/config/webpack.config',
	}
) {
	on('dev-server:start', async (options) => {
		const webpackConfig = findReactScriptsWebpackConfig(config, {
			webpackConfigPath,
		});
		const rules = webpackConfig.module.rules.find((rule) => !!rule.oneOf).oneOf;
		const babelRule = rules.find((rule) => /babel-loader/.test(rule.loader));
		babelRule.options.plugins.push(require.resolve('babel-plugin-istanbul'), [
			'@babel/plugin-transform-modules-commonjs',
			{
				loose: true,
			},
		]);
		return startDevServer({
			options,
			webpackConfig,
		});
	});

	config.env.reactDevtools = true;

	return config;
}

module.exports = (on, config) => {
	if (config.testingType === 'component') {
		customDevServer(on, config);
	}

	require('@cypress/code-coverage/task')(on, config);

	return config;
};

The main fix is adding this to your babel config:

[
	'@babel/plugin-transform-modules-commonjs',
	{
		loose: true,
	},
]
1reaction
muratkeremozcancommented, Mar 19, 2022

Stubbing modules isn’t working in the component runner. The same thing is ok in the e2e runner. Wait for Cy 10 or work around it like the above…

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mockito stubbing is not working when service class method is ...
I tried to stub a set of data suppose to be return by a specific service method to a method present in controller...
Read more >
Stubbing and Mocking with Mockito and JUnit - Semaphore CI
Learn how to create true unit tests by mocking all external dependencies in your JUnit classes with the help of Mockito.
Read more >
`void` methods | Migrating from Mockito | MockK Guidebook
Mockito's when method doesn't work with void methods. To create a stub that doesn't return anything, the doNothing method is used.
Read more >
A Unit Testing Practitioner's Guide to Everyday Mockito - Toptal
After the test, you can query the mock to see what specific methods were ... AClass mock = mock(AClass.class); //This doesn't work. when(mock.call("a", ......
Read more >
Mock / stub private method does not work, weird
misusing.MissingMethodInvocationException : when() requires an argument which has to be 'a method call on a mock'. For example: when(mock.
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