Use vitest instead of approvals (#6366)

Move to using `vitest`'s built-in behaviour, or nearer to it. Rename all
the txt to `.json` files, as that's what they are.

A bit of a workaround, though we might decide to keep it.

To update the "expected" you can run `npx vitest test/filter-tests.ts
--update` or similar.
This commit is contained in:
Matt Godbolt
2024-04-18 20:19:43 -05:00
committed by GitHub
parent b8325cf0c6
commit c1ed64473e
639 changed files with 342 additions and 4263 deletions

View File

@@ -46,3 +46,4 @@ lib/asm-docs/generated/asm-docs-*
# Files we ought to ignore
static/policies/*.html
test/**/*.json

2469
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -108,7 +108,6 @@
"@typescript-eslint/eslint-plugin": "^7.5.0",
"@typescript-eslint/parser": "^7.5.0",
"@vitest/coverage-v8": "^1.4.0",
"approvals": "^6.2.4",
"aws-sdk-client-mock": "^4.0.0",
"c8": "^9.1.0",
"cheerio": "^1.0.0-rc.12",

View File

@@ -24,9 +24,7 @@
import path from 'path';
import {configure, verifyAsJSON} from 'approvals';
import type {ApprovalFailureReporter} from 'approvals/lib/Core/ApprovalFailureReporter.js';
import {beforeAll, describe, expect, it} from 'vitest';
import {describe, expect, it} from 'vitest';
import {CC65AsmParser} from '../lib/parsers/asm-parser-cc65.js';
import {AsmEWAVRParser} from '../lib/parsers/asm-parser-ewavr.js';
@@ -59,12 +57,26 @@ const filesInCaseDir = files.map(x => resolvePathFromTestRoot('filters-cases', x
const cases = filesInCaseDir.filter(x => x.endsWith('.asm'));
const optionsOverride = {
forceApproveAll: false, // set to true to automatically regenerate all the cases.
blockUntilReporterExits: false,
maxLaunches: 1,
normalizeLineEndingsTo: process.platform === 'win32' ? '\r\n' : '\n',
errorOnStaleApprovedFiles: process.platform !== 'win32',
const recursivelyOrderKeys = (unordered: any): any => {
if (unordered === null) {
return null;
}
if (Array.isArray(unordered)) {
return unordered.map(recursivelyOrderKeys);
}
if (typeof unordered === 'object') {
const ordered: {[key: string]: any} = {};
for (const key of Object.keys(unordered).sort()) {
ordered[key] = recursivelyOrderKeys(unordered[key]);
}
return ordered;
}
return unordered;
};
const stringifyKeysInOrder = (data: any): string => {
const sortedData = recursivelyOrderKeys(data);
return JSON.stringify(sortedData, null, ' ');
};
function testFilter(filename: string, suffix: string, filters: ParseFiltersAndOutputOptions) {
@@ -75,33 +87,18 @@ function testFilter(filename: string, suffix: string, filters: ParseFiltersAndOu
const result = processAsm(filename, filters);
delete result.parsingTime;
delete result.filteredCount;
verifyAsJSON(casesRoot, testName, result, optionsOverride);
// TODO normalize line endings?
expect(stringifyKeysInOrder(result)).toMatchFileSnapshot(path.join(casesRoot, testName + '.json'));
},
{timeout: 10000},
); // Bump the timeout a bit so that we don't fail for slow cases
}
class VitestReporter implements ApprovalFailureReporter {
name: string = 'VitestReporter';
canReportOn() {
return true;
}
report(approvedFilePath: string, receivedFilePath: string) {
const approvedText = JSON.parse(fs.readFileSync(approvedFilePath).toString('utf8'));
const receivedText = JSON.parse(fs.readFileSync(receivedFilePath).toString('utf8'));
expect(receivedText).toBe(approvedText);
}
}
/*
The before() hooks on mocha are for it()s - They don't execute before the describes!
That's sad because then we can't have cases be loaded in a before() for every describe child to see.
*/
describe('Filter test cases', () => {
beforeAll(() => configure({reporters: [new VitestReporter()]}));
if (process.platform === 'win32') {
it('should skip filter-tests on Windows', () => {
expect(true).toBe(true);
@@ -194,9 +191,3 @@ describe('Filter test cases', () => {
}
});
});
describe('forceApproveAll should be false', () => {
it('should have forceApproveAll false', () => {
expect(optionsOverride.forceApproveAll).toBe(false);
});
});

Some files were not shown because too many files have changed in this diff Show More