Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring variable in beforeEach

I get

ReferenceError: initialState is not defined

when I declare initialState as const in beforeEach(()=> {.... Shouldn't that supposed to work ?

describe('register reducer', () => {
        beforeEach(() => {
            const initialState = UsersService.getInitialUsersState();
        })

        it('should return the initial state', () => {
            expect(usersReducer(undefined, [])).toEqual(initialState);
        });

        it('Toggle isBaby or sitter', () => {
            deepFreeze(initialState);
            let newState = initialState;
            newState.isBaby = true;

            expect(
                usersReducer(initialState, {
                    type: types.UsersActions.SET_TYPE,
                    payload: true
                })).toEqual(newState);
        });
like image 580
July333 Avatar asked May 29 '18 08:05

July333


2 Answers

While it's true that beforeEach runs before each test, if you do it that way, initialState is only visible in the scope of beforeEach, change it to this:

describe('register reducer', () => {
  let initialState;
  beforeEach(() => {
      initialState = UsersService.getInitialUsersState();
  })
  ...
like image 86
bugs Avatar answered Nov 13 '22 03:11

bugs


this question is really old, but just putting a working example here for others that come across this question. here's one way you could do it:

describe('register reducer', () => {
  let initialState
  beforeEach(() => {
      initialState = UsersService.getInitialUsersState();
  })

  it('should return the initial state', () => {
      expect(usersReducer(undefined, [])).toEqual(initialState);
  });

  it('Toggle isBaby or sitter', () => {
      deepFreeze(initialState);
      let newState = initialState;
      newState.isBaby = true;

      expect(
        usersReducer(initialState, {
          type: types.UsersActions.SET_TYPE,
          payload: true
        })
      ).toEqual(newState)
  })
})
like image 23
brianyang Avatar answered Nov 13 '22 02:11

brianyang