(session)
| 165 | } |
| 166 | |
| 167 | async function testCommandLineAPI(session) { |
| 168 | const testModulePath = require.resolve('../fixtures/empty.js'); |
| 169 | const testModuleStr = JSON.stringify(testModulePath); |
| 170 | const printAModulePath = require.resolve('../fixtures/printA.js'); |
| 171 | const printAModuleStr = JSON.stringify(printAModulePath); |
| 172 | const printBModulePath = require.resolve('../fixtures/printB.js'); |
| 173 | const printBModuleStr = JSON.stringify(printBModulePath); |
| 174 | |
| 175 | // We can use `require` outside of a callframe with require in scope |
| 176 | let result = await session.send( |
| 177 | { |
| 178 | 'method': 'Runtime.evaluate', 'params': { |
| 179 | 'expression': 'typeof require("fs").readFile === "function"', |
| 180 | 'includeCommandLineAPI': true |
| 181 | } |
| 182 | }); |
| 183 | checkException(result); |
| 184 | assert.strictEqual(result.result.value, true); |
| 185 | |
| 186 | // The global require has the same properties as a normal `require` |
| 187 | result = await session.send( |
| 188 | { |
| 189 | 'method': 'Runtime.evaluate', 'params': { |
| 190 | 'expression': [ |
| 191 | 'typeof require.resolve === "function"', |
| 192 | 'typeof require.extensions === "object"', |
| 193 | 'typeof require.cache === "object"', |
| 194 | ].join(' && '), |
| 195 | 'includeCommandLineAPI': true |
| 196 | } |
| 197 | }); |
| 198 | checkException(result); |
| 199 | assert.strictEqual(result.result.value, true); |
| 200 | // `require` twice returns the same value |
| 201 | result = await session.send( |
| 202 | { |
| 203 | 'method': 'Runtime.evaluate', 'params': { |
| 204 | // 1. We require the same module twice |
| 205 | // 2. We mutate the exports so we can compare it later on |
| 206 | 'expression': ` |
| 207 | Object.assign( |
| 208 | require(${testModuleStr}), |
| 209 | { old: 'yes' } |
| 210 | ) === require(${testModuleStr})`, |
| 211 | 'includeCommandLineAPI': true |
| 212 | } |
| 213 | }); |
| 214 | checkException(result); |
| 215 | assert.strictEqual(result.result.value, true); |
| 216 | // After require the module appears in require.cache |
| 217 | result = await session.send( |
| 218 | { |
| 219 | 'method': 'Runtime.evaluate', 'params': { |
| 220 | 'expression': `JSON.stringify( |
| 221 | require.cache[${testModuleStr}].exports |
| 222 | )`, |
| 223 | 'includeCommandLineAPI': true |
| 224 | } |
no test coverage detected
searching dependent graphs…