(rootName, expected, { loose = false } = {})
| 118 | |
| 119 | // Validate the v8 heap snapshot |
| 120 | validateSnapshot(rootName, expected, { loose = false } = {}) { |
| 121 | const rootNodes = this.snapshot.filter( |
| 122 | (node) => node.name === rootName && node.type !== 'string'); |
| 123 | if (loose) { |
| 124 | assert(rootNodes.length >= expected.length, |
| 125 | `Expect to find at least ${expected.length} '${rootName}', ` + |
| 126 | `found ${rootNodes.length}`); |
| 127 | } else { |
| 128 | assert.strictEqual( |
| 129 | rootNodes.length, expected.length, |
| 130 | `Expect to find ${expected.length} '${rootName}', ` + |
| 131 | `found ${rootNodes.length}`); |
| 132 | } |
| 133 | |
| 134 | for (const expectation of expected) { |
| 135 | if (expectation.children) { |
| 136 | for (const expectedEdge of expectation.children) { |
| 137 | const check = typeof expectedEdge === 'function' ? expectedEdge : |
| 138 | (edge) => (isEdge(edge, expectedEdge)); |
| 139 | const hasChild = rootNodes.some( |
| 140 | (node) => node.outgoingEdges.some(check), |
| 141 | ); |
| 142 | // Don't use assert with a custom message here. Otherwise the |
| 143 | // inspection in the message is done eagerly and wastes a lot of CPU |
| 144 | // time. |
| 145 | if (!hasChild) { |
| 146 | throw new Error( |
| 147 | 'expected to find child ' + |
| 148 | `${util.inspect(expectedEdge)} in ${inspectNode(rootNodes)}`); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | if (expectation.detachedness !== undefined) { |
| 154 | const matchedNodes = rootNodes.filter( |
| 155 | (node) => node.detachedness === expectation.detachedness); |
| 156 | if (loose) { |
| 157 | assert(matchedNodes.length >= rootNodes.length, |
| 158 | `Expect to find at least ${rootNodes.length} with ` + |
| 159 | `detachedness ${expectation.detachedness}, ` + |
| 160 | `found ${matchedNodes.length}`); |
| 161 | } else { |
| 162 | assert.strictEqual( |
| 163 | matchedNodes.length, rootNodes.length, |
| 164 | `Expect to find ${rootNodes.length} with detachedness ` + |
| 165 | `${expectation.detachedness}, found ${matchedNodes.length}`); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // Validate our internal embedded graph representation |
| 172 | validateGraph(rootName, expected, { loose = false } = {}) { |
no test coverage detected