When I try to perform a JEST test using a parameter fetched from a database, it simply doesn’t work… I tried an example without a database, and it doesn’t work the same way.
See Code
// Example function that queries IDs from the database
async function fetchIDsFromDatabase() {
// Simulating an asynchronous database query
return [1, 2, 3]; // Example list of IDs returned by the database
}
// Test using Jest
describe('Test for IDs returned from the database', () => {
let ids;
// Before all tests, fetch IDs from the database
beforeAll(async () => {
ids = await fetchIDsFromDatabase();
});
// Test to verify if the list of IDs is not empty
test('Checks if the list of IDs is not empty', () => {
expect(ids).toBeTruthy();
});
// Iterate over each ID returned from the database using test.each
test.each(ids)('Testing ID %i', async (id) => {
// Here you can perform test operations for each ID
expect(id).toBeGreaterThan(0); // Simple assertion example
// Add more assertions as needed
});
});
the return IS
Test for IDs returned from the database
√ Checks if the list of IDs is not empty (2 ms)
× Testing ID %i (1 ms)
● Test for IDs returned from the database › Testing ID %i
`.each` must be called with an Array or Tagged Template Literal.
Instead was called with: undefined
20 |
21 | // Iterate over each ID returned from the database using test.each
> 22 | test.each(ids)('Testing ID %i', async (id) => {
| ^
23 | // Here you can perform test operations for each ID
24 | expect(id).toBeGreaterThan(0); // Simple assertion example
25 | // Add more assertions as needed
at each (__TEST__/api.test.js:22:10)
at Object.describe (__TEST__/api.test.js:8:3)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 passed, 2 total
Snapshots: 0 total
Time: 0.508 s, estimated 1 s
Ran all test suites.