Jest: Imported class 'undefined'

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Jest: Imported class 'undefined'



Importing class Grid into test file but keep on getting errors that its undefined.



The import path is correct but I keep on getting "TypeError: Cannot read property 'grid' of undefined" and "Cannot read property 'instance' of undefined" in the console when I run Jest.



Grid.test.js


import React from 'react';
import shallow from 'enzyme';
import Grid from '../../components/Grid';

let grid;

describe('Grid', () =>

beforeEach(() =>
grid = shallow(<Grid/>);
);

it('renders correctly', () =>
expect(grid).toMatchSnapshot();
);

describe('splitUpTiles', () =>

it('returns an empty array if a.length === 0', () =>
expect(grid.instance().splitUpTiles([0,1], 0)).toEqual();
);
);

);



Grid.js:


import React, Component from 'react';
import Tile from './Tile';

class Grid extends Component

splitUpTiles = (a, l) =>
if (a.length === 0) return ;
else return [a.slice(0, l)].concat(this.splitUpTiles(a.slice(l), l));
;

(omitted code for brevity)

render()

(omitted code for brevity)

return (
<div>
<table>
<tbody>
this.createColumns(arrayOfTiles)
</tbody>
</table>
</div>
)



export default Grid;



I tested my other files in the same way but had no error. Can someone please explain where I am going wrong?




2 Answers
2



I think you have a scope issue with beforeEach.



According to the docs:



"By default, the before and after blocks apply to every test in a file. You can also group tests together using a describe block. When they are inside a describe block, the before and after blocks only apply to the tests within that describe block".



Try this:


describe('Grid', () =>

let grid; // Move your var inside describe

beforeEach(() =>
grid = shallow(<Grid/>);
);

it('renders correctly', () =>
expect(grid).toMatchSnapshot();
);
// Delete second describe block
it('returns an empty array if a.length === 0', () =>
expect(grid.instance().splitUpTiles([0,1], 0)).toEqual();
);

);



Check docs HERE





hey, I've tried this and I receive the same error. I even took the grid = shallow(<Grid/>); outside of the beforeEach block and it still considers grid as undefined. I don't think moving the let to inside of the describe block makes any difference and I don't think my issue is to do with the beforeEach block. Somehow the grid component is not being imported properly
– SlimJim
Aug 8 at 15:05






Do you have an error trying to import Grid?
– Damian Peralta
Aug 8 at 15:42





I have no error importing Grid (not underlined by IDE and no console error), its the correct path and the file is in the correct place. But the error must be something to do with it, I just don't know what. I've tested the other components in the same way and none of those components have been undefined.
– SlimJim
Aug 8 at 15:46





think I may have found my problem actually...
– SlimJim
Aug 8 at 16:06





If you think it's useful, share it ;)
– Damian Peralta
Aug 8 at 16:10



I think it's because of nested describe. Can you try putting your inner it inside above describe like below.


it


describe


import React from 'react';
import shallow from 'enzyme';
import Grid from '../../components/Grid';

describe('Grid', () =>

let grid;

beforeEach(() =>
grid = shallow(<Grid/>);
);

it('renders correctly', () =>
expect(grid).toMatchSnapshot();
);

it('returns an empty array if a.length === 0', () =>
expect(grid.instance().splitUpTiles([0,1], 0)).toEqual();
);

);





hey, just tried this and I receive the same error :/
– SlimJim
Aug 8 at 11:34





can you try now. i edited the answer above. If it still does't works you need to debug if beforeEach is called before each test case.
– Mohit Tilwani
Aug 8 at 11:42






unfortunately I have the same error. Even if I remove the beforeEach block and define grid normally I get the same error.
– SlimJim
Aug 8 at 15:02





fixed it, thanks for the help though
– SlimJim
Aug 8 at 16:12






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS