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.

Cypress 10.7x - TypeError: addContext is not a function

See original GitHub issue

Describe the bug Since the latest Cypress version the ‘after:run’ API configuration is changed and the mochawesome addContext configureation is now part of the cypress config file and not in the support file anymore. So I’ve tried to refactor the configuration for I can attach the failed tests screenshoots but it yell about

An error was thrown in your plugins file while executing the handler for the after:spec event.

The error we received was:

TypeError: addContext is not a function
...

Code Reproduce

// cypress/cypress.config.ts
/// <reference types="cypress" />
/**
 * @type {Cypress.PluginConfig}
 */

import { defineConfig } from 'cypress';
// @ts-ignore
import * as addContext from './node_modules/mochawesome/src/addContext';
const fs = require('fs');
const ansi = require('ansi-colors');
const webpackPreprocessor = require('@cypress/webpack-preprocessor');
const { execSync } = require('child_process');


export default defineConfig({
	env: {
		SMTP_HOST: process.env.SMTP_HOST,
	},
	e2e: {
		supportFile: './../my-shared-cy-infra/cypress/support/e2e.ts',
		downloadsFolder: './../my-shared-cy-infra/cypress/downloads',
		chromeWebSecurity: false,
		experimentalSourceRewriting: false,
		experimentalModifyObstructiveThirdPartyCode: true,
		numTestsKeptInMemory: 1,
		defaultCommandTimeout: 30000,
		requestTimeout: 30000,
		responseTimeout: 30000,
		taskTimeout: 30000,
		pageLoadTimeout: 30000,
		screenshotsFolder: './../my-shared-cy-infra/mochawesome-report/assets',
		screenshotOnRunFailure: true,
		reporter: './../my-shared-cy-infra/node_modules/mochawesome',
		reporterOptions: {
			reportDir: './../my-shared-cy-infra/cypress/report',
			code: false,
			charts: true,
			overwrite: false,
			html: false,
			json: true,
			embeddedScreenshots: true,
		},
		video: false,
		viewportHeight: 1080,
		viewportWidth: 1920,
		waitForAnimations: true,
		setupNodeEvents(on, config) {
			// @ts-ignore
			on('after:spec', (spec: any, results: any) => {
					const screenshot = `assets/${spec.name}/${results.tests[0].title[0]} -- ${results.tests[0].title[1]} (failed).png`;
				console.log('log', '>>>>>>> ' + screenshot);

				addContext({ spec }, screenshot);
			});

			on('task', {
				log(message) {
					console.log(ansi.yellow(message));
					return true;
				},
			});

			on('task', {
				err(message) {
					console.log(ansi.red(message));
					return true;
				},
			});
			return config;
		},
	},
});

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add screenshots to help explain your problem.

Environment (please complete the following information):

  • Platform: mac os 12
  • mochawesome version: 7.3.1
  • cypress version 10.7.0
  • Node version 14.19.1

Additional context Add any other context about the problem here.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
adamgrubercommented, Sep 22, 2022

I’m not sure this line is correct:

import * as addContext from './node_modules/mochawesome/src/addContext';

Have you tried replacing with this?

const addContext = require('mochawesome/addContext');
0reactions
idanElitzurcommented, Sep 23, 2022

OK Thanks a lot anyway, Love your reporter! Shana tova 🍎 🍯 @adamgruber

I’ll close this one.

Read more comments on GitHub >

github_iconTop Results From Across the Web

adamgruber - Github Plus
Cypress 10.7x - TypeError: addContext is not a function ... I'm not sure this line is correct: import * as addContext from './node_modules/mochawesome/src/ ......
Read more >
mochawesome - githubmemory
Can not marge the mocha report which genrated in parallel test. RaviThippana ... Cypress 10.7x - TypeError: addContext is not a function. ittaibaratz....
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