Skip to content

Commit

Permalink
Post-build hook that tests the built ECMAScript Module
Browse files Browse the repository at this point in the history
  • Loading branch information
fatso83 committed May 28, 2018
1 parent e3d9fd2 commit 19705f8
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@
"test-coverage": "mochify --recursive -R dot --grep WebWorker --invert --plugin [ proxyquire-universal ] --plugin [ mochify-istanbul --exclude '**/test/**' --report text --report lcovonly --dir ./coverage ] test/**.js",
"test-cloud": "npm run test-headless -- --wd",
"test-webworker": "mochify --https-server 8080 test/webworker/webworker-support-assessment.js",
"test-es-module": "mocha -r esm test/es2015/module-support-assessment-test.mjs",
"test": "run-s test-node test-headless test-webworker test-es-module",
"test-esm": "mocha -r esm test/es2015/module-support-assessment-test.mjs",
"test-esm-bundle": "node test/es2015/check-esm-bundle-is-runnable.js",
"test": "run-s test-node test-headless test-webworker test-esm",
"check-dependencies": "dependency-check package.json --unused --no-dev --ignore-module coveralls --ignore-module esm",
"build": "node ./build.js",
"build": "run-p build-*",
"build-bundle": "node ./build.js",
"build-esm": "rollup -c",
"lint": "run-p lint-js lint-markdown",
"lint-js": "eslint '**/*.{js,mjs}'",
"lint-markdown": "find docs -type f -name '*.md' ! -name 'changelog.md' | xargs markdownlint",
"precommit": "lint-staged",
"pretest-webworker": "npm run build",
"prebuild": "rimraf pkg && npm run check-dependencies",
"postbuild": "run-s test-esm-bundle",
"prepublishOnly": "npm run build",
"preversion": "./scripts/preversion.sh",
"postversion": "./scripts/postversion.sh"
Expand Down Expand Up @@ -69,6 +72,7 @@
"proxyquire": "^1.8.0",
"proxyquire-universal": "^1.0.8",
"proxyquireify": "^3.2.1",
"puppeteer": "^1.4.0",
"rimraf": "^2.5.3",
"rollup": "^0.59.3",
"rollup-plugin-commonjs": "^9.1.3",
Expand Down
1 change: 1 addition & 0 deletions test/es2015/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
parserOptions:
sourceType: module
ecmaVersion: 2017
71 changes: 71 additions & 0 deletions test/es2015/check-esm-bundle-is-runnable.js
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);

0 comments on commit 19705f8

Please sign in to comment.