app.test.js
I have the following code in my jest file:
'use strict';
const request = require('supertest');
const app = require('./app');
//https://stackoverflow.com/questions/1714786/query-string-encoding-of-a-javascript-object
function serialise (obj) {
return Object.keys(obj).map(k => `${encodeURIComponent(k)}=${encodeURIComponent(obj[k])}`).join('&');
}
describe('Test other /', () => {
test('POST /example succeeds (200 OK) if checkboxes are ticked', () => {
const toSend = {
check: 'Spiderman'
};
return request(app).post('/example')
.send(serialise(toSend))
.expect(200);
});
});
The test is fine so far, but I would like to set a variable (in my node.js file) called Identifier
equal to the value 1
for the duration of this particular test. How can I do this via jest?
(I tried reading the documentation for jest and looked at similar question on SO but was unable to find a more specific answer).
app.js
node.js /example POST route:
app.post('/example', (req, res) => {
var checked = req.body.check;
var Identifier = req.app.get('identifier'); // Accessed like a global variable (value set in previous block of code).
...
});