Priya Sivakumar
Priya Sivakumar

Reputation: 31

Is there a way to NOT execute beforeEach function only for certain tests ('it' blocks)

Is there a way to NOT execute beforeEach function only for certain tests ('it' blocks).

I have a protractor clean up file with an aftereach function that is supposed to run after each and every test cases. Is there a way to not execute them for certain test cases('it' blocks).

afterEach(() => {
    common.performance.captureMemory();
    replay.cleanReplay();
    dialog.cleanupDialog(); // Will also close search page source selector dialog if open
    pinboards.closeVizContextIfOpen();
    common.util.dismissNotificationIfPresent();
    formula.closeFormulaEditorIfOpen();
    common.util.ensureLoggedInAsDefaultUser();
    common.util.clearStickerSelection();
    common.util._uniqueNameSeed = -1;
});

I tried this:
global.defaultJasmineAfterEach = () => {
    common.performance.captureMemory();
    replay.cleanReplay();
    dialog.cleanupDialog(); // Will also close search page source selector dialog if open
    pinboards.closeVizContextIfOpen();
    common.util.dismissNotificationIfPresent();
    formula.closeFormulaEditorIfOpen();
    common.util.ensureLoggedInAsDefaultUser();
    common.util.clearStickerSelection();
    common.util._uniqueNameSeed = -1;
};

global.overrideAfterEachOnce = (fn) => {
    global.jasmineAfterEach = fn;
};

global.jasmineAfterEach = defaultJasmineAfterEach;

// This beforeEach block will run after every test, we use this to perform some cleanup that might
// be necessary before next test can run.
afterEach(() => {
    global.jasmineAfterEach();
    if (global.jasmineAfterEach !== global.defaultJasmineAfterEach) {
        global.jasmineAfterEach = global.defaultJasmineAfterEach();
    }
});```

Thanks in advance. :)

Upvotes: 3

Views: 4895

Answers (2)

DublinDev
DublinDev

Reputation: 2348

I think you should check out this answer but the easiest way to me would be as follows. Instead of putting it in the config file make a separate file called afterEach.js

afterEach.js

module.exports = function () {
    afterEach(() => {
        console.log('afterEach complete')
    })
}

For whatever spec which needs the after each use require('./afterAll')(); within the describe block

spec.js

describe('angularjs homepage todo list', function () {
  require('./afterEach')();

  it('should add a todo', function () {
    expect(true).toBe(false);

  });
  it('should add a todo2', function () {
    expect(true).toBe(true);

  });
});

Upvotes: 0

tin
tin

Reputation: 155

beforeEach and afterEach are scoped inside "describe()"

describe('title', () => { 

    beforeEach(...)
    it(...)
    it(...)

})

so maybe you want to scope different "it"s on different "describe"s ?

Upvotes: 5

Related Questions