3

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. :)
  • There are some approaches which may work. Is it the same tests every time you do not want to run it for? How many tests roughly require it? – DublinDev Mar 12 '20 at 10:47
  • About 8 to 10 test cases. All those cases have redirections from a non-angular page to an angular page. – Priya Sivakumar Mar 12 '20 at 10:52

2 Answers2

3

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 ?

tin
  • 137
  • 1
  • 8
  • The "afterEach" is inside a protractor clean up file that will be called automatically for all the test cases. I want a way to ignore those only for 8 test cases. – Priya Sivakumar Mar 12 '20 at 10:50
0

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);

  });
});
DublinDev
  • 2,318
  • 2
  • 8
  • 31