timray
timray

Reputation: 626

Access const defined in beforeEach within test in Jasmine

For the given code, how can I access the const myConst within test myTest:

describe('HeaderCustomerDetailsComponent child components',() => {
  beforeEach(() => {
    ...
    const myConst = "Something";
  });

  it('myTest', () => {
    expect(myConst).toBeTruthy();
  });
});

Upvotes: 3

Views: 2744

Answers (1)

Tiz
Tiz

Reputation: 707

Because you're defining myConst in the beforeEach(...) method its constrained to that scope. The best way to do it is to probably move myConst out to the class level, and define it using let.

Try this:

describe('HeaderCustomerDetailsComponent child components',() => {
  let myConst;

  beforeEach(() => {
    ...
    this.myConst = "Something";
  });

  it('myTest', () => {
    expect(this.myConst).toBeTruthy();
  });
});

Since you are still setting myConst in the beforeEach(...) method you avoid pollution between tests, but you should still be careful.

Upvotes: 7

Related Questions