I am trying to mock https.get with is nested in my custom function getSecureString(). https.get returns two json elements which getSecureString() uses in its try/catch section. See functions below.
Online version at https://repl.it/@SandraSchlichti/jest-playground-4#getSecureString.test.js
The error I am getting is
Expected: 1
Received: undefined
25 |
26 | it('should return 1', () => {
> 27 | expect(result).toEqual(1)
| ^
28 | })
29 | });
30 |
at Object.<anonymous> (getSecureString.test.js:27:22)
where I would have expected the test to break as I haven't included the mocked res.statusCode: 200 response from https.get() yet.
getSecureString.js
const fs = require('fs');
const https = require('https');
var uuid = require('uuid');
const {v4: uuidv4} = require('uuid');
module.exports = async function getSecureString(options) {
options = options || {};
options.hostname = options.hostname || {};
options.path = options.path || '/';
options.string = options.string || {};
options.statusCode = options.statusCode || {};
options.aftaleId = options.aftaleId || {};
options.certFile = options.certFile || {};
options.keyFile = options.keyFile || {};
options.timeout = options.timeout || 0;
const opt = {
hostname: options.hostname,
port: 443,
path: options.path,
method: 'GET',
cert: fs.readFileSync(options.certFile),
key: fs.readFileSync(options.keyFile),
headers: {'AID': options.aftaleId,
'TID': uuidv4(),
'RID': uuidv4()
},
};
opt.agent = new https.Agent(opt);
try {
const r = await httpsGet(opt, options.timeout);
return (r.statusCode === options.statusCode && r.body.includes(options.string)) ? 1 : 0;
} catch (error) {
console.error(error);
}
};
function httpsGet(opts, timeout) {
return new Promise((resolve, reject) => {
const req = https.get(opts, (res) => {
let body = '';
res.on('data', (data) => {
body += data.toString();
});
res.on('end', () => {
resolve({body, statusCode: res.statusCode});
});
});
req.setTimeout(timeout, function() {
req.destroy('error');
});
req.on('error', (e) => {
console.error(e);
reject(e);
});
});
};
getSecureString.test.js
const getSecureString = require('./getSecureString');
const https = require('https');
jest.mock('https');
describe("getSecureString ", () => {
describe('when httpsGet returns expected statusCode and body includes expected string', () => {
let result;
beforeAll(async () => {
https.get.mockResolvedValue({
body: 'xyz secret_string xyz',
//res.statusCode: 200
})
result = await getSecureString({
hostname: 'encrypted.google.com',
path: '/',
string: 'secret_string',
statusCode: 200,
aftaleId: 1234,
certFile: './test.crt',
keyFile: './test.key',
timeout: 1000,
})
});
it('should return 1', () => {
expect(result).toEqual(1)
})
});
describe('when httpsGet returns expected statusCode and body includes expected string', () => {
let result;
beforeAll(async () => {
https.get.mockResolvedValue({
body: 'xyz secret_string xyz',
//res.statusCode: 200
})
result = await getSecureString({
hostname: 'encrypted.google.com',
path: '/',
string: 'not_secret_string',
statusCode: 201,
aftaleId: 1234,
certFile: './test.crt',
keyFile: './test.key',
timeout: 1000,
})
});
it('should return 0', () => {
expect(result).toEqual(0)
})
});
describe("when an exception is thrown", () => {
let result;
beforeAll(async () => {
// mockRejected value returns rejected promise
// which will be handled by the try/catch
https.get.mockRejectedValue({
body: 'xyz secret_string xyz',
//res.statusCode: 200
})
result = await getSecureString();
})
it('should return -1', () => {
expect(result).toEqual(-1)
})
});
});
Question
Can anyone figure out why I get the above error, and how I can mock both body and res.statusCode in https.get()?