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

Intention of global aliases #1252

Closed
tbartley opened this issue Sep 29, 2022 · 3 comments
Closed

Intention of global aliases #1252

tbartley opened this issue Sep 29, 2022 · 3 comments

Comments

@tbartley
Copy link

Hi,

I often define my own conditional implementations of describe or it where I might have something like:

let relevantTests = describe.skip
if (env.THESE_TESTS_ARE_RELEVANT) {
  relevantTests = describe
}

relevantTests('run the relevant tests', () => {
  it('should ...)
})

so I tried setting relevantTests as a global alias for describe in settings but it is ignored because of this code in resolveToJestFn in src/rules/utils/parseJestFnCall.ts:

  // the identifier was found as a local variable or function declaration
  // meaning it's not a function from jest
  if (references.locals.has(identifier)) {
    return null;
  }

If I remove that code then my aliased describe function is correctly identified as an alias for describe.

I'm happy to submit a PR but clearly that code was deliberately added for a reason and just removing it may not be the right solution. Or maybe my understanding of the purpose of the globalAliases setting is incorrect.

Cheers,

Tim

@tbartley
Copy link
Author

tbartley commented Sep 29, 2022

A more complete example as to why this causes an issue for me would be a pattern like:

let relevantTests = describe.skip
if (env.THESE_TESTS_ARE_RELEVANT) {
  relevantTests = describe
}

let otherRelevantTests = describe.skip
if (env.THESE_OTHER_TESTS_ARE_RELEVANT) {
  otherRelevantTests = describe
}

describe('full test suite', () => {
  relevantTests('run the relevant tests', () => {
    it('should behave like this', () = {
       ...
    })
  })

  otherRelevantTests('run the other relevant tests', () => {
    it('should behave like this', () = {
       ...
    })
  })
})

In the above case I have a test in each aliased describe section with the same name. Because my alias is not picked up as a describe alias, I get a "duplicate title" error as the tests in each sub-suite have the same title.

This scenario is when I'm using jest to run system tests against different environments where one, both or none of the above sub-suites may be relevant in any given environment.

I was hoping I could simply add the following settings to my .eslintrc.js to resolve:

{
   settings: {
     jest: {
       globalAliases: {
          describe: [ 'relevantTests', 'otherRelevantTests' ]
        }
     }
   }
}

@G-Rath
Copy link
Collaborator

G-Rath commented Sep 29, 2022

It's because it expects them to be global aliases - I'm not really that comfortable with extending this support out further given its already a stretch and you can resolve this by creating a setup file that assigns global.relevantTests = describe.skip.

See #83 and jestjs/jest#2468 (comment) for more

There's a few ways you can agree your advance case, such as having a function global (i.e. enableTheseTests('key');) or something like:

const runTestsIf = (condition, tests) => {
  if (condition()) {
    tests();
  }
};

describe("full test suite", () => {
  runTestsIf(
    () => env.THESE_TESTS_ARE_RELEVANT,
    () => {
      describe("run the other relevant tests", () => {
        it("should behave like this", () => {
          // ...
        });
      });
    }
  );

  runTestsIf(
    () => env.THESE_OTHER_TESTS_ARE_RELEVANT,
    () => {
      describe("run the other relevant tests", () => {
        it("should behave like this", () => {
          // ...
        });
      });
    }
  );
});

@tbartley
Copy link
Author

Thanks Gareth, I can look at other approaches to my needs that dont interfere as you suggest.

@tbartley tbartley closed this as not planned Won't fix, can't repro, duplicate, stale Sep 29, 2022
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

2 participants