Migrate unit/integration tests to Vitest with Vite

As part of transition to Vue 3.0 and Vite (#230), this commit
facilitates the shift towards building rest of the application using
Vite. By doing so, it eliminates reliance on outdated Electron building
system that offered limited control, blocking desktop builds (#233).

Changes include:

- Introduce Vite with Vue 2.0 plugin for test execution.
- Remove `mocha`, `chai` and other related dependencies.
- Adjust test to Vitest syntax.
- Revise and update `tests.md` to document the changes.
- Add `@modyfi/vite-plugin-yaml` plugin to be able to use yaml file
  depended logic on test files, replacing previous webpack behavior.
- Fix failing tests that are revealed by Vitest due to unhandled errors
  and lack of assertments.
- Remove the test that depends on Vue CLI populating `process.env`.
- Use `jsdom` for unit test environment, adding it to dependency to
  `package.json` as project now depends on it and it was not specified
  even though `package-lock.json` included it.
This commit is contained in:
undergroundwires
2023-08-22 14:02:35 +02:00
parent 08737698c2
commit 5f11c8d98f
133 changed files with 8174 additions and 7597 deletions

View File

@@ -1,5 +1,4 @@
import 'mocha';
import { expect } from 'chai';
import { describe, it, expect } from 'vitest';
import { ScriptingLanguage } from '@/domain/ScriptingLanguage';
import { ScriptingLanguageFactory } from '@/application/Common/ScriptingLanguage/ScriptingLanguageFactory';
import { EnumRangeTestRunner } from '@tests/unit/application/Common/EnumRangeTestRunner';
@@ -58,8 +57,12 @@ describe('ScriptingLanguageFactory', () => {
const sut = new ScriptingLanguageConcrete();
const runner = new ScriptingLanguageFactoryTestRunner();
// act
sut.registerGetter(ScriptingLanguage.batchfile, () => 1);
sut.registerGetter(ScriptingLanguage.batchfile, () => ScriptingLanguage.batchfile);
sut.registerGetter(ScriptingLanguage.shellscript, () => ScriptingLanguage.shellscript);
// assert
runner.testCreateMethod(sut);
runner
.expectValue(ScriptingLanguage.shellscript, ScriptingLanguage.shellscript)
.expectValue(ScriptingLanguage.batchfile, ScriptingLanguage.batchfile)
.testCreateMethod(sut);
});
});

View File

@@ -1,29 +1,45 @@
import 'mocha';
import { expect } from 'chai';
import { describe, it, expect } from 'vitest';
import { IScriptingLanguageFactory } from '@/application/Common/ScriptingLanguage/IScriptingLanguageFactory';
import { ScriptingLanguage } from '@/domain/ScriptingLanguage';
import { EnumRangeTestRunner } from '@tests/unit/application/Common/EnumRangeTestRunner';
export class ScriptingLanguageFactoryTestRunner<T> {
private expectedTypes = new Map<ScriptingLanguage, T>();
type Constructible<T = object> = new (...args: unknown[]) => T;
public expect(language: ScriptingLanguage, resultType: T) {
this.expectedTypes.set(language, resultType);
export class ScriptingLanguageFactoryTestRunner<T> {
private expectedLanguageTypes = new Map<ScriptingLanguage, Constructible<T>>();
private expectedValues = new Map<ScriptingLanguage, T>();
public expectInstance(language: ScriptingLanguage, resultType: Constructible<T>) {
this.expectedLanguageTypes.set(language, resultType);
return this;
}
public expectValue(language: ScriptingLanguage, resultType: T) {
this.expectedValues.set(language, resultType);
return this;
}
public testCreateMethod(sut: IScriptingLanguageFactory<T>) {
if (!sut) { throw new Error('missing sut'); }
testLanguageValidation(sut);
testExpectedInstanceTypes(sut, this.expectedTypes);
if (this.expectedLanguageTypes.size) {
testExpectedInstanceTypes(sut, this.expectedLanguageTypes);
}
if (this.expectedValues.size) {
testExpectedValues(sut, this.expectedValues);
}
}
}
function testExpectedInstanceTypes<T>(
sut: IScriptingLanguageFactory<T>,
expectedTypes: Map<ScriptingLanguage, T>,
expectedTypes: Map<ScriptingLanguage, Constructible<T>>,
) {
describe('create returns expected instances', () => {
if (!expectedTypes?.size) {
throw new Error('No expected types provided.');
}
describe('`create` creates expected instances', () => {
// arrange
for (const language of expectedTypes.keys()) {
it(ScriptingLanguage[language], () => {
@@ -37,8 +53,29 @@ function testExpectedInstanceTypes<T>(
});
}
function testExpectedValues<T>(
sut: IScriptingLanguageFactory<T>,
expectedValues: Map<ScriptingLanguage, T>,
) {
if (!expectedValues?.size) {
throw new Error('No expected values provided.');
}
describe('`create` creates expected values', () => {
// arrange
for (const language of expectedValues.keys()) {
it(ScriptingLanguage[language], () => {
// act
const expected = expectedValues.get(language);
const result = sut.create(language);
// assert
expect(result).to.equal(expected);
});
}
});
}
function testLanguageValidation<T>(sut: IScriptingLanguageFactory<T>) {
describe('validates language', () => {
describe('`create` validates language selection', () => {
// arrange
const validValue = ScriptingLanguage.batchfile;
// act