'Data not being added to Redis during integration test
I am attempting to write a integration test for my expressjs router with a Redis store.
const request = require('supertest');
const redisClient = require('../../startup/redis-client');
describe('/api/', () => {
beforeEach(() => {
server = require('../../index');
});
afterEach(async () => {
await redisClient.flushAll();
await redisClient.disconnect();
server.close();
});
describe('GET /:key', () => {
it('should return the key', async () => {
console.log(redisClient);
await redisClient.set('key1', 'value1');
await redisClient.set('key2', 'value2');
const res = await request(server).get('/api/key1');
expect(res.status).toBe(200);
expect(res.body).toBe('value1');
});
});
});
The resulting data looks like this:
[
{
id: '1647263665426-0',
message: [Object: null prototype] { key1: 'value1' }
}
]
My route handler code:
const result = await redisClient.get(key);
console.log(result);
if(result && result[0] && result[0].message)
{
res.send(result[0].message[`${key}`]);
} else {
res.status(404).send("Key not found.");
}
Is there a reason result[0].message['${key}']) or result[0].message.key does not work? What is the correct way to get the 'value1' from this returned object from Redis?
Solution 1:[1]
Turning the response for node-redis to a valid object allowed me to return the value I expected to.
const resultObject = JSON.parse(JSON.stringify(result[0].message));
res.send(resultObject[key]);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | nbonbon |
