diff --git a/lively.server/plugins/test-runner.js b/lively.server/plugins/test-runner.js index b55bf675c0..61ae729dbd 100644 --- a/lively.server/plugins/test-runner.js +++ b/lively.server/plugins/test-runner.js @@ -1,4 +1,5 @@ import { HeadlessSession } from 'lively.headless'; +import { promise } from 'lively.lang'; export default class TestRunner { async setup (livelyServer) { @@ -8,35 +9,50 @@ export default class TestRunner { async run (module_to_test) { let results; try { - this.headlessSession = new HeadlessSession(); - await this.headlessSession.open('http://localhost:9011/worlds/load?name=__newWorld__&askForWorldName=false&fastLoad=false', (sess) => sess.runEval(`typeof $world !== 'undefined' && $world.name == 'aLivelyWorld' && $world._uiInitialized`)); - results = await this.headlessSession.runEval(` - const { resource } = await System.import("lively.resources"); - const { promise } = await System.import('lively.lang') - const { localInterface } = await System.import("lively-system-interface"); - const { loadPackage } = await System.import("lively-system-interface/commands/packages.js"); - const packageToTestLoaded = localInterface.coreInterface.getPackages().find(pkg => pkg.name === '${module_to_test}'); - if (!packageToTestLoaded){ - await loadPackage(localInterface.coreInterface, { - name: '${module_to_test}', - address: 'http://localhost:9011/local_projects/${module_to_test}', - type: 'package' + let attempts = 1; + while (true) { + try { + this.headlessSession = new HeadlessSession(); + await this.headlessSession.open('http://localhost:9011/worlds/load?name=__newWorld__&askForWorldName=false&fastLoad=false', (sess) => sess.runEval(`typeof $world !== 'undefined' && $world.name == 'aLivelyWorld' && $world._uiInitialized`)); + } catch (err) { + if (attempts < 3) { + attempts++; + await this.headlessSession.dispose(); + await promise.delay(1000); + console.log('Failed to load browser, retrying...') + continue; + } + else throw err; + } + break; + } + results = await this.headlessSession.runEval(` + const { resource } = await System.import("lively.resources"); + const { promise } = await System.import('lively.lang') + const { localInterface } = await System.import("lively-system-interface"); + const { loadPackage } = await System.import("lively-system-interface/commands/packages.js"); + const packageToTestLoaded = localInterface.coreInterface.getPackages().find(pkg => pkg.name === '${module_to_test}'); + if (!packageToTestLoaded){ + await loadPackage(localInterface.coreInterface, { + name: '${module_to_test}', + address: 'http://localhost:9011/local_projects/${module_to_test}', + type: 'package' + }); + } + $world.handForPointerId(1); + await System.import('mocha-es6/index.js'); + await promise.waitFor(()=> !!window.chai && !!window.Mocha); + const { default: TestRunner } = await System.import("lively.ide/test-runner.js"); + const runner = new TestRunner(); + const results = await runner.runTestsInPackage('${module_to_test}'); + results.forEach(r => { + r.tests?.forEach(t => { + if (t.error) t.error = true + else t.error = false; + }) }); - } - $world.handForPointerId(1); - await System.import('mocha-es6/index.js'); - await promise.waitFor(()=> !!window.chai && !!window.Mocha); - const { default: TestRunner } = await System.import("lively.ide/test-runner.js"); - const runner = new TestRunner(); - const results = await runner.runTestsInPackage('${module_to_test}'); - results.forEach(r => { - r.tests?.forEach(t => { - if (t.error) t.error = true - else t.error = false; - }) - }); - JSON.stringify(results); - `) + JSON.stringify(results); + `) } catch (err) { results = JSON.stringify({ error: err.message }); } finally {