Skip to content

Commit

Permalink
Avoid use of esm when running tests on legacy Node.js versions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
phillipj committed Mar 5, 2021
1 parent d4a5042 commit 69bf4bd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion test/cli-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/helper.js
Original file line number Diff line number Diff line change
@@ -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');

0 comments on commit 69bf4bd

Please sign in to comment.