Skip to content

Commit 69bf4bd

Browse files
committed
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.
1 parent d4a5042 commit 69bf4bd

3 files changed

Lines changed: 11 additions & 2 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"build": "cp mustache.js mustache.mjs && rollup mustache.mjs --file mustache.js --format umd --name Mustache && uglifyjs mustache.js > mustache.min.js",
3232
"test": "npm run test-lint && npm run test-unit",
3333
"test-lint": "eslint mustache.js bin/mustache test/**/*.js",
34-
"test-unit": "mocha --reporter spec --require esm test/*-test.js",
34+
"test-unit": "mocha --reporter spec test/*-test.js",
3535
"test-render": "mocha --reporter spec test/render-test",
3636
"pre-test-browser": "node test/create-browser-suite.js",
3737
"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",

test/cli-test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ var cliPartialsTxt = path.resolve(_files, 'cli_with_partials.txt');
99
var moduleVersion = require('../package').version;
1010

1111
function changeForOS (command) {
12-
command = command.replace('bin/mustache', 'node --require esm bin/mustache')
12+
var requireFlag = !isLegacyNodeVersion ? '--require esm' : '';
13+
command = command.replace('bin/mustache', 'node ' + requireFlag + ' bin/mustache')
1314

1415
if (process.platform === 'win32') {
1516
return command

test/helper.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
var chai = require('chai');
2+
var nodejsMajorVersion = Number(process.versions.node.split(".")[0]);
3+
4+
isLegacyNodeVersion = !(nodejsMajorVersion >= 10);
5+
6+
if (!isLegacyNodeVersion) {
7+
require = require("esm")(module);
8+
}
9+
210
assert = chai.assert;
311
chai.should();
412
Mustache = require('../mustache');

0 commit comments

Comments
 (0)