From f15befd50f3d3d371f692d41815f68311082390d Mon Sep 17 00:00:00 2001 From: Phillip Johnsen Date: Sat, 6 Feb 2021 20:51:45 +0100 Subject: [PATCH] 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. --- .travis.yml | 2 +- test/helper.js | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 33fd94e6a..a81a06dcd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: diff --git a/test/helper.js b/test/helper.js index f89a5f6f7..aa9f83dd3 100644 --- a/test/helper.js +++ b/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');