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

Pass docblock pragmas to TestEnvironment constructor. #8320

Merged
merged 7 commits into from Apr 13, 2019

Conversation

scotthovestadt
Copy link
Contributor

Summary

This simple PR allows test environments to take advantage of docblock pragmas for test file configuration.

For example, in an E2E test, you may want your environment to know during setup that the test user should be configured a certain way. Since that setup may happen when the environment is constructed and before the tests and test hooks are actually run, there needs to be a way for that configuration to be specified.

The docblock is a great fit for that use-case and fits the "Jest way" of doing things, since it's already used in a similar way internally (for specifying the test environment). While it's possible today for any environment to import jest-docblock and re-parse the test file, I thought it would be a better UX to have the pragmas passed in, especially since they are already available.

Test plan

  • All tests pass.
  • Added new test for this feature.
  • Added documentation.

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea!

// passed, or not. The context itself is optional, not properties within it.
export type EnvironmentContext = Partial<{
console: Console;
docblockPragmas: {[key: string]: string | Array<string>};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the docblock types exported from anywhere? Maybe not worth the dep

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I didn’t think it was worth the dependency.

Copy link
Contributor

@jeysal jeysal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good idea indeed. Wondering if you already have use cases for this in mind @scotthovestadt?

*/
'use strict';

test('access env', () => {
expect(property).toBe('value'); // eslint-disable-line no-undef
});

test('docblock pragmas', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer a distinct test file and Environment impl to separate concerns - this one tests ESM default export support and I wouldn't want docblock pragmas to fail as well if that breaks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@codecov-io
Copy link

Codecov Report

Merging #8320 into master will not change coverage.
The diff coverage is 0%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #8320   +/-   ##
=======================================
  Coverage   62.17%   62.17%           
=======================================
  Files         266      266           
  Lines       10683    10683           
  Branches     2597     2598    +1     
=======================================
  Hits         6642     6642           
  Misses       3452     3452           
  Partials      589      589
Impacted Files Coverage Δ
packages/jest-runner/src/runTest.ts 2.43% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 538cef4...740075b. Read the comment docs.

@github-actions
Copy link

This pull request 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 May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants