forked from sinonjs/sinon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Post-build hook that tests the built ECMAScript Module
closes sinonjs#1805
- Loading branch information
Showing
3 changed files
with
79 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
parserOptions: | ||
sourceType: module | ||
ecmaVersion: 2017 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* eslint-disable no-process-exit */ | ||
const puppeteer = require("puppeteer"); | ||
|
||
const http = require("http"); | ||
const fs = require("fs"); | ||
const port = 3876; | ||
|
||
const htmlWithModuleScript = ` | ||
<script type="module"> | ||
import sinon from '/sinon-esm.js'; | ||
const assert = (result) => { if(!result) throw new Error(); }; | ||
try { | ||
const stub = sinon.stub().returns(42); | ||
assert(42 === stub()); | ||
console.log('sinon-result:works'); | ||
} catch(err) { | ||
console.log('sinon-result:fails'); | ||
} | ||
</script> | ||
`; | ||
|
||
// start server where our built sinon esm bundle resides | ||
process.chdir(`${__dirname}/../../pkg/`); | ||
const sinonModule = fs.readFileSync("./sinon-esm.js"); | ||
|
||
async function evaluatePageContent() { | ||
const browser = await puppeteer.launch(); | ||
const page = await browser.newPage(); | ||
|
||
page.on("error", function (err) { | ||
throw err; | ||
}); | ||
|
||
// our "assertion framework" :) | ||
page.on("console", function (msg) { | ||
var text = msg.text(); | ||
|
||
if (text.startsWith("sinon-result:works")) { | ||
browser.close(); | ||
process.exit(0); | ||
} else if (text.startsWith("sinon-result:fails")) { | ||
browser.close(); | ||
process.exit(1); | ||
} | ||
}); | ||
|
||
await page.goto("http://localhost:3876"); | ||
} | ||
|
||
const app = http.createServer((req, res) => { | ||
let body, type; | ||
|
||
if (req.url.match(/sinon-esm\.js/)) { | ||
body = sinonModule; | ||
type = "application/javascript"; | ||
} else { | ||
body = htmlWithModuleScript; | ||
type = "text/html"; | ||
} | ||
|
||
const headers = { | ||
"Content-Length": Buffer.byteLength(body), | ||
"Content-Type": type | ||
}; | ||
res.writeHead(200, headers); | ||
res.end(body); | ||
}); | ||
|
||
app.listen(port, evaluatePageContent); |