0

I'm encountering a situation when I use the beforeEach async and I have a couple of tests in my spec. I see that the beforeEach is getting called for each test that runs instead of only once before all the tests - what am I doing wrong? Here's my code:

describe("desc1", function () {

   beforeEach((done) => {
   // some logic
        done();

    });

}
describe("desc2",() => {

    it("has a value", () => {
        expect().toBe('');
    });
    it("has a value", () => {
        expect().toBe('');
    });
    it("has a value", () => {
        expect().toBe('');
    });

});        
});

});

I'm using Typescript as you can see and Chutzpa

u_mulder
  • 54,101
  • 5
  • 48
  • 64
Ace
  • 831
  • 2
  • 8
  • 28
  • In mocha there is before hook and beforeEach hook, so might be there will be something before hook in the typescript – Mukesh Agarwal Jan 31 '15 at 13:06
  • 1
    beforeEach will get called for every test – Mukesh Agarwal Jan 31 '15 at 13:07
  • Ok. So in my beforeEach, I have scripts which are loaded async. I can check to see if it's my first test and load them but I was just wandering if there's a way to call beforeEach once for the describe. – Ace Jan 31 '15 at 14:11
  • within describe just user before(done){ //what you want to do done()} for each describe it will run only once for each describe before the test – Mukesh Agarwal Jan 31 '15 at 15:28
  • It looks like "before" is not recognized. There's a beforeSuite: https://www.npmjs.com/package/jasmine-before-suite – Ace Jan 31 '15 at 19:47
  • Ok then use that beforeSuite.From documentation it is telling it will be called once – Mukesh Agarwal Feb 01 '15 at 08:39

2 Answers2

2

as the name says, "beforeEach" runs before-each-test, you should use "before"

Andrea
  • 21
  • 2
0

you can use the before command like so

describe('desc1', () => {
  before(() => {
    cy.log('Logged Once Before All tests');
  });

  beforeEach(() => {
    cy.log('Logged Before Every test');
  })

  it("has a value", () => {
    expect().toBe('');
  });
  it("has a value", () => {
    expect().toBe('');
  });
  it("has a value", () => {
    expect().toBe('');
  });
});

Craig Wayne
  • 4,499
  • 4
  • 35
  • 50