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

Abstracted Jest tests using __helpers__/helper.ts #105

Open
SirAppSec opened this issue Feb 29, 2024 · 0 comments
Open

Abstracted Jest tests using __helpers__/helper.ts #105

SirAppSec opened this issue Feb 29, 2024 · 0 comments

Comments

@SirAppSec
Copy link

SirAppSec commented Feb 29, 2024

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.

@SirAppSec SirAppSec changed the title Abstrected Jest tests using __helpers__/helper.ts Abstracted Jest tests using __helpers__/helper.ts Feb 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant