Created
May 28, 2018 09:37
-
-
Save fatso83/b5d7d3aa366fc4e5d6b622a5b8c6c338 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const puppeteer = require("puppeteer"); | |
const http = require("http"); | |
const fs = require("fs"); | |
const port = 3876; | |
const scriptContent = ` | |
import sinon from '/sinon-esm.js'; | |
console.log('sinon is here', typeof sinon); | |
console.log('the answer of all is', sinon.stub().returns(42)()); | |
window.sinonLoaded = true; | |
window.mySinon = sinon; | |
`; | |
const htmlWithModuleScript = ""; | |
//const htmlWithModuleScript = ` | |
//<script type="module"> | |
//${content} | |
//</script> | |
//`; | |
// change to where our built sinon module resides | |
process.chdir(`${__dirname}/../../pkg/`); | |
const sinonModule = fs.readFileSync("./sinon-esm.js"); | |
// This runs in the browser (Chrome)! | |
async function evaluatePageContent() { | |
console.log("Starting browser ..."); | |
const browser = await puppeteer.launch(); | |
const page = await browser.newPage(); | |
await page.goto("http://localhost:3876"); | |
page.on("error", function(err) { | |
const theTempValue = err.toString(); | |
console.log("Error: " + theTempValue); | |
}); | |
const frame = page.mainFrame(); | |
await frame.addScriptTag({ content: scriptContent, type: "module" }); | |
page | |
.waitForFunction(() => typeof window.sinonLoaded !== "undefined", { | |
timeout: 5000 | |
}) | |
.then(() => browser.close()) | |
.then(() => { | |
console.log("We were successful"); | |
process.exit(); | |
}) | |
.catch(err => { | |
console.log("Failed: ", err); | |
}) | |
.then(() => browser.close()) | |
.then(() => process.exit(1)); | |
} | |
const app = http.createServer((req, res) => { | |
res.writeHead(200); | |
if (req.url.match(/sinon-esm\.js/)) { | |
console.log("server responded with sinon module"); | |
res.end(sinonModule); | |
} else { | |
res.end(htmlWithModuleScript); | |
} | |
}); | |
app.listen(port, evaluatePageContent); | |
//app.listen(port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This times out, for some reason. Chrome and Firefox finds the
sinonLoaded
without problems.