Skip to content

Commit

Permalink
Added tests for Promise/async support in renderFile
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Jan 6, 2018
1 parent c04ec96 commit 7c77ceb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
14 changes: 12 additions & 2 deletions lib/ejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ exports.fileLoader = fs.readFileSync;

exports.localsName = _DEFAULT_LOCALS_NAME;

/**
* Promise implementation -- defaults to the native implementation if available
* This is mostly just for testability
*
* @type {Function}
* @public
*/

exports.promiseImpl = (new Function('return this;'))().Promise;

/**
* Get the path to the included file from the parent file path and the
* specified path.
Expand Down Expand Up @@ -221,8 +231,8 @@ function handleCache(options, template) {
function tryHandleCache(options, data, cb) {
var result;
if (!cb) {
if (typeof Promise == 'function') {
return new Promise(function (resolve, reject) { // eslint-disable-line no-undef
if (typeof exports.promiseImpl == 'function') {
return new exports.promiseImpl(function (resolve, reject) {
try {
result = handleCache(options)(data);
resolve(result);
Expand Down
47 changes: 46 additions & 1 deletion test/ejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ suite('ejs.render(str, data, opts)', function () {

});

suite('ejs.renderFile(path, [data], [options], fn)', function () {
suite('ejs.renderFile(path, [data], [options], [fn])', function () {
test('render a file', function(done) {
ejs.renderFile('test/fixtures/para.ejs', function(err, html) {
if (err) {
Expand All @@ -303,6 +303,51 @@ suite('ejs.renderFile(path, [data], [options], fn)', function () {
});
});

test('Promise support', function(done) {
var AsyncCtor;
var func;
function checkResult(html) {
assert.equal(html, '<p>hey</p>\n');
}
// Environments without Promise support -- should throw
// when no callback provided
function checkNoPromise() {
delete ejs.promiseImpl;
assert.throws(function () {
ejs.renderFile('test/fixtures/para.ejs');
});
ejs.promiseImpl = global.Promise;
done();
}

// Check for async/await support -- have to use eval for check because
// envs without async will break at parsing step
try {
eval('AsyncCtor = (async function () {}).constructor;');
}
catch (e) {
// No-op
}

// Both async and Promise -- in both cases, also check the call
// correctly throws in non-Promise envs if no callback provided
// -------------------
// Async support -- have to use eval for constructing async func for
// same reasons as above
if (AsyncCtor) {
func = new AsyncCtor('ejs', 'checkResult', 'checkNoPromise',
"let res = await ejs.renderFile('test/fixtures/para.ejs'); checkResult(res); checkNoPromise();");
func(ejs, checkResult, checkNoPromise);
}
// Ordinary Promise support
else {
ejs.renderFile('test/fixtures/para.ejs').then(function (res) {
checkResult(res);
checkNoPromise();
});
}
});

test('accept locals', function(done) {
var data = {name: 'fonebone'};
var options = {delimiter: '$'};
Expand Down

0 comments on commit 7c77ceb

Please sign in to comment.