admin管理员组文章数量:1287956
I'm trying to test my app using Jest and Node.js. What is the correct setup to avoid getting the following error in the terminal when running tests with JestJS?
Cannot read property 'addEventListener' of null
The test for the sum
function passes as soon as I ment out adding the event listener in the app.js
file. I'm not even sure why is this line as well as the console.log('Not part...')
executed by Jest since I only export the sum
function.
The contents of my index.html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<button id="button">JavaScript</button>
<script src="./app.js"></script>
</body>
</html>
The contents of my app.js file:
function sum(a, b) {
return a + b;
}
console.log('Not part of module.exports but still appearing in terminal, why?');
var button = document.getElementById('button');
button.addEventListener('click', function(e) {
console.log('button was clicked');
});
module.exports = {
sum
};
The contents of my app.test.js file:
var { sum } = require('./app');
describe('sum', () => {
test('adds numbers', () => {
expect(sum(1, 2)).toBe(3);
});
});
My package.json:
"scripts": {
"test": "jest --coverage",
"test:watch": "npm run test -- --watch"
},
I'm trying to test my app using Jest and Node.js. What is the correct setup to avoid getting the following error in the terminal when running tests with JestJS?
Cannot read property 'addEventListener' of null
The test for the sum
function passes as soon as I ment out adding the event listener in the app.js
file. I'm not even sure why is this line as well as the console.log('Not part...')
executed by Jest since I only export the sum
function.
The contents of my index.html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<button id="button">JavaScript</button>
<script src="./app.js"></script>
</body>
</html>
The contents of my app.js file:
function sum(a, b) {
return a + b;
}
console.log('Not part of module.exports but still appearing in terminal, why?');
var button = document.getElementById('button');
button.addEventListener('click', function(e) {
console.log('button was clicked');
});
module.exports = {
sum
};
The contents of my app.test.js file:
var { sum } = require('./app');
describe('sum', () => {
test('adds numbers', () => {
expect(sum(1, 2)).toBe(3);
});
});
My package.json:
"scripts": {
"test": "jest --coverage",
"test:watch": "npm run test -- --watch"
},
Share
Improve this question
edited Mar 4, 2017 at 12:36
Piotr Berebecki
asked Mar 4, 2017 at 11:26
Piotr BerebeckiPiotr Berebecki
7,4684 gold badges35 silver badges42 bronze badges
1
-
1
getElementById
might execute too soon. Maybe put that code block inwindow.load = function () { ... }
. – trincot Commented Mar 4, 2017 at 14:17
1 Answer
Reset to default 9getElementById
might execute before the DOM is loaded. Put that code block in a callback that is executed when the document has been loaded. For instance:
document.addEventListener('DOMContentLoaded', function () {
console.log('Not part of module.exports but still appearing in terminal, why?');
var button = document.getElementById('button');
button.addEventListener('click', function(e) {
console.log('button was clicked');
});
});
本文标签:
版权声明:本文标题:node.js - Jest testing of simple vanilla JavaScript - Cannot read property 'addEventListener' of null - Stack Ov 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741322904a2372309.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论