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:
Igor Savin
2026-03-02 22:33:52 +02:00
committed by GitHub
parent 1245255892
commit d65683434d
38 changed files with 3453 additions and 3138 deletions
-22
View File
@@ -1,22 +0,0 @@
const { execNodeJsScriptWithInheritedOutput } = require('./util/exec-file');
const { createRegisterAsyncTaskFn } = require('./util/async-grunt-task');
const nodeJs = process.argv0;
module.exports = function (grunt) {
const registerAsyncTask = createRegisterAsyncTaskFn(grunt);
registerAsyncTask('test:mocha', async () =>
execNodeJsScriptWithInheritedOutput('./spec/env/runner')
);
registerAsyncTask('test:cov', async () =>
execNodeJsScriptWithInheritedOutput('node_modules/nyc/bin/nyc', [
nodeJs,
'./spec/env/runner.js',
])
);
registerAsyncTask('test:min', async () =>
execNodeJsScriptWithInheritedOutput('./spec/env/runner', ['--min'])
);
};
+7 -2
View File
@@ -1,5 +1,10 @@
module.exports = {
env: {
mocha: true,
globals: {
describe: true,
it: true,
expect: true,
beforeEach: true,
afterEach: true,
vi: true,
},
};
+45 -31
View File
@@ -3,10 +3,6 @@ const fs = require('fs');
const os = require('os');
const path = require('path');
const chai = require('chai');
chai.use(require('chai-diff'));
const expect = chai.expect;
const testCases = [
{
binInputParameters: ['-a', 'spec/artifacts/empty.handlebars'],
@@ -74,7 +70,7 @@ const testCases = [
{
binInputParameters: ['-v'],
outputLocation: 'stdout',
expectedOutput: require('../package.json').version,
expectedOutput: require('../../package.json').version,
},
{
binInputParameters: [
@@ -177,15 +173,46 @@ const testCases = [
},
];
module.exports = function (grunt) {
grunt.registerTask('test:bin', function () {
testCases.forEach(
({
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) {
@@ -193,25 +220,19 @@ module.exports = function (grunt) {
}
const useStdout = outputLocation === 'stdout';
const normalizedOutput = normalizeCrlf(
useStdout ? stdout : fs.readFileSync(outputLocation, 'utf-8')
);
const normalizedExpectedOutput = normalizeCrlf(expectedOutput);
const actualOutput = useStdout
? stdout
: fs.readFileSync(outputLocation, 'utf-8');
if (!useStdout) {
fs.unlinkSync(outputLocation);
}
expect(normalizedOutput).not.to.be.differentFrom(
normalizedExpectedOutput,
{
relaxedSpace: true,
}
);
}
);
});
};
expect(actualOutput).toEqualWithRelaxedSpace(expectedOutput);
});
}
);
});
// helper functions
@@ -233,10 +254,3 @@ function execFilesSyncUtf8(command, args) {
function addPathToNodeJs(pathEnvironment) {
return path.dirname(process.argv0) + path.delimiter + pathEnvironment;
}
function normalizeCrlf(string) {
if (typeof string === 'string') {
return string.replace(/\r\n/g, '\n');
}
return string;
}
+15 -15
View File
@@ -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();
});
});
});
View File