OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

When I try to perform a JEST test using a parameter fetched from a database, it simply doesn't work

  • Thread starter Thread starter user25778937
  • Start date Start date
U

user25778937

Guest
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;

Code:
// 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

Code:
`.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.

<p>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.</p>
<p>See Code</p>
<p><code>// Example function that queries IDs from the database</code>
<code>async function fetchIDsFromDatabase() {</code>
<code>// Simulating an asynchronous database query</code>
<code>return [1, 2, 3]; // Example list of IDs returned by the database</code>
<code>}</code></p>
<p><code>// Test using Jest</code>
<code>describe('Test for IDs returned from the database', () => {</code>
<code>let ids;</code></p>
<pre><code>// 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
});
</code></pre>
<p><code>});</code></p>
<p>the return IS</p>
<p>Test for IDs returned from the database
√ Checks if the list of IDs is not empty (2 ms)<br />
× Testing ID %i (1 ms)</p>
<p>● Test for IDs returned from the database › Testing ID %i</p>
<pre><code>`.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)
</code></pre>
<p>Test Suites: 1 failed, 1 total<br />
Tests: 1 failed, 1 passed, 2 total<br />
Snapshots: 0 total
Time: 0.508 s, estimated 1 s
Ran all test suites.</p>
 

Latest posts

B
Replies
0
Views
1
Blundering Ecologist
B
Top