Having an issue with running Jest testing with my lambda/node code. When I run the index.js the Axios get works fine.
This is my index.js
const sslRootCAs = require('ssl-root-cas/latest')
const util = require('util')
exports.handler = async function (event) {
const axios = require('axios');
//const user = process.env.user;
//const pw = process.env.pw;
const user = '';
const pw = '';
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
return axios.get('https://url', {
auth: {username: user, password: pw}, rejectUnauthorized: false
})
.then(res => {
return {
statusCode: 200,
body: res.data
};
})
.catch(err => {
return {
statusCode: 500,
body: JSON.stringify(err)
};
});
}
This is my index.test.js
const lambda = require("./index");
const axios = require("axios");
const mockAdapter = require("axios-mock-adapter");
// afterEach(() => {
// mockAxios.reset();
// });
it("test get - this will pass", async () => {
let mock = new mockAdapter(axios, {delayResponse: 1000});
mock.onGet().reply({
status: 200,
data: {
expand: 'schema,names',
startAt: 0,
maxResults: 50,
total: 2
}
});
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
var event = {Key: "12345"};
const result = await lambda.handler(event);
console.log(result);
expect(result).toMatchObject({ status: 200, body: "test" });
mock.reset();
});
I've tried multiple things with the index.test.js. I've even cut out all the mocking and was testing to see if the get would work. It does not. I get "unable to verify the first certificate" when I run npm test.
The code does not throw that error when I do a node index.js. I suspect jest has some type of built in handler for that error or perhaps Axios?
Any ideas would be great.
Thanks, Tim