I'm trying to write a test using Vitest for a function localComplement that modifies a graph represented using Cytoscape.js. Here's the function:
export function localComplement(cy: any, elements: TElement | undefined, target: string): any { const neighbors: string[] = []; const pairs: [string, string][] = []; // Loop through the elements to find connections if (elements != undefined) { elements.edges.forEach((edge) => { // Check if the element is an Edge if ('source' in edge.data && 'target' in edge.data) { // If the target matches the source, add the target to neighbors if (edge.data.source === target) { neighbors.push(edge.data.target); } // If the target matches the target, add the source to neighbors if (edge.data.target === target) { neighbors.push(edge.data.source); } } }); } // Create every neighbor composition for (let i = 0; i < neighbors.length; i++) { for (let j = i + 1; j < neighbors.length; j++) { pairs.push([neighbors[i], neighbors[j]]); } } pairs.forEach(([node1, node2]) => { if (isConnected(elements, node1, node2)) { removeEdge(cy, elements, node1, node2); } else { addEdge(cy, elements, node1, node2); } }); return cy;}
I wrote the following test to verify this function:
import { describe, it, expect, test } from 'vitest'; // Importing necessary functions from Vitestimport { localComplement } from './GraphUtilities.ts'; // Importing the localComplement functionimport { type TElement } from './GraphType';import cytoscape from 'cytoscape';import { gridLayout } from './config.js';// Define constants for graph elementsconst graphElementBeforeLC: TElement = { nodes: [ { data: { id: '0' } }, { data: { id: '1' } }, { data: { id: '2' } }, { data: { id: '3' } }, { data: { id: '4' } } ], edges: [ { data: { id: '01', source: '0', target: '1' } }, { data: { id: '12', source: '1', target: '2' } }, { data: { id: '23', source: '2', target: '3' } }, { data: { id: '34', source: '3', target: '4' } } ]};const graphElementAfterLC: TElement = { nodes: [ { data: { id: '0' } }, { data: { id: '1' } }, { data: { id: '2' } }, { data: { id: '3' } }, { data: { id: '4' } } ], edges: [ { data: { id: '01', source: '0', target: '1' } }, { data: { id: '12', source: '1', target: '2' } }, { data: { id: '13', source: '1', target: '3' } }, { data: { id: '23', source: '2', target: '3' } }, { data: { id: '34', source: '3', target: '4' } } ]};describe('LC test', () => { const cy = cytoscape({ elements: graphElementBeforeLC, style: [], layout: gridLayout }); const result = localComplement(cy, graphElementBeforeLC, '2'); const expectedCy = cytoscape({ elements: graphElementAfterLC, style: [], layout: gridLayout }); // expect(result).toEqual(expectedCy); it('localComplement should add edges correctly for node "2"', () => { expect(result).toEqual(expectedCy); });});
When I run the test, I encounter the following error:
Unhandle error
Vitest caught 1 unhandled error during the test run.This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.
Observations
- The demo tests from Vitest run fine, but my custom test triggers this error.
- I suspect the error might be related to how I'm using Cytoscape.js or comparing the results (cy objects).
- I also verified that the localComplement function works as expected when used in isolation.