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

chore(tests): Move code in global test scope to inside beforeAll #90

Merged
merged 1 commit into from
Aug 20, 2024

Conversation

sandrina-p
Copy link
Collaborator

@sandrina-p sandrina-p commented Aug 19, 2024

chore(tests): Move code in global test scope to inside beforeAll

Problem:

When we need to run only a specific test (it.only()), we were seeing logs of createHeadlessForm() outside that test unit. That can cause a lot of noise if we want to debug only a specific unit test, but also seeing logs from other instances.

Reason

That happens because we had 1 instance of createHeadlessForm() being called outside jest lifecycle.

Solution

Moved that 1 instance in the global scope (inside describe()) to an isolated scope, using beforeAll()

How to test.

A simple way can be to add console.log() to inside the createHeadlessForm()

export function createHeadlessForm(jsonSchema, customConfig = {}) {
+  console.log('Hello!')

And then run only one test at your choise. Eg it.only('returns empty result given no schema'

You'll see the logs saying "Hello" once. But if you go to main master and do the same thing, you'll see the logs twice (Because of the other global instance)

This prevents that code from running when using it.only
Copy link
Collaborator

@ollyd ollyd left a comment

Choose a reason for hiding this comment

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

Tested it, looks good 👍

@sandrina-p sandrina-p merged commit 88f7d3c into main Aug 20, 2024
3 checks passed
@sandrina-p sandrina-p deleted the fix/tests-remove-code-execution-from-global-scope branch August 20, 2024 09:47
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

Successfully merging this pull request may close these issues.

2 participants