I'm trying to make unit and e2e test on a project, i decided to use jest and puppeteer (with also jest-puppeteer) to achive this.
My problem is that I initialize a var, named tools, in an script of index.html and i want to get it to do some test after, but he return me an error that is "tools" is not defined.
I already tryed to see on the web if a solution exist but without success.
Can I have somme help ? :')
Code extracts:
// index.html
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<script src="./js/Variables.js"></script>
<script src="./js/Tools.js"></script>
</head>
<body>
<script>
tools = new Tools();
</script>
</body>
</html>
// Variables.js
let tools;
// Tools.js
class Tools {
constructor(){
// do some stuff
}
test(){
return "test string";
}
}
// app.test.js
beforeAll(async () => {
await page.goto('http://myPage/');
});
test("can i get \"tools\"", () => {
console.log(tools); // tools is not defined
expect(tools.test()).toBe("test string");
});
EDIT 22/07/2022 15:38
I finally managed to get something BUT now i can't use functions on it, the error says that tools.test() is not a function, it seems to retrieve only his "pure" value and not the Tools instance.
test("can i get \"tools\"", async () => {
let tools = await page.evaluate('tools');
console.log(tools); // gets {} (doesn't seems to retrieve instance)
expect(tools.test()).toBe("test string"); // TypeError: tools.test() is not a function
});