Skip to content

Instantly share code, notes, and snippets.

@fatso83
Created May 28, 2018 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fatso83/b5d7d3aa366fc4e5d6b622a5b8c6c338 to your computer and use it in GitHub Desktop.
Save fatso83/b5d7d3aa366fc4e5d6b622a5b8c6c338 to your computer and use it in GitHub Desktop.
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);
@fatso83
Copy link
Author

fatso83 commented May 28, 2018

This times out, for some reason. Chrome and Firefox finds the sinonLoaded without problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment