Why can't nested describe() blocks see vars defined in outer blocks?

前端 未结 3 1125
攒了一身酷
攒了一身酷 2021-01-30 20:07

I\'ve run into this issue in real code, but I put together a trivial example to prove the point.

The below code works fine. I\'ve set up a variable in my root desc

3条回答
  •  后悔当初
    2021-01-30 20:35

    Well you could still initialize variables outside the beforeEach block. I generally do it for constants and still remain DRY without introducing beforeEach blocks.

    describe('simple object', function () {
        const orchard = {
            trees: {
                apple: 10,
                orange: 20
            },
            bushes: {
                boysenberry: 40,
                blueberry: 35
            }
        };
    
    
        describe('trees', function () {
            const trees = orchard.trees;
    
            it('should have apples and oranges', function () {
    
    
                expect(trees.apple).toBeDefined();
                expect(trees.orange).toBeDefined();
    
                expect(trees.apple).toEqual(10);
                expect(trees.orange).toEqual(20);
            });
            it('should NOT have pears or cherries', function () {
                var trees = orchard.trees;
    
                expect(trees.pear).toBeUndefined();
                expect(trees.cherry).toBeUndefined();
            });
        });
    });
    

提交回复
热议问题