neotest-jest
neotest-jest copied to clipboard
Abstracted Jest tests using __helpers__/helper.ts
I have a setup where I offload some common strategies for testing in a helper file,
I run tests the same as if they were using the regular describe/it pattern:
jest x-var.test.ts
I assume neotest-jest, is looking for the describe() and test() methods to register the tests. Is there a configuration I'm missing or an improvement I can implement to the project that would help neotest-jest infer the tests? as they function exactly the same but just a bit abstracted for convenience.
helpers/helper.ts:
export default (ruleName: RuleName, tests: Scenario): void => {
describe(`Rule ${ruleName}`, () => {
const concurrent = tests.every(
(test) => test.mocks === void 0 || Object.keys(test.mocks).length === 0,
);
for (const testCase of tests) {
(concurrent ? it.concurrent : it)(testCase.name, async () => {
const s = createWithRules([ruleName]);
const doc =
testCase.document instanceof Document
? testCase.document
: JSON.stringify(testCase.document);
const errors = await s.run(doc);
expect(errors.filter(({ code }) => code === ruleName)).toEqual(
testCase.errors.map((error) => ({
code: ruleName,
message: expect.stringMatching(
new RegExp(error.message?.replace(/['"]/g, "['\"]") || "", "i"),
),
path: expect.arrayContaining(error.path || []),
range: expect.objectContaining({
start: expect.objectContaining({
character: expect.any(Number),
line: expect.any(Number),
}),
end: expect.objectContaining({
character: expect.any(Number),
line: expect.any(Number),
}),
}),
severity: error.severity,
})),
);
});
}
});
};
And a Test file x-var.test.ts:
import testRule from "./__helpers__/helper";
testRule("x-var-truthy", [
{
name: "pass:x-var is properly defined in OAS3",
document: {
openapi: "3.0.0",
info: { version: "1.0" },
paths: { "/": { get: { "x-var": { name: "aabbcc" } } } },
},
errors: [],
},
]);
Edit: I've been experimenting with tricking neotest-jest into thinking it's in another describe/test block. But neotest throws errors when nesting describe inside tests.
Note: I'm a bit new to using Typescript and jest in general. So if it simply isn't possible, Could a solution be to synthetically define tests through annotations or comments? I'd hate to miss out on re-running tests and navigating through the summery buffer. It's amazing.