Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Build ESM -> CJS before running tests in browsers via Saucelabs
Noteworthy tweak is the fact that it seems `zuul` are eagerly loading
all `require('whatever-package-or-path')` it sees before pushing the
contents to Saucelabs.

That was troublesome because we don't want the `esm` package to be
loaded when running browser tests. But it was, even though we loaded
`esm` conditionally after checking the current Node.js version used.

The trick was to not use `require('esm')` but `module.require('esm')`
instead, to fool the assumed look-out for `require` somewhere inside
`zuul`'s code.
  • Loading branch information
phillipj committed Mar 5, 2021
1 parent 3e29d67 commit f15befd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -2,7 +2,7 @@ language: node_js
node_js:
- 8
script:
- npm test
- npm run build
- "test $TRAVIS_PULL_REQUEST != 'false' || test $TRAVIS_NODE_VERSION != '8' || npm run test-browser"
env:
global:
Expand Down
16 changes: 11 additions & 5 deletions test/helper.js
@@ -1,12 +1,18 @@
var chai = require('chai');
var nodejsMajorVersion = Number(process.versions.node.split(".")[0]);
var isRunningInNode = process !== undefined && process.versions.node !== undefined;

isLegacyNodeVersion = !(nodejsMajorVersion >= 10);
if (isRunningInNode) {
var nodejsMajorVersion = Number(process.versions.node.split('.')[0]);
isLegacyNodeVersion = !(nodejsMajorVersion >= 10);

if (!isLegacyNodeVersion) {
require = require("esm")(module);
if (!isLegacyNodeVersion) {
// The `zuul` package we use to run tests in browsers via Saucelabs eagerly loads all
// packages it sees being used via `require()`. Because we don't want the `esm` package
// to be loaded when running browser tests, we refer to `require()` via `module.require()`
// because that avoid the mentioned eager loading
module.require = module.require('esm')(module);
}
}

assert = chai.assert;
chai.should();
Mustache = require('../mustache');

0 comments on commit f15befd

Please sign in to comment.