jest test emitting events for eventemitter objects (http)
问题 assume the following nodejs code const server = http.listen(8080,'127.0.0.1') .on("error", err => { // ... }) module.exports = server; how to write a test using jest to emit the http "error" event (to cover the error event handler)? 回答1: Since you create a server in module scope, the code will execute immediately when you require or import server.js . You need to stub the http.createServer before you require this module. For testing .on(error, callback) method, you should use