I’m trying to use jest’s test.each to do some table tests. Unfortunately, it seems I am not able to use variables in the array of test cases that I pass to the test.each.
How can I use the variables that are populated from beforeAll in the test cases array?
Here’s the error I receive:
TypeError: Cannot read properties of undefined (reading 'id')
51 | name: "invalid attributes",
52 | params: {
> 53 | from: customerEntity.id,
| ^
54 | to: addressEntity.id,
55 | relationshipType: "HAS_ADDRESS",
56 | },
This is my code:
import request from "supertest";
import { EntityDTO } from "../dto/entity.dto";
import { ModuleDTO } from "../dto/module.dto";
import { app } from "../main";
import { clearDatabase } from "./helpers/dbOperations";
describe("Relationship", () => {
let module: ModuleDTO;
let customerEntity: EntityDTO;
let addressEntity: EntityDTO;
let codeEntity: EntityDTO;
beforeAll(async () => {
module = (
await request(app).post("/v1/modules").send({ name: "Customer Module" })
).body;
customerEntity = (
await request(app)
.post("/v1/entities")
.send({ moduleId: module.id, name: "Customer Entity" })
).body;
addressEntity = (
await request(app)
.post("/v1/entities")
.send({ moduleId: module.id, name: "Address Entity" })
).body;
codeEntity = (
await request(app)
.post("/v1/entities")
.send({ moduleId: module.id, name: "Code Entity" })
).body;
});
afterAll(async () => {
await clearDatabase();
});
describe("Create Relationship", () => {
describe("Rejects invalid params", () => {
const testCases: {
name: string;
params: any;
expected: {
status: number;
code: string;
message: string;
};
}[] = [
{
name: "invalid attributes",
params: {
from: customerEntity.id,
to: addressEntity.id,
relationshipType: "HAS_ADDRESS",
},
expected: {
status: 400,
code: "",
message: "",
},
},
{
name: "invalid values",
params: {
fromId: "some id",
to: codeEntity.id,
relationshipType: "HAS_CODE",
},
expected: {
status: 400,
code: "",
message: "",
},
},
];
test.each(testCases)("$name", async (testCase) => {
const res = await request(app)
.post("/v1/relationships")
.send({
...testCase.params,
});
expect(res.status).toEqual(testCase.expected.status);
});
});
});
});
Ignore this below – I don’t know what more details I need to add before posting – it complains I have to add more details but there really aren’t any. Sorry!!
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla magna nisi, pretium a neque at, lobortis sodales risus. Integer sit amet nunc massa. Suspendisse potenti. Donec vitae augue vel dui bibendum mattis vel eget sem. Pellentesque convallis lorem nec neque blandit maximus sed eget quam. Etiam mattis eros sapien, a vehicula nibh rutrum at. Proin pulvinar neque neque, eget ultricies lacus faucibus nec. Pellentesque condimentum vestibulum sapien, hendrerit bibendum augue elementum quis. Curabitur ex velit, eleifend non elit ut, vehicula molestie massa. Vestibulum semper risus erat, vel vestibulum lectus porta at. Donec elementum purus neque, ut egestas enim volutpat at.