Convert tests to vitest (#2127)
* Convert tests to vitest * fix git tests * Cleanup * Convert ignore comments * address code review comments
This commit is contained in:
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
// Pre-setup for browser tests. Must run before the main setup file
|
||||
// imports the Handlebars library, so that noConflict() captures this value.
|
||||
globalThis.Handlebars = 'no-conflict';
|
||||
|
||||
// Polyfill Node.js 'global' for specs that reference it at module level
|
||||
globalThis.global = globalThis;
|
||||
Vendored
+27
@@ -0,0 +1,27 @@
|
||||
import './common.js';
|
||||
import Handlebars from '../../lib/handlebars.js';
|
||||
|
||||
globalThis.Handlebars = Handlebars;
|
||||
|
||||
globalThis.CompilerContext = {
|
||||
browser: true,
|
||||
|
||||
compile: function (template, options) {
|
||||
var templateSpec = handlebarsEnv.precompile(template, options);
|
||||
return handlebarsEnv.template(safeEval(templateSpec));
|
||||
},
|
||||
compileWithPartial: function (template, options) {
|
||||
return handlebarsEnv.compile(template, options);
|
||||
},
|
||||
};
|
||||
|
||||
function safeEval(templateSpec) {
|
||||
/* eslint-disable no-eval, no-console */
|
||||
try {
|
||||
return eval('(' + templateSpec + ')');
|
||||
} catch (err) {
|
||||
console.error(templateSpec);
|
||||
throw err;
|
||||
}
|
||||
/* eslint-enable no-eval, no-console */
|
||||
}
|
||||
Vendored
-11
@@ -3,20 +3,9 @@ require('./common');
|
||||
var fs = require('fs'),
|
||||
vm = require('vm');
|
||||
|
||||
var chai = require('chai');
|
||||
var dirtyChai = require('dirty-chai');
|
||||
|
||||
chai.use(dirtyChai);
|
||||
global.expect = chai.expect;
|
||||
|
||||
global.sinon = require('sinon');
|
||||
|
||||
global.Handlebars = 'no-conflict';
|
||||
|
||||
var filename = 'dist/handlebars.js';
|
||||
if (global.minimizedTest) {
|
||||
filename = 'dist/handlebars.min.js';
|
||||
}
|
||||
var distHandlebars = fs.readFileSync(
|
||||
require.resolve('../../' + filename),
|
||||
'utf-8'
|
||||
|
||||
Vendored
+33
-48
@@ -1,22 +1,4 @@
|
||||
var global = (function () {
|
||||
return this;
|
||||
})();
|
||||
|
||||
var AssertError;
|
||||
if (Error.captureStackTrace) {
|
||||
AssertError = function AssertError(message, caller) {
|
||||
Error.prototype.constructor.call(this, message);
|
||||
this.message = message;
|
||||
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this, caller || AssertError);
|
||||
}
|
||||
};
|
||||
|
||||
AssertError.prototype = new Error();
|
||||
} else {
|
||||
AssertError = Error;
|
||||
}
|
||||
var global = globalThis;
|
||||
|
||||
/**
|
||||
* @deprecated Use "expectTemplate(template)...toCompileTo(output)" instead
|
||||
@@ -33,15 +15,10 @@ global.shouldCompileToWithPartials = function shouldCompileToWithPartials(
|
||||
hashOrArray,
|
||||
partials,
|
||||
expected,
|
||||
message
|
||||
message // eslint-disable-line no-unused-vars
|
||||
) {
|
||||
var result = compileWithPartials(string, hashOrArray, partials);
|
||||
if (result !== expected) {
|
||||
throw new AssertError(
|
||||
"'" + result + "' should === '" + expected + "': " + message,
|
||||
shouldCompileToWithPartials
|
||||
);
|
||||
}
|
||||
expect(result).toBe(expected);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -76,21 +53,15 @@ global.compileWithPartials = function (string, hashOrArray, partials) {
|
||||
};
|
||||
|
||||
/**
|
||||
* @deprecated Use chai's expect-style API instead (`expect(actualValue).to.equal(expectedValue)`)
|
||||
* @see https://www.chaijs.com/api/bdd/
|
||||
* @deprecated Use vitest's expect API instead
|
||||
*/
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
global.equals = global.equal = function equals(a, b, msg) {
|
||||
if (a !== b) {
|
||||
throw new AssertError(
|
||||
"'" + a + "' should === '" + b + "'" + (msg ? ': ' + msg : ''),
|
||||
equals
|
||||
);
|
||||
}
|
||||
expect(a).toBe(b);
|
||||
};
|
||||
|
||||
/**
|
||||
* @deprecated Use chai's expect-style API instead (`expect(actualValue).to.equal(expectedValue)`)
|
||||
* @see https://www.chaijs.com/api/bdd/#method_throw
|
||||
* @deprecated Use vitest's expect API instead
|
||||
*/
|
||||
global.shouldThrow = function (callback, type, msg) {
|
||||
var failed;
|
||||
@@ -99,25 +70,24 @@ global.shouldThrow = function (callback, type, msg) {
|
||||
failed = true;
|
||||
} catch (caught) {
|
||||
if (type && !(caught instanceof type)) {
|
||||
throw new AssertError('Type failure: ' + caught);
|
||||
throw new Error('Type failure: ' + caught);
|
||||
}
|
||||
if (
|
||||
msg &&
|
||||
!(msg.test ? msg.test(caught.message) : msg === caught.message)
|
||||
) {
|
||||
throw new AssertError(
|
||||
throw new Error(
|
||||
'Throw mismatch: Expected ' +
|
||||
caught.message +
|
||||
' to match ' +
|
||||
msg +
|
||||
'\n\n' +
|
||||
caught.stack,
|
||||
shouldThrow
|
||||
caught.stack
|
||||
);
|
||||
}
|
||||
}
|
||||
if (failed) {
|
||||
throw new AssertError('It failed to throw', shouldThrow);
|
||||
throw new Error('It failed to throw');
|
||||
}
|
||||
};
|
||||
|
||||
@@ -200,18 +170,29 @@ HandlebarsTestBench.prototype.withMessage = function (message) {
|
||||
};
|
||||
|
||||
HandlebarsTestBench.prototype.toCompileTo = function (expectedOutputAsString) {
|
||||
expect(this._compileAndExecute()).to.equal(
|
||||
expectedOutputAsString,
|
||||
this.message
|
||||
);
|
||||
expect(this._compileAndExecute()).toBe(expectedOutputAsString);
|
||||
};
|
||||
|
||||
// see chai "to.throw" (https://www.chaijs.com/api/bdd/#method_throw)
|
||||
HandlebarsTestBench.prototype.toThrow = function (errorLike, errMsgMatcher) {
|
||||
var self = this;
|
||||
expect(function () {
|
||||
var caught;
|
||||
try {
|
||||
self._compileAndExecute();
|
||||
}).to.throw(errorLike, errMsgMatcher, this.message);
|
||||
} catch (e) {
|
||||
caught = e;
|
||||
}
|
||||
|
||||
expect(caught).toBeDefined();
|
||||
|
||||
if (typeof errorLike === 'function') {
|
||||
expect(caught).toBeInstanceOf(errorLike);
|
||||
if (errMsgMatcher) {
|
||||
expect(caught.message).toMatch(errMsgMatcher);
|
||||
}
|
||||
} else if (errorLike) {
|
||||
// errorLike is a string or regex message matcher (single-argument form)
|
||||
expect(caught.message).toMatch(errorLike);
|
||||
}
|
||||
};
|
||||
|
||||
HandlebarsTestBench.prototype._compileAndExecute = function () {
|
||||
@@ -237,3 +218,7 @@ HandlebarsTestBench.prototype._combineRuntimeOptions = function () {
|
||||
combinedRuntimeOptions.decorators = this.decorators;
|
||||
return combinedRuntimeOptions;
|
||||
};
|
||||
|
||||
beforeEach(function () {
|
||||
global.handlebarsEnv = Handlebars.create();
|
||||
});
|
||||
|
||||
Vendored
-8
@@ -1,13 +1,5 @@
|
||||
require('./common');
|
||||
|
||||
var chai = require('chai');
|
||||
var dirtyChai = require('dirty-chai');
|
||||
|
||||
chai.use(dirtyChai);
|
||||
global.expect = chai.expect;
|
||||
|
||||
global.sinon = require('sinon');
|
||||
|
||||
global.Handlebars = require('../../lib');
|
||||
|
||||
global.CompilerContext = {
|
||||
|
||||
Vendored
-63
@@ -1,63 +0,0 @@
|
||||
/* eslint-disable no-console */
|
||||
var fs = require('fs'),
|
||||
Mocha = require('mocha'),
|
||||
path = require('path');
|
||||
|
||||
var errors = 0,
|
||||
testDir = path.dirname(__dirname),
|
||||
grep = process.argv[2];
|
||||
|
||||
// Lazy hack, but whatever
|
||||
if (grep === '--min') {
|
||||
global.minimizedTest = true;
|
||||
grep = undefined;
|
||||
}
|
||||
|
||||
var files = fs
|
||||
.readdirSync(testDir)
|
||||
.filter(function (name) {
|
||||
return /.*\.js$/.test(name);
|
||||
})
|
||||
.map(function (name) {
|
||||
return testDir + path.sep + name;
|
||||
});
|
||||
|
||||
if (global.minimizedTest) {
|
||||
run('./runtime', function () {
|
||||
run('./browser', function () {
|
||||
/* eslint-disable no-process-exit */
|
||||
process.exit(errors);
|
||||
/* eslint-enable no-process-exit */
|
||||
});
|
||||
});
|
||||
} else {
|
||||
run('./runtime', function () {
|
||||
run('./browser', function () {
|
||||
run('./node', function () {
|
||||
/* eslint-disable no-process-exit */
|
||||
process.exit(errors);
|
||||
/* eslint-enable no-process-exit */
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function run(env, callback) {
|
||||
var mocha = new Mocha();
|
||||
mocha.ui('bdd');
|
||||
mocha.files = files.slice();
|
||||
if (grep) {
|
||||
mocha.grep(grep);
|
||||
}
|
||||
|
||||
files.forEach(function (name) {
|
||||
delete require.cache[name];
|
||||
});
|
||||
|
||||
console.log('Running env: ' + env);
|
||||
require(env);
|
||||
mocha.run(function (errorCount) {
|
||||
errors += errorCount;
|
||||
callback();
|
||||
});
|
||||
}
|
||||
Vendored
-73
@@ -1,73 +0,0 @@
|
||||
require('./common');
|
||||
|
||||
var fs = require('fs'),
|
||||
vm = require('vm');
|
||||
|
||||
var chai = require('chai');
|
||||
var dirtyChai = require('dirty-chai');
|
||||
|
||||
chai.use(dirtyChai);
|
||||
global.expect = chai.expect;
|
||||
|
||||
global.sinon = require('sinon');
|
||||
|
||||
global.Handlebars = 'no-conflict';
|
||||
|
||||
var filename = 'dist/handlebars.runtime.js';
|
||||
if (global.minimizedTest) {
|
||||
filename = 'dist/handlebars.runtime.min.js';
|
||||
}
|
||||
vm.runInThisContext(
|
||||
fs.readFileSync(__dirname + '/../../' + filename),
|
||||
filename
|
||||
);
|
||||
|
||||
var parse = require('@handlebars/parser').parse;
|
||||
var compiler = require('../../dist/cjs/handlebars/compiler/compiler');
|
||||
var JavaScriptCompiler = require('../../dist/cjs/handlebars/compiler/javascript-compiler');
|
||||
|
||||
global.CompilerContext = {
|
||||
browser: true,
|
||||
|
||||
compile: function (template, options) {
|
||||
// Hack the compiler on to the environment for these specific tests
|
||||
handlebarsEnv.precompile = function (
|
||||
precompileTemplate,
|
||||
precompileOptions
|
||||
) {
|
||||
return compiler.precompile(
|
||||
precompileTemplate,
|
||||
precompileOptions,
|
||||
handlebarsEnv
|
||||
);
|
||||
};
|
||||
handlebarsEnv.parse = parse;
|
||||
handlebarsEnv.Compiler = compiler.Compiler;
|
||||
handlebarsEnv.JavaScriptCompiler = JavaScriptCompiler;
|
||||
|
||||
var templateSpec = handlebarsEnv.precompile(template, options);
|
||||
return handlebarsEnv.template(safeEval(templateSpec));
|
||||
},
|
||||
compileWithPartial: function (template, options) {
|
||||
// Hack the compiler on to the environment for these specific tests
|
||||
handlebarsEnv.compile = function (compileTemplate, compileOptions) {
|
||||
return compiler.compile(compileTemplate, compileOptions, handlebarsEnv);
|
||||
};
|
||||
handlebarsEnv.parse = parse;
|
||||
handlebarsEnv.Compiler = compiler.Compiler;
|
||||
handlebarsEnv.JavaScriptCompiler = JavaScriptCompiler;
|
||||
|
||||
return handlebarsEnv.compile(template, options);
|
||||
},
|
||||
};
|
||||
|
||||
function safeEval(templateSpec) {
|
||||
/* eslint-disable no-eval, no-console */
|
||||
try {
|
||||
return eval('(' + templateSpec + ')');
|
||||
} catch (err) {
|
||||
console.error(templateSpec);
|
||||
throw err;
|
||||
}
|
||||
/* eslint-enable no-eval, no-console */
|
||||
}
|
||||
Reference in New Issue
Block a user