Files
privacy.sexy/tests/unit/application/collections/NoUnintentedInlining.spec.ts
undergroundwires 5f11c8d98f 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.
2023-08-22 14:02:35 +02:00

82 lines
2.7 KiB
TypeScript

import { readdirSync, readFileSync } from 'fs';
import { resolve, join, basename } from 'path';
import { describe, it, expect } from 'vitest';
/*
A common mistake when working with yaml files to forget mentioning that a value should
be interpreted as multi-line string using "|".
E.g.
```
code: |-
echo Hello
echo World
```
If "|" is missing then the code is inlined like `echo Hello echo World``, which can be
unintended. This test checks for similar issues in collection yaml files.
These tests can be considered as "linter" more than "unit-test" and therefore can lead
to false-positives.
*/
describe('collection files to have no unintended inlining', () => {
// arrange
const testCases = createTestCases('src/application/collections/');
for (const testCase of testCases) {
it(`${testCase.name}`, async () => {
// act
const lines = await findBadLineNumbers(testCase.content);
// assert
expect(lines).to.be.have.lengthOf(0, printMessage());
function printMessage(): string {
return 'Did you intend to have multi-lined string in lines: ' // eslint-disable-line prefer-template
+ lines.map(((line) => line.toString())).join(', ');
}
});
}
});
async function findBadLineNumbers(fileContent: string): Promise<number[]> {
return [
...findLineNumbersEndingWith(fileContent, 'revertCode:'),
...findLineNumbersEndingWith(fileContent, 'code:'),
];
}
function findLineNumbersEndingWith(content: string, ending: string): number[] {
sanityCheck(content, ending);
return content
.split(/\r\n|\r|\n/)
.map((line, index) => ({ text: line, index }))
.filter((line) => line.text.trim().endsWith(ending))
.map((line) => line.index + 1 /* first line is 1, not 0 */);
}
function sanityCheck(content: string, ending: string): void {
if (!content) {
throw new Error('File content is empty, is the file loaded correctly?');
}
if (!content.includes(ending)) {
throw new Error(
`File does not contain string "${ending}" string at all.`
+ `Did the word "${ending}" change? Or is this sanity check wrong?`,
);
}
}
interface ITestCase {
name: string;
content: string;
}
function createTestCases(collectionsDirFromRoot: string): ITestCase[] {
const collectionsDir = resolve(`./${collectionsDirFromRoot}`);
const fileNames = readdirSync(collectionsDir);
if (fileNames.length === 0) {
throw new Error(`Could not find any collection in ${collectionsDir}`);
}
const collectionFilePaths = fileNames
.filter((name) => name.endsWith('.yaml'))
.map((name) => join(collectionsDir, name));
return collectionFilePaths.map((path) => ({
name: basename(path),
content: readFileSync(path, 'utf-8'),
}));
}