* @param {string} code * @param {Record | null} manifest
(code, manifest)
| 41 | * @param {Record<string, string> | null} manifest |
| 42 | */ |
| 43 | async runTests(code, manifest) { |
| 44 | this.emit('test-start'); |
| 45 | const startTime = Date.now(); |
| 46 | const testsRan = this.testsRan; |
| 47 | this.testsRan = true; |
| 48 | logger.progress('Waiting for worker host...'); |
| 49 | const page = await this.workerHost.pageReady; |
| 50 | if (testsRan) { |
| 51 | logger.progress('Reloading worker host...'); |
| 52 | await this.workerHost.reloadPage(); // reset so that tests can be rerun. |
| 53 | } |
| 54 | this.testsRan = true; |
| 55 | logger.progress('Loading mocha and chai...'); |
| 56 | await this.loadMocha(page, this.workerHost.server.pathPrefix); |
| 57 | const globals = ['mocha', 'chai', ...this.mochaGlobals]; |
| 58 | await this.workerHost.setWorkerCode(code, '/test.js', globals, manifest); |
| 59 | /** @type {number} */ |
| 60 | logger.progress('Running tests...'); |
| 61 | const failures = await page.evaluate( |
| 62 | () => new Promise(resolve => mocha.run(resolve)) |
| 63 | ); |
| 64 | if (failures) { |
| 65 | logger.error('Failed'); |
| 66 | } else { |
| 67 | logger.success('Passed', Date.now() - startTime); |
| 68 | } |
| 69 | this.emit('test-end', failures); |
| 70 | return failures; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @param {import('puppeteer').Page} page |
no test coverage detected