So this question seems really stupid, but I've been searching and searching, and have not found an answer.
I'm trying out the Jest framework for unit testing my custom javascript project, which is not using React or any other framework. My project structure currently looks like this:
Inside main.js there is class Card defined. I doubt there are any issues with the file, as I've run it perfectly fine in the browser.
"use strict";
class Card
{
...
}
The one test I've written so far, just to see if it works is:
require('../js/main.js');
test("stuff", () => {
let card = new Card(1, 1);
});
When I try to run the test:
yarn test
But all I'm getting is:
ReferenceError: Card is not defined
2 |
3 | test("stuff", () => {
> 4 | let card = new Card(1, 1);
| ^
5 | });
at Object.<anonymous>.test (__tests__/card.test.js:4:13)
It seems like main.js is getting loaded, because if I add a console.log() in it, that gets output when I run the test. So why is it complaining that Card is not defined?
I got something that seems to work, though using ES6 modules.
"use strict";
export class Card
{
...
}
yarn add --dev @babel/core @babel/cli @babel/preset-env
{
"presets": ["@babel/preset-env"]
}
const { Card } = require('../js/models.js');
test("stuff", () => {
let card = new Card(1, 1);
});
Now there are no errors when running
yarn test
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With