diff --git a/jakefile.js b/jakefile.cjs similarity index 100% rename from jakefile.js rename to jakefile.cjs diff --git a/lib/esm/package.json b/lib/esm/package.json deleted file mode 100644 index 3dbc1ca5..00000000 --- a/lib/esm/package.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "type": "module" -} diff --git a/package.json b/package.json index a4ea5c6b..d82074b9 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "version": "4.0.0", "author": "Matthew Eernisse ", "license": "Apache-2.0", + "type": "module", "bin": { "ejs": "./bin/cli.js" }, @@ -47,7 +48,7 @@ "npm": ">=10" }, "scripts": { - "build": "jake build", - "test": "jake test" + "build": "jake --jakefile=jakefile.cjs build", + "test": "jake --jakefile=jakefile.cjs test" } } diff --git a/test/cli.js b/test/cli.js index 31c33dc1..2fda4bc1 100644 --- a/test/cli.js +++ b/test/cli.js @@ -1,7 +1,7 @@ -let exec = require('child_process').execSync; -let fs = require('fs'); -let path = require('path'); -let assert = require('assert'); +import { execSync as exec } from 'child_process'; +import { readFileSync } from 'fs'; +import { join } from 'path'; +import { equal } from 'assert'; let os = process.platform !== 'win32' ? '' : 'node '; let lf = process.platform !== 'win32' ? '\n' : '\r\n'; @@ -11,55 +11,55 @@ function run(cmd) { suite('cli', function () { test('rendering, custom delimiter, passed data', function () { - let x = path.join('./bin/cli.js'); - let u = path.join('./test/fixtures/user.ejs'); + let x = join('./bin/cli.js'); + let u = join('./test/fixtures/user.ejs'); let o = run(os+x+' -m $ '+u+' name=foo'); - assert.equal(o, '

foo

'+lf); + equal(o, '

foo

'+lf); }); test('rendering, custom delimiter, data from file with -f', function () { - let x = path.join('./bin/cli.js'); - let u = path.join('./test/fixtures/user.ejs'); + let x = join('./bin/cli.js'); + let u = join('./test/fixtures/user.ejs'); let o = run(os+x+' -m $ -f ./test/fixtures/user_data.json '+u); - assert.equal(o, '

zerb

'+lf); + equal(o, '

zerb

'+lf); }); test('rendering, custom delimiter, data from CLI arg with -i', function () { - let x = path.join('./bin/cli.js'); - let u = path.join('./test/fixtures/user.ejs'); + let x = join('./bin/cli.js'); + let u = join('./test/fixtures/user.ejs'); let o = run(os+x+' -m $ -i %7B%22name%22%3A%20%22foo%22%7D '+u); - assert.equal(o, '

foo

'+lf); + equal(o, '

foo

'+lf); }); test('rendering, custom delimiter, data from stdin / pipe', function () { if ( process.platform !== 'win32' ) { let o = run('cat ./test/fixtures/user_data.json | ./bin/cli.js -m $ ./test/fixtures/user.ejs'); - assert.equal(o, '

zerb

\n'); + equal(o, '

zerb

\n'); } // does not work on windows... }); test('rendering, custom delimiter, passed data overrides file', function () { - let x = path.join('./bin/cli.js'); - let f = path.join('./test/fixtures/user_data.json'); - let g = path.join('./test/fixtures/user.ejs'); + let x = join('./bin/cli.js'); + let f = join('./test/fixtures/user_data.json'); + let g = join('./test/fixtures/user.ejs'); let o = run(os+x+' -m $ -f '+f+' '+g+' name=frang'); - assert.equal(o, '

frang

'+lf); + equal(o, '

frang

'+lf); }); test('rendering, remove whitespace option (hyphen case)', function () { - let x = path.join('./bin/cli.js'); - let f = path.join('./test/fixtures/rmWhitespace.ejs'); + let x = join('./bin/cli.js'); + let f = join('./test/fixtures/rmWhitespace.ejs'); let o = run(os+x+' --rm-whitespace '+f); - let c = fs.readFileSync('test/fixtures/rmWhitespace.html', 'utf-8'); - assert.equal(o.replace(/\n/g, lf), c); + let c = readFileSync('test/fixtures/rmWhitespace.html', 'utf-8'); + equal(o.replace(/\n/g, lf), c); }); test('relative path in nested include', function () { - let x = path.join('./bin/cli.js'); - let u = path.join('test/fixtures/include-simple.ejs'); + let x = join('./bin/cli.js'); + let u = join('test/fixtures/include-simple.ejs'); let o = run(os+x+' '+u); - let c = fs.readFileSync('test/fixtures/include-simple.html', 'utf-8'); - assert.equal(o, c); + let c = readFileSync('test/fixtures/include-simple.html', 'utf-8'); + equal(o, c); }); }); diff --git a/test/ejs.js b/test/ejs.js index 62581d54..8c1527a8 100755 --- a/test/ejs.js +++ b/test/ejs.js @@ -4,15 +4,15 @@ * Module dependencies. */ -var ejs = require('..'); -var fs = require('fs'); -var assert = require('assert'); -var path = require('path'); -var LRU = require('lru-cache'); +import ejs from '../lib/esm/ejs.js'; +import { mkdirSync, readFileSync, writeFileSync } from 'fs'; +import { equal, notEqual, ifError, ok, throws } from 'assert'; +import { join } from 'path'; +import LRU from 'lru-cache'; let lf = process.platform !== 'win32' ? '\n' : '\r\n'; try { - fs.mkdirSync(__dirname + '/tmp'); + mkdirSync(__dirname + '/tmp'); } catch (ex) { if (ex.code !== 'EEXIST') { throw ex; @@ -63,12 +63,12 @@ try { suite('ejs.compile(str, options)', function () { test('compile to a function', function () { var fn = ejs.compile('

yay

'); - assert.equal(fn(), '

yay

'); + equal(fn(), '

yay

'); }); test('empty input works', function () { var fn = ejs.compile(''); - assert.equal(fn(), ''); + equal(fn(), ''); }); test('throw if there are syntax errors', function () { @@ -76,13 +76,13 @@ suite('ejs.compile(str, options)', function () { ejs.compile(fixture('fail.ejs')); } catch (err) { - assert.ok(err.message.indexOf('compiling ejs') > -1); + ok(err.message.indexOf('compiling ejs') > -1); try { ejs.compile(fixture('fail.ejs'), {filename: 'fail.ejs'}); } catch (err) { - assert.ok(err.message.indexOf('fail.ejs') > -1); + ok(err.message.indexOf('fail.ejs') > -1); return; } } @@ -92,29 +92,29 @@ suite('ejs.compile(str, options)', function () { test('allow customizing delimiter local var', function () { var fn; fn = ejs.compile('

', {delimiter: '?'}); - assert.equal(fn({name: 'geddy'}), '

geddy

'); + equal(fn({name: 'geddy'}), '

geddy

'); fn = ejs.compile('

<:= name :>

', {delimiter: ':'}); - assert.equal(fn({name: 'geddy'}), '

geddy

'); + equal(fn({name: 'geddy'}), '

geddy

'); fn = ejs.compile('

<$= name $>

', {delimiter: '$'}); - assert.equal(fn({name: 'geddy'}), '

geddy

'); + equal(fn({name: 'geddy'}), '

geddy

'); }); test('allow customizing open and close delimiters', function() { var fn; fn = ejs.compile('

[#= name #]

', {delimiter: '#', openDelimiter: '[', closeDelimiter: ']'}); - assert.equal(fn({name: 'geddy'}), '

geddy

'); + equal(fn({name: 'geddy'}), '

geddy

'); }); test('default to using ejs.delimiter', function () { var fn; ejs.delimiter = '&'; fn = ejs.compile('

<&= name &>

'); - assert.equal(fn({name: 'geddy'}), '

geddy

'); + equal(fn({name: 'geddy'}), '

geddy

'); fn = ejs.compile('

<|= name |>

', {delimiter: '|'}); - assert.equal(fn({name: 'geddy'}), '

geddy

'); + equal(fn({name: 'geddy'}), '

geddy

'); delete ejs.delimiter; }); @@ -125,17 +125,17 @@ suite('ejs.compile(str, options)', function () { return !str ? '' : str.toUpperCase(); }; fn = ejs.compile('HELLO <%= name %>', {escape: customEscape}); - assert.equal(fn({name: 'world'}), 'HELLO WORLD'); + equal(fn({name: 'world'}), 'HELLO WORLD'); }); test('strict mode works', function () { - assert.equal(ejs.render(fixture('strict.ejs'), {}, {strict: true}), 'true'); + equal(ejs.render(fixture('strict.ejs'), {}, {strict: true}), 'true'); }); test('destructuring works in strict mode as an alternative to `with`', function () { var locals = Object.create(null); locals.foo = 'bar'; - assert.equal(ejs.render(fixture('strict-destructuring.ejs'), locals, { + equal(ejs.render(fixture('strict-destructuring.ejs'), locals, { strict: true, destructuredLocals: Object.keys(locals), _with: true @@ -150,7 +150,7 @@ suite('ejs.compile(str, options)', function () { async: true, destructuredLocals: Object.keys(locals), }).then(function (value) { - assert.equal(value, locals.foo); + equal(value, locals.foo); }).then( () => done(), e => done(e) @@ -160,7 +160,7 @@ suite('ejs.compile(str, options)', function () { testAsync('can compile to an async function', function (done) { ejs.compile('<%= await "Hi" %>', {async: true})().then(function (value) { try { - assert.equal(value, 'Hi'); + equal(value, 'Hi'); } catch (e) { done(e); return; @@ -176,7 +176,7 @@ suite('ejs.compile(str, options)', function () { } catch (err) { if (err instanceof SyntaxError) { - assert.ok(err.message.indexOf('async: true') > -1); + ok(err.message.indexOf('async: true') > -1); return done(); } else { throw err; @@ -192,14 +192,14 @@ suite('ejs.compile(str, options)', function () { filename: 'foo.ejs' }); - assert.ok(func.name === 'foo'); + ok(func.name === 'foo'); return done(); }); testFuncName('Compiled function name defaults to "anonymous" when `filename` is unspecified', function (done) { var func = ejs.compile('<%= "Foo" %>'); - assert.ok(func.name === 'anonymous'); + ok(func.name === 'anonymous'); return done(); }); }); @@ -214,7 +214,7 @@ suite('client mode', function () { str = fn.toString(); if (!process.env.running_under_istanbul) { eval('var preFn = ' + str); - assert.equal(preFn({foo: 'bar'}), '

bar

'); + equal(preFn({foo: 'bar'}), '

bar

'); } }); @@ -226,7 +226,7 @@ suite('client mode', function () { str = fn.toString(); if (!process.env.running_under_istanbul) { eval('var preFn = ' + str); - assert.equal(preFn(), '

foo

'); + equal(preFn(), '

foo

'); } }); @@ -250,12 +250,12 @@ suite('client mode', function () { str = fn.toString(); if (!process.env.running_under_istanbul) { eval('var preFn = ' + str); - assert.equal(preFn({name: 'world'}), 'HELLO WORLD'); + equal(preFn({name: 'world'}), 'HELLO WORLD'); } }); test('escape filename in errors in client mode', function () { - assert.throws(function () { + throws(function () { var fn = ejs.compile('<% throw new Error("whoops"); %>', {client: true, filename: '