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:
@@ -1,5 +1,10 @@
|
||||
module.exports = {
|
||||
env: {
|
||||
mocha: true,
|
||||
globals: {
|
||||
describe: true,
|
||||
it: true,
|
||||
expect: true,
|
||||
beforeEach: true,
|
||||
afterEach: true,
|
||||
vi: true,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -0,0 +1,256 @@
|
||||
const childProcess = require('child_process');
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
|
||||
const testCases = [
|
||||
{
|
||||
binInputParameters: ['-a', 'spec/artifacts/empty.handlebars'],
|
||||
outputLocation: 'stdout',
|
||||
expectedOutputSpec: './spec/expected/empty.amd.js',
|
||||
},
|
||||
{
|
||||
binInputParameters: [
|
||||
'-a',
|
||||
'-f',
|
||||
'TEST_OUTPUT',
|
||||
'spec/artifacts/empty.handlebars',
|
||||
],
|
||||
outputLocation: 'TEST_OUTPUT',
|
||||
expectedOutputSpec: './spec/expected/empty.amd.js',
|
||||
},
|
||||
{
|
||||
binInputParameters: [
|
||||
'-a',
|
||||
'-n',
|
||||
'CustomNamespace.templates',
|
||||
'spec/artifacts/empty.handlebars',
|
||||
],
|
||||
outputLocation: 'stdout',
|
||||
expectedOutputSpec: './spec/expected/empty.amd.namespace.js',
|
||||
},
|
||||
{
|
||||
binInputParameters: [
|
||||
'-a',
|
||||
'--namespace',
|
||||
'CustomNamespace.templates',
|
||||
'spec/artifacts/empty.handlebars',
|
||||
],
|
||||
outputLocation: 'stdout',
|
||||
expectedOutputSpec: './spec/expected/empty.amd.namespace.js',
|
||||
},
|
||||
{
|
||||
binInputParameters: ['-a', '-s', 'spec/artifacts/empty.handlebars'],
|
||||
outputLocation: 'stdout',
|
||||
expectedOutputSpec: './spec/expected/empty.amd.simple.js',
|
||||
},
|
||||
{
|
||||
binInputParameters: ['-a', '-m', 'spec/artifacts/empty.handlebars'],
|
||||
outputLocation: 'stdout',
|
||||
expectedOutputSpec: './spec/expected/empty.amd.min.js',
|
||||
},
|
||||
{
|
||||
binInputParameters: [
|
||||
'spec/artifacts/known.helpers.handlebars',
|
||||
'-a',
|
||||
'-k',
|
||||
'someHelper',
|
||||
'-k',
|
||||
'anotherHelper',
|
||||
'-o',
|
||||
],
|
||||
outputLocation: 'stdout',
|
||||
expectedOutputSpec: './spec/expected/non.empty.amd.known.helper.js',
|
||||
},
|
||||
{
|
||||
binInputParameters: ['--help'],
|
||||
outputLocation: 'stdout',
|
||||
expectedOutputSpec: './spec/expected/help.menu.txt',
|
||||
},
|
||||
{
|
||||
binInputParameters: ['-v'],
|
||||
outputLocation: 'stdout',
|
||||
expectedOutput: require('../../package.json').version,
|
||||
},
|
||||
{
|
||||
binInputParameters: [
|
||||
'-a',
|
||||
'-e',
|
||||
'hbs',
|
||||
'./spec/artifacts/non.default.extension.hbs',
|
||||
],
|
||||
outputLocation: 'stdout',
|
||||
expectedOutputSpec: './spec/expected/non.default.extension.amd.js',
|
||||
},
|
||||
{
|
||||
binInputParameters: [
|
||||
'-a',
|
||||
'-p',
|
||||
'./spec/artifacts/partial.template.handlebars',
|
||||
],
|
||||
outputLocation: 'stdout',
|
||||
expectedOutputSpec: './spec/expected/partial.template.js',
|
||||
},
|
||||
{
|
||||
binInputParameters: ['spec/artifacts/empty.handlebars', '-c'],
|
||||
outputLocation: 'stdout',
|
||||
expectedOutputSpec: './spec/expected/empty.common.js',
|
||||
},
|
||||
{
|
||||
binInputParameters: [
|
||||
'spec/artifacts/empty.handlebars',
|
||||
'spec/artifacts/empty.handlebars',
|
||||
'-a',
|
||||
'-n',
|
||||
'someNameSpace',
|
||||
],
|
||||
outputLocation: 'stdout',
|
||||
expectedOutputSpec: './spec/expected/namespace.amd.js',
|
||||
},
|
||||
{
|
||||
binInputParameters: [
|
||||
'spec/artifacts/empty.handlebars',
|
||||
'-h',
|
||||
'some-path/',
|
||||
'-a',
|
||||
],
|
||||
outputLocation: 'stdout',
|
||||
expectedOutputSpec: './spec/expected/handlebar.path.amd.js',
|
||||
},
|
||||
{
|
||||
binInputParameters: [
|
||||
'spec/artifacts/partial.template.handlebars',
|
||||
'-r',
|
||||
'spec',
|
||||
'-a',
|
||||
],
|
||||
outputLocation: 'stdout',
|
||||
expectedOutputSpec: './spec/expected/empty.root.amd.js',
|
||||
},
|
||||
{
|
||||
binInputParameters: [
|
||||
'-i',
|
||||
'<div>1</div>',
|
||||
'-i',
|
||||
'<div>2</div>',
|
||||
'-N',
|
||||
'firstTemplate',
|
||||
'-N',
|
||||
'secondTemplate',
|
||||
'-a',
|
||||
],
|
||||
outputLocation: 'stdout',
|
||||
expectedOutputSpec: './spec/expected/empty.name.amd.js',
|
||||
},
|
||||
{
|
||||
binInputParameters: [
|
||||
'-i',
|
||||
'<div>1</div>',
|
||||
'-a',
|
||||
'-m',
|
||||
'-N',
|
||||
'test',
|
||||
'--map',
|
||||
'./spec/tmp/source.map.amd.txt',
|
||||
],
|
||||
outputLocation: 'stdout',
|
||||
expectedOutputSpec: './spec/expected/source.map.amd.js',
|
||||
},
|
||||
{
|
||||
binInputParameters: ['./spec/artifacts/bom.handlebars', '-b', '-a'],
|
||||
outputLocation: 'stdout',
|
||||
expectedOutputSpec: './spec/expected/bom.amd.js',
|
||||
},
|
||||
// Issue #1673
|
||||
{
|
||||
binInputParameters: [
|
||||
'--amd',
|
||||
'--no-amd',
|
||||
'spec/artifacts/empty.handlebars',
|
||||
],
|
||||
outputLocation: 'stdout',
|
||||
expectedOutputSpec: './spec/expected/empty.common.js',
|
||||
},
|
||||
];
|
||||
|
||||
expect.extend({
|
||||
toEqualWithRelaxedSpace(received, expected) {
|
||||
const normalize = (str) =>
|
||||
typeof str === 'string'
|
||||
? str
|
||||
.replace(/\r\n/g, '\n')
|
||||
.split('\n')
|
||||
.map((line) => line.replace(/\s+/g, ' ').trim())
|
||||
.filter((line) => line.length > 0)
|
||||
.join('\n')
|
||||
.trim()
|
||||
: str;
|
||||
|
||||
const normalizedReceived = normalize(received);
|
||||
const normalizedExpected = normalize(expected);
|
||||
const pass = normalizedReceived === normalizedExpected;
|
||||
|
||||
return {
|
||||
pass,
|
||||
message: () =>
|
||||
`Expected output to match with relaxed whitespace.\n\n` +
|
||||
`Expected:\n${normalizedExpected}\n\nReceived:\n${normalizedReceived}`,
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
describe('bin/handlebars', function () {
|
||||
testCases.forEach(
|
||||
(
|
||||
{
|
||||
binInputParameters,
|
||||
outputLocation,
|
||||
expectedOutputSpec,
|
||||
expectedOutput,
|
||||
},
|
||||
index
|
||||
) => {
|
||||
it(`test case ${index}: handlebars ${binInputParameters.join(
|
||||
' '
|
||||
)}`, function () {
|
||||
const stdout = executeBinHandlebars(...binInputParameters);
|
||||
|
||||
if (!expectedOutput && expectedOutputSpec) {
|
||||
expectedOutput = fs.readFileSync(expectedOutputSpec, 'utf-8');
|
||||
}
|
||||
|
||||
const useStdout = outputLocation === 'stdout';
|
||||
const actualOutput = useStdout
|
||||
? stdout
|
||||
: fs.readFileSync(outputLocation, 'utf-8');
|
||||
|
||||
if (!useStdout) {
|
||||
fs.unlinkSync(outputLocation);
|
||||
}
|
||||
|
||||
expect(actualOutput).toEqualWithRelaxedSpace(expectedOutput);
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
// helper functions
|
||||
|
||||
function executeBinHandlebars(...args) {
|
||||
if (os.platform() === 'win32') {
|
||||
// On Windows, the executable handlebars.js file cannot be run directly
|
||||
const nodeJs = process.argv[0];
|
||||
return execFilesSyncUtf8(nodeJs, ['./bin/handlebars.js'].concat(args));
|
||||
}
|
||||
return execFilesSyncUtf8('./bin/handlebars.js', args);
|
||||
}
|
||||
|
||||
function execFilesSyncUtf8(command, args) {
|
||||
const env = process.env;
|
||||
env.PATH = addPathToNodeJs(env.PATH);
|
||||
return childProcess.execFileSync(command, args, { encoding: 'utf-8', env });
|
||||
}
|
||||
|
||||
function addPathToNodeJs(pathEnvironment) {
|
||||
return path.dirname(process.argv0) + path.delimiter + pathEnvironment;
|
||||
}
|
||||
+15
-15
@@ -1,13 +1,9 @@
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
const fs = require('fs-extra');
|
||||
const chai = require('chai');
|
||||
chai.use(require('dirty-chai'));
|
||||
|
||||
const git = require('../util/git');
|
||||
|
||||
const expect = chai.expect;
|
||||
|
||||
const tmpBaseDir = path.join(os.tmpdir(), 'handlebars-task-tests');
|
||||
const tmpDir = path.join(tmpBaseDir, Date.now().toString(36));
|
||||
const remoteDir = path.join(tmpDir, 'remote-repo');
|
||||
@@ -21,6 +17,8 @@ describe('utils/git', function () {
|
||||
process.chdir(tmpDir);
|
||||
await git.git('clone', 'remote-repo', 'clone-repo');
|
||||
process.chdir(cloneDir);
|
||||
await git.git('config', 'user.email', 'test@test.com');
|
||||
await git.git('config', 'user.name', 'Test');
|
||||
});
|
||||
|
||||
async function createRepositoryThatActsAsRemote() {
|
||||
@@ -28,6 +26,8 @@ describe('utils/git', function () {
|
||||
process.chdir(remoteDir);
|
||||
|
||||
await git.git('init');
|
||||
await git.git('config', 'user.email', 'test@test.com');
|
||||
await git.git('config', 'user.name', 'Test');
|
||||
await fs.writeFile('testfile.txt', 'Testfile');
|
||||
await git.add('testfile.txt');
|
||||
await git.commit('commit message');
|
||||
@@ -44,7 +44,7 @@ describe('utils/git', function () {
|
||||
|
||||
const result = await git.remotes();
|
||||
|
||||
expect(result.trim().split('\n')).to.deep.equal([
|
||||
expect(result.trim().split('\n')).toEqual([
|
||||
'origin\thttps://test.org/test (fetch)',
|
||||
'origin\thttps://test.org/test (push)',
|
||||
'second-remote\thttps://test.org/test2 (fetch)',
|
||||
@@ -59,7 +59,7 @@ describe('utils/git', function () {
|
||||
await git.git('branch', 'test2');
|
||||
|
||||
const result = await git.branches();
|
||||
expect(result.trim().split('\n')).to.deep.equal([
|
||||
expect(result.trim().split('\n')).toEqual([
|
||||
'* master',
|
||||
' test',
|
||||
' test2',
|
||||
@@ -72,21 +72,21 @@ describe('utils/git', function () {
|
||||
describe('the "commitInfo"-function', function () {
|
||||
it('should list head and master sha', async function () {
|
||||
const result = await git.commitInfo();
|
||||
expect(result.masterSha).to.equal(result.headSha);
|
||||
expect(result.masterSha).to.match(/^[0-9a-f]+$/);
|
||||
expect(result.headSha).to.match(/^[0-9a-f]+$/);
|
||||
expect(result.masterSha).toBe(result.headSha);
|
||||
expect(result.masterSha).toMatch(/^[0-9a-f]+$/);
|
||||
expect(result.headSha).toMatch(/^[0-9a-f]+$/);
|
||||
});
|
||||
|
||||
it('should have "isMaster=true" if the master branch is checked out', async function () {
|
||||
const result = await git.commitInfo();
|
||||
expect(result.isMaster).to.be.true();
|
||||
expect(result.isMaster).toBe(true);
|
||||
});
|
||||
|
||||
it('should have "isMaster=true" if the current commit is the last commit of the master branch', async function () {
|
||||
await git.git('checkout', '-b', 'new-branch');
|
||||
|
||||
const result = await git.commitInfo();
|
||||
expect(result.isMaster).to.be.true();
|
||||
expect(result.isMaster).toBe(true);
|
||||
});
|
||||
|
||||
it('should have "isMaster=false" if the current commit is NOT the last commit of the master branch', async function () {
|
||||
@@ -96,13 +96,13 @@ describe('utils/git', function () {
|
||||
await git.commit('added new file');
|
||||
|
||||
const result = await git.commitInfo();
|
||||
expect(result.isMaster).to.be.false();
|
||||
expect(result.isMaster).toBe(false);
|
||||
});
|
||||
|
||||
it('should show the current tag', async function () {
|
||||
await git.git('tag', 'test-tag');
|
||||
const result = await git.commitInfo();
|
||||
expect(result.tagName).to.be.equal('test-tag');
|
||||
expect(result.tagName).toBe('test-tag');
|
||||
});
|
||||
|
||||
it('should show a version tag rather than standard tags', async function () {
|
||||
@@ -110,12 +110,12 @@ describe('utils/git', function () {
|
||||
await git.git('tag', 'v1.2');
|
||||
await git.git('tag', 'test-tag2');
|
||||
const result = await git.commitInfo();
|
||||
expect(result.tagName).to.be.equal('v1.2');
|
||||
expect(result.tagName).toBe('v1.2');
|
||||
});
|
||||
|
||||
it('should show no tag if there is no tag', async function () {
|
||||
const result = await git.commitInfo();
|
||||
expect(result.tagName).to.be.null();
|
||||
expect(result.tagName).toBeNull();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user