Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow usage of mocha --grep in spec tests #4310

Merged
merged 2 commits into from Jul 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
77 changes: 35 additions & 42 deletions packages/spec-test-util/src/single.ts
Expand Up @@ -100,18 +100,47 @@ export function describeDirectorySpecTest<TestCase extends {meta?: any}, Result>
if (!isDirectory(testCaseDirectoryPath)) {
throw new Error(`${testCaseDirectoryPath} is not directory`);
}

describe(name, function () {
if (options.timeout !== undefined) {
this.timeout(options.timeout || "10 min");
}

const testCases = fs
.readdirSync(testCaseDirectoryPath)
.map((name) => join(testCaseDirectoryPath, name))
.filter(isDirectory);
for (const testSubDirname of fs.readdirSync(testCaseDirectoryPath)) {
const testSubDirPath = join(testCaseDirectoryPath, testSubDirname);
if (!isDirectory(testSubDirPath)) {
continue;
}

for (const [index, testCaseDirectory] of testCases.entries()) {
generateTestCase(testCaseDirectory, index, testFunction, options);
// Use full path here, not just `testSubDirname` to allow usage of `mocha --grep`
it(`${name}/${testSubDirname}`, async function () {
// some tests require to load meta.yaml first in order to know respective ssz types.
const metaFilePath = join(testSubDirPath, "meta.yaml");
const meta: TestCase["meta"] = fs.existsSync(metaFilePath)
? loadYaml(fs.readFileSync(metaFilePath, "utf8"))
: undefined;

let testCase = loadInputFiles(testSubDirPath, options, meta);
if (options.mapToTestCase) testCase = options.mapToTestCase(testCase);
if (options.shouldSkip && options.shouldSkip(testCase, name, 0)) {
this.skip();
return;
}

if (options.shouldError?.(testCase)) {
try {
await testFunction(testCase, name);
} catch (e) {
return;
}
} else {
const result = await testFunction(testCase, name);
if (!options.getExpected) throw Error("getExpected is not defined");
if (!options.expectFunc) throw Error("expectFunc is not defined");
const expected = options.getExpected(testCase);
options.expectFunc(testCase, expected, result);
}
});
}
});
}
Expand All @@ -120,42 +149,6 @@ export function loadYamlFile(path: string): Record<string, unknown> {
return loadYaml(fs.readFileSync(path, "utf8"));
}

function generateTestCase<TestCase extends {meta?: any}, Result>(
testCaseDirectoryPath: string,
index: number,
testFunction: (...args: any) => Result | Promise<Result>,
options: ISpecTestOptions<TestCase, Result>
): void {
const name = basename(testCaseDirectoryPath);
it(name, async function () {
// some tests require to load meta.yaml first in order to know respective ssz types.
const metaFilePath = join(testCaseDirectoryPath, "meta.yaml");
let meta: TestCase["meta"] = undefined;
if (fs.existsSync(metaFilePath)) {
meta = loadYaml(fs.readFileSync(metaFilePath, "utf8"));
}
let testCase = loadInputFiles(testCaseDirectoryPath, options, meta);
if (options.mapToTestCase) testCase = options.mapToTestCase(testCase);
if (options.shouldSkip && options.shouldSkip(testCase, name, index)) {
this.skip();
return;
}
if (options.shouldError && options.shouldError(testCase)) {
try {
await testFunction(testCase, name);
} catch (e) {
return;
}
} else {
const result = await testFunction(testCase, name);
if (!options.getExpected) throw Error("getExpected is not defined");
if (!options.expectFunc) throw Error("expectFunc is not defined");
const expected = options.getExpected(testCase);
options.expectFunc(testCase, expected, result);
}
});
}

function loadInputFiles<TestCase extends {meta?: any}, Result>(
directory: string,
options: ISpecTestOptions<TestCase, Result>,
Expand Down