From 02c9ef1bf0c5f4fcef7fc3f7de559566be3bc66d Mon Sep 17 00:00:00 2001 From: Phillip Johnsen Date: Sat, 6 Feb 2021 19:30:53 +0100 Subject: [PATCH] Avoid use of `esm` when running tests on legacy Node.js versions Because `esm` does not support legacy versions of Node.js, at least not below Node.js 6 as of writing this, we have to avoid using that whenever tests are running on legacy versions. But now that the source code (`mustache.js`) is written in ESM syntax, how on earth are we going to run tests on these legacy versions of Node.js? We gotta run the build step first, so that we end up with a `mustache.js` file in CJS, or strictly speaking it will be UMD. That's kinda pain in the backside isn't it? Yes, but running tests on legacy versions are not meant to be done locally, but rather in CI. That means we can easily automate the flow of (1) building the source code before (2) starting the test suite. For our futureselves, if we want to stop running tests on legacy versions of Node.js; the changes introduces in this commit, could be removed completely. --- package.json | 2 +- test/cli-test.js | 3 ++- test/helper.js | 8 ++++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 71312e0..48d672c 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "build": "cp mustache.js mustache.mjs && rollup mustache.mjs --file mustache.js --format umd --name Mustache && uglifyjs mustache.js > mustache.min.js", "test": "npm run test-lint && npm run test-unit", "test-lint": "eslint mustache.js bin/mustache test/**/*.js", - "test-unit": "mocha --reporter spec --require esm test/*-test.js", + "test-unit": "mocha --reporter spec test/*-test.js", "test-render": "mocha --reporter spec test/render-test", "pre-test-browser": "node test/create-browser-suite.js", "test-browser": "npm run pre-test-browser && zuul -- test/context-test.js test/parse-test.js test/scanner-test.js test/render-test-browser.js", diff --git a/test/cli-test.js b/test/cli-test.js index 6f7d2f8..cfaabdd 100644 --- a/test/cli-test.js +++ b/test/cli-test.js @@ -9,7 +9,8 @@ var cliPartialsTxt = path.resolve(_files, 'cli_with_partials.txt'); var moduleVersion = require('../package').version; function changeForOS (command) { - command = command.replace('bin/mustache', 'node --require esm bin/mustache') + var requireFlag = !isLegacyNodeVersion ? '--require esm' : ''; + command = command.replace('bin/mustache', 'node ' + requireFlag + ' bin/mustache') if (process.platform === 'win32') { return command diff --git a/test/helper.js b/test/helper.js index 2988766..f89a5f6 100644 --- a/test/helper.js +++ b/test/helper.js @@ -1,4 +1,12 @@ var chai = require('chai'); +var nodejsMajorVersion = Number(process.versions.node.split(".")[0]); + +isLegacyNodeVersion = !(nodejsMajorVersion >= 10); + +if (!isLegacyNodeVersion) { + require = require("esm")(module); +} + assert = chai.assert; chai.should(); Mustache = require('../mustache');