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

New method "setTestUrl" to allow us to set the testUrl from jest config on a per-test basis #11109

Closed
ahayes91 opened this issue Feb 23, 2021 · 4 comments

Comments

@ahayes91
Copy link

ahayes91 commented Feb 23, 2021

🚀 Feature Proposal

https://jestjs.io/docs/en/configuration#testurl-string allows you to set the URL for the JSDOM environment at the jest.config.js / package.json level, but there is no way to set this attribute on a per-test basis. A setTestUrl method on the jest object would allow you to set the URL on the fly in tests (similar to setTimeout https://jestjs.io/docs/en/jest-object#jestsettimeouttimeout).

Motivation

We use jest, @testing-library/react, and Mock Service Worker to do App-level integration testing (if I ever get around to writing a blog post about it I'll link it here https://hmh.engineering/want-to-increase-your-quality-and-reduce-your-deployment-times-meet-app-level-integration-testing-308a38cbb321 - it's been great in helping us slim down our expensive Selenium end to end suites) and that involves mocking data endpoints for a particular scenario, rendering an app, and performing semantic queries to emulate end user testing. The app might have a Router inside it with several different routes, so what we currently do is set the global test URL on a per-test basis before rendering the app:

describe('Testing my app on myRoute1: ', () => {
  beforeAll(() => {
    window.history.replaceState('', '', '/myRoute1');
    render(<App />);
  });

Another test for a different route might look like:

describe('Testing my app on myRoute1 with some pre-supplied query parameters: ', () => {
  beforeAll(() => {
    window.history.replaceState('', '', '/myRoute1?sort=apples');
    render(<App />);
  });

We should be able to utilise the out-of-the-box jest feature for testUrl rather than messing with the window object in these tests.

Example

Those examples above would become something like:

describe('Testing my app on myRoute1 with some pre-supplied query parameters: ', () => {
  beforeAll(() => {
    jest.setTestUrl('/myRoute1?sort=apples');
    render(<App />);
  });

Pitch

Why does this feature belong in the Jest core platform?

Common feature proposals that do not typically make it to core:

  • New matchers (see jest-extended)
  • Changes to the default reporter (use custom reporters instead)
  • Changes to node/jsdom test environments (use custom environments instead)
@mattbdc
Copy link

mattbdc commented Aug 20, 2021

Yes this would be much cleaner. There's so much outdated jest/jsdom docs out there, its needs to be integrated as first class imo.

@github-actions
Copy link

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Aug 20, 2022
@github-actions
Copy link

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants