An error, Cannot find context with specified id undefined, can be thrown by Puppeteer. According to an issue in the Puppeteer issue queue, this can arise from unplanned interactions between Puppeteer and WebSockets: https://github.com/GoogleChrome/puppeteer/issues/1325 This issue in turn affects the Socket.IO support in the Notes application, and therefore it may be useful to disable Socket.IO support during test runs.
It's fairly simple to allow disabling of Socket.IO. In app.mjs, add this exported function:
export function enableSocketio() {
var ret = true;
const env = process.env.NOTES_DISABLE_SOCKETIO;
if (!env || env !== 'true') {
ret = true;
}
return ret;
}
This looks for an environment variable to cause the function to return true or false.
In routes/index.mjs and routes/notes.mjs, add this line:
import { enableSocketio, sessionCookieName } from '../app';
We do this to import the preceding function. It also demonstrates some of the flexibility we get from ES6 Modules, because we can import just the required functions.
In routes/index.mjs and routes/notes.mjs, for every router function that calls res.render to send results, use the enableSocketio function as so:
res.render('view-name', {
...
enableSocketio: enableSocketio()
});
Hence, we've imported the function and for every view we pass enableSocketio as data to the view template.
In views/index.hbs and views/noteview.hbs, we have a section of JavaScript code to implement SocketIO-based semi-real-time features. Surround each such section like so:
{{#if enableSocketio}}
... JavaScript code for SocketIO support
{{/if}}
By eliminating the client-side SocketIO code, we ensure the user interface does not open a connection to the SocketIO service. The point of this exercise was to avoid using WebSockets to avoid issues with Puppeteer.
Similarly, in views/noteview.hbs support disabling the Comment button like so:
{{#if enableSocketio}}
<button id="btnComment" type="button" class="btn btn-outline-dark"
data-toggle="modal" data-target="#notes-comment-modal">Comment</button>
{{/if}}
The final step would be to set the environment variable, NOTES_DISABLE_SOCKETIO, in the Docker Compose file.