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

✅ Fix test cleanup tasks order #3141

Merged
merged 2 commits into from
Nov 18, 2024
Merged

Conversation

amortemousque
Copy link
Contributor

@amortemousque amortemousque commented Nov 18, 2024

Motivation

We are transitionning from using afterEach to registerCleanupTask API to clean our tests. However with registerCleanupTask, tasks are executed in the wrong order:

afterEachregisterCleanupTask
describe('Test', () => {
  beforeEach(() => { 
    console.log('instrument')
  })

  afterEach(() => { 
    console.log('clean instrumentation')
  })

  describe('Nested test', () => {
    beforeEach(() => {
      console.log('instrument 2')
    })

    afterEach(() => { 
      console.log('clean instrumentation 2')
    })
  })
})
describe('Test', () => {
  beforeEach(() => {
    console.log('instrument')
    registerCleanupTask(() => {
      console.log('clean instrumentation')
    })
  })

  describe('Nested test', () => {
    beforeEach(() => {
      console.log('instrument 2')
      registerCleanupTask(() => {
        console.log('clean instrumentation 2 ')
      })
    })
  })
})
LOG LOG: 'instrument'
LOG LOG: 'instrument 2'
LOG LOG: 'clean instrumentation 2'
LOG LOG: 'clean instrumentation'
Before
LOG LOG: 'instrument'
LOG LOG: 'instrument 2'
LOG LOG: 'clean instrumentation'
LOG LOG: 'clean instrumentation 2'

After

LOG LOG: 'instrument'
LOG LOG: 'instrument 2'
LOG LOG: 'clean instrumentation 2'
LOG LOG: 'clean instrumentation'

Changes

  • Fix cleanup tasks order: image
  • Fix tests

Testing

  • Local
  • Staging
  • Unit
  • End to end

I have gone over the contributing documentation.

@amortemousque amortemousque marked this pull request as ready for review November 18, 2024 14:31
@amortemousque amortemousque requested a review from a team as a code owner November 18, 2024 14:31
Comment on lines 55 to 56
EventTarget.prototype.addEventListener = jasmine
.createSpy()
Copy link
Member

Choose a reason for hiding this comment

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

This seems unrelated to this PR, but in any case:

❓ question:spyOn(EventTarget.prototype, 'addEventListener') didn't work?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I found cases where mocking the same API both manually with registerCleanup and with spyOn case don't work. I can't really explain why, but it seems reasonable to use the same mocking strategy twice. wdyt?

Copy link
Member

Choose a reason for hiding this comment

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

wdyt?

I guess spies are removed after all afterEach, so we have an order issue similar to what you are fixing in this ticket. So, I guess it makes sense.

Your explanation makes sense but it's a bit annoying that we cannot use Jasmine tools for that. This sounds extremely error prone..

One workaround I have in mind is to always attach spies first, before any other instrumentation. Then they will be detached after everything, so it will be fine.

An alternative could be to have our own spyOn utility that works with registerCleanupTask and forbid the jasmine one.

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.44%. Comparing base (0a018d6) to head (4480edc).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3141      +/-   ##
==========================================
- Coverage   93.71%   93.44%   -0.28%     
==========================================
  Files         279      279              
  Lines        7682     7683       +1     
  Branches     1718     1718              
==========================================
- Hits         7199     7179      -20     
- Misses        483      504      +21     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

Copy link

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum 161.52 KiB 161.52 KiB 0 B 0.00%
Logs 55.77 KiB 55.77 KiB 0 B 0.00%
Rum Slim 110.36 KiB 110.36 KiB 0 B 0.00%
Worker 25.21 KiB 25.21 KiB 0 B 0.00%
🚀 CPU Performance
Action Name Base Average Cpu Time (ms) Local Average Cpu Time (ms) 𝚫
addglobalcontext 0.001 0.002 0.000
addaction 0.038 0.048 0.010
addtiming 0.001 0.001 0.000
adderror 0.034 0.070 0.036
startstopsessionreplayrecording 0.902 1.015 0.113
startview 0.986 1.125 0.139
logmessage 0.020 0.038 0.018
🧠 Memory Performance
Action Name Base Consumption Memory (bytes) Local Consumption Memory (bytes) 𝚫 (bytes)
addglobalcontext 6.75 KiB 7.56 KiB 830 B
addaction 38.88 KiB 38.61 KiB -277 B
addtiming 5.85 KiB 6.22 KiB 380 B
adderror 42.85 KiB 46.27 KiB 3.41 KiB
startstopsessionreplayrecording 5.70 KiB 4.69 KiB -1035 B
startview 424.00 KiB 428.67 KiB 4.67 KiB
logmessage 40.75 KiB 41.30 KiB 561 B

🔗 RealWorld

@amortemousque amortemousque merged commit 16efff7 into main Nov 18, 2024
18 checks passed
@amortemousque amortemousque deleted the aymeric/fix-test-cleanup-order branch November 18, 2024 19:13
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.

3 participants