0

I am using mocha to run tests, I want to skip the test programatically. I came across this.skip(). I want to skip the test based on condition but I am seeing error 'TypeError: this.skip is not a function'

Also I am not this.skip(), here this refers to which object?

This is my spec file

test.spec.js

const expect = require('chai').expect;
const Mochaexport = require('../cypress/support/mochaExport');
let mochaExp = new Mochaexport(); 

describe('mocha test', function() {
    it('runs mocha test', function() {
        mochaExp.skipTest("2020");
        expect(true).to.equal(true);
    })
})

mochaExport.js

const { expect } = require("chai");

module.exports = function() {

    this.skipTest = function(Year) {
        if(Year == "2020") {
            this.skip();
        } else {
            expect(Year).to.equal(2021);
        }
    }
}
karansys
  • 2,449
  • 7
  • 40
  • 78
  • Does this answer your question? [How to programmatically skip a test in mocha?](https://stackoverflow.com/questions/32723167/how-to-programmatically-skip-a-test-in-mocha) – Randy Casburn Dec 11 '20 at 19:10
  • When I tried to find the name of this by printing console.log(this.constructor.name); it printed 'Context' so in the exports, I used Context.skip(); but this didn't work TypeError: Suite argument "title" must be a string. Received type "undefined" + expected - actual – karansys Dec 11 '20 at 19:10
  • At least one of those answers will answer your question. – Randy Casburn Dec 11 '20 at 19:10
  • Hi Randy, it would be nice if I get the context.skip() working in the exports, I am wondering why, I will try to reframe my question with new code – karansys Dec 11 '20 at 19:15
  • modules run in their own context. You are only using a reference to that export. So you won't have much luck attempting to manipulate the meaning of _this_ through module gymnastics – Randy Casburn Dec 11 '20 at 19:20

1 Answers1

0

I was looking to run the statement this.skip in export.Found solution how to do this,

I passed this under to the function I was referring in exports and called skip, this is skipped the test.

Here is the code snippet

const expect = require('chai').expect;
const Mochaexport = require('../cypress/support/mochaExport');
let mochaExp = new Mochaexport(); 

describe('mocha test', function() {
    it('runs mocha test', function() {
        mochaExp.skipTest("2020", this);
        expect(true).to.equal(true);
    })
})
const { expect } = require("chai");

module.exports = function() {

    this.skipTest = function(Year, context) {
        if(Year == "2020") {
            context.skip();
        } else {
            expect(Year).to.equal(2021);
        }
    }
}
karansys
  • 2,449
  • 7
  • 40
  • 78