Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] programatically mark a test to be skipped #704

Closed
onthespotqa opened this issue May 5, 2022 · 5 comments
Closed

[question] programatically mark a test to be skipped #704

onthespotqa opened this issue May 5, 2022 · 5 comments

Comments

@onthespotqa
Copy link

onthespotqa commented May 5, 2022

We have a plugin that will skip tests based on a dynmically generated json fille.

So in the previous implementation and cypress 9+ the following code would just skip the test.

beforeEach(function () `{`
  mochaContext = this;
});

export function cucumberBefore() {
  const quarantineManifest = Cypress.env('cypressQuarantine')
  if (quarantineIsEnabled) {
      const testState = window.testState;
      const feature = testState.feature.name;
      const scenario = testState.currentScenario.name;
    skipTest(feature, scenario, quarantineManifest)
  }
}

function skipTest(context: string, testName: string, quarantineManifest?: [{ testcase: string, context: string }]) {
  const shouldSkip = !!quarantineManifest?.find(
    t => t?.testcase === testName && t?.context?.includes(context)
  );
  if (shouldSkip) {
    mochaContext.skip();
  }
}

How it's called in the suite

Before(function() {
  quarantine.cucumberBefore();
});

I think previously it was always doing a 'skip sync; abort execution' but cypress/cucumber-preprocessor would just skip the test. During the upgrade process this is now marking the skipped test as a failure.

So my question is, is there an alternate way we should be programmatically skipping tests. I was trying to see if there was some way i could add the ignore tag during this hook to the test and didn't see anything.

I know cypress fixed the sync skip isssue in version 9.2. and it was working on the previous version of cypress-cucumber-preprocessor we were on 4.1.2.

Versions

  • Cypress version: 9.5.4
  • Preprocessor version: 9.1.2
  • Node version: 16.14.2
@badeball
Copy link
Owner

badeball commented May 5, 2022

Hey @onthespotqa, I've fixed this with v9.1.3.

@badeball badeball closed this as completed May 5, 2022
@onthespotqa
Copy link
Author

@badeball thanks. will install and let you know if there's an issue still

@onthespotqa
Copy link
Author

@badeball so this works when i added the code inline, but not when its a package in a mono-repo. I've created a repo for reproducing the issue, that has a basic test that should be skipped. both the test suite and the yarn workspace package are on 9.1.3 and the latest cypress https://github.com/onthespotqa/cucumber-issue-repo

@badeball
Copy link
Owner

So, it seems to work well when invoking .skip() on the this-context of Before(..), like so.

Before(function () {
  this.skip();
})

However, trying to save the context from beforeEach and invoke on that, that is what fails.

let context;

beforeEach(function () {
  context = this;
});

Before(function () {
  context.skip();
})

Why? I don't know, but I don't believe that it has got to do anything with the preprocessor. Saving contexts simply isn't documented and I expect all sorts of trouble to arise when you do it. Instead, I would do something like shown below.

diff --git a/cucumber-tests/cypress/integration/common/quarantine.ts b/cucumber-tests/cypress/integration/common/quarantine.ts
index 0666895..2dfd74d 100644
--- a/cucumber-tests/cypress/integration/common/quarantine.ts
+++ b/cucumber-tests/cypress/integration/common/quarantine.ts
@@ -2,7 +2,7 @@ import {Given, Before} from "@badeball/cypress-cucumber-preprocessor"
 import * as quarantine from 'cypress-quarantine';
 
 Before(function () {
-  quarantine.cucumberBefore();
+  quarantine.cucumberBefore(this);
 })
 
 Given(`A Step`, () =>{
diff --git a/cypress-quarantine/index.ts b/cypress-quarantine/index.ts
index 4207416..75e8968 100644
--- a/cypress-quarantine/index.ts
+++ b/cypress-quarantine/index.ts
@@ -1,38 +1,23 @@
 let quarantineIsEnabled = true; // Toggle in order to stop or resume skipping tests in CI
 let shouldSkip = false;
 
-var mochaContext;
-
 // declare global {
 //   interface Window {
 //     testState: { gherkinDocument: IGherkinDocument; pickles: IPickle[]; pickle: IPickle; };
 //   }
 // }
 
-beforeEach(function () {
-  mochaContext = this;
-});
-
-export function mochaBeforeEach() {
-  const quarantineManifest = Cypress.env('cypressQuarantine')
-  if (quarantineIsEnabled) {
-      const contextName = mochaContext.currentTest.parent.title;
-      const testName = mochaContext.currentTest.title;
-      skipTest(contextName, testName, quarantineManifest)
-  }
-}
-
-export function cucumberBefore() {
+export function cucumberBefore(mochaContext) {
   const quarantineManifest = Cypress.env('cypressQuarantine')
   if (quarantineIsEnabled) {
       const testState = window.testState;
       const feature = testState.gherkinDocument.feature.name;
       const scenario = testState.pickle.name;
-    skipTest(feature, scenario, quarantineManifest)
+    skipTest(mochaContext, feature, scenario, quarantineManifest)
   }
 }
 
-function skipTest(context: string, testName: string, quarantineManifest?: [{ testcase: string, context: string }]) {
+function skipTest(mochaContext, context: string, testName: string, quarantineManifest?: [{ testcase: string, context: string }]) {
   const shouldSkip = !!quarantineManifest?.find(
     t => t?.testcase === testName && t?.context?.includes(context)
   );

@onthespotqa
Copy link
Author

passing in the mocha context to the method works. thanks for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants