Skip to content

Add vitest browser test framework#2537

Open
codingjoe wants to merge 1 commit into
django:mainfrom
codingjoe:jstests
Open

Add vitest browser test framework#2537
codingjoe wants to merge 1 commit into
django:mainfrom
codingjoe:jstests

Conversation

@codingjoe

@codingjoe codingjoe commented Mar 4, 2026

Copy link
Copy Markdown
Contributor

First step towards a robust test suite to enable JavaScript changes with confidence.
I chose the cookie functions as PoC.

@django/django-website was this what you had regarding
"We need to test the changed javascript for regressions"?

I didn't add Edge as a test browser since Edge and Chromium are engine-wise almost identical.

@codingjoe codingjoe force-pushed the jstests branch 2 times, most recently from 12253ca to 5578c20 Compare March 4, 2026 16:53
@codecov-commenter

codecov-commenter commented Mar 4, 2026

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 88.23529% with 2 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (main@7cdb932). Learn more about missing BASE report.

Files with missing lines Patch % Lines
djangoproject/static/js/mod/switch-dark-mode.js 88.23% 2 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2537   +/-   ##
=======================================
  Coverage        ?   85.49%           
=======================================
  Files           ?      185           
  Lines           ?     6727           
  Branches        ?      395           
=======================================
  Hits            ?     5751           
  Misses          ?      894           
  Partials        ?       82           
Flag Coverage Δ
javascript 52.72% <88.23%> (?)
python 85.76% <ø> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@codingjoe codingjoe marked this pull request as ready for review March 4, 2026 17:06
Copilot AI review requested due to automatic review settings March 4, 2026 17:06

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces vitest as a browser test framework for JavaScript code, using the cookie functions in switch-dark-mode.js as a proof of concept. It adds vitest configuration, package.json, initial tests, and CI integration, while also refactoring switch-dark-mode.js to use const declarations, strict equality, arrow functions, and exports for testability.

Changes:

  • Sets up vitest browser testing framework with WebdriverIO provider, coverage via istanbul, and CI integration in GitHub Actions.
  • Adds browser tests for getCookie and setCookie functions from switch-dark-mode.js.
  • Refactors switch-dark-mode.js to export functions, use const/let, strict equality, globalThis, and arrow functions.

Reviewed changes

Copilot reviewed 6 out of 8 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
vitest.config.js New vitest configuration with browser mode (WebdriverIO, Chrome + Firefox) and istanbul coverage
package.json New package metadata with devDependencies for vitest and test runner
djangoproject/static/js/mod/tests/switch-dark-mode.test.js New browser tests for getCookie and setCookie
djangoproject/static/js/mod/switch-dark-mode.js Refactored to use exports, const/let, globalThis, strict equality, arrow functions
.nvmrc New file pinning Node.js to the LTS version
.gitignore Added node_modules/ and coverage/ ignore rules
.github/workflows/tests.yml Renamed tests job to python, added vitest CI job, and added Codecov reporting to both

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread djangoproject/static/js/mod/switch-dark-mode.js
Comment thread djangoproject/static/js/mod/tests/switch-dark-mode.test.js Outdated
Comment thread djangoproject/static/js/mod/tests/switch-dark-mode.test.js
Comment thread package.json Outdated
Comment thread package.json Outdated
Comment thread djangoproject/static/js/mod/switch-dark-mode.js
@codingjoe codingjoe force-pushed the jstests branch 6 times, most recently from b458625 to e4a37dc Compare March 4, 2026 17:51
@codingjoe

Copy link
Copy Markdown
Contributor Author

Ref #2325 @ulgens I added codecov here, mainly to test it and to make sure I add tags. I can drop the changes, but someone needs to remember to tag both languages.

@codingjoe codingjoe force-pushed the jstests branch 3 times, most recently from 89a5cdf to 2ec6504 Compare May 9, 2026 16:17
@sarahboyce

Copy link
Copy Markdown
Contributor

We recently added Playwright tests. I just opened a pr to show how we could add some tests here: #2623
@codingjoe Do you still feel this is needed?

@codingjoe

Copy link
Copy Markdown
Contributor Author

We recently added Playwright tests. I just opened a pr to show how we could add some tests here: #2623 @codingjoe Do you still feel this is needed?

Yes, I do believe it's needed. I want to add unit tests, not integration tests. The latter is done via Playwright but comes at a performance and reliability cost. Besides, writing unit tests is a more rewarding experience in most cases.

We recently added the same framework to django-debug-toolbar too, and it serves us well.

@adamzap

adamzap commented May 15, 2026

Copy link
Copy Markdown
Member

There's not much JavaScript in this project currently, and there should be less as time goes on. My preference would be to invest in Playwright tests now and finish the in-progress JavaScript refactor before evaluating the need for JavaScript unit tests.

@codingjoe

Copy link
Copy Markdown
Contributor Author

There's not much JavaScript in this project currently, and there should be less as time goes on. My preference would be to invest in Playwright tests now and finish the in-progress JavaScript refactor before evaluating the need for JavaScript unit tests.

While I don't believe there will be less JavaScript in the future, I only added this to provide tests for the fix of the setCookie utility, link in the pr description.

If you say I don't have to, I won't. However, a refactor may benefit from unit test. I would use Playwright with the same caution, as Selenium. I love Python, but not to test JavaScript.

@marksweb

Copy link
Copy Markdown
Contributor

I'm not against more things to help us test. And I'm sure there will always be some JS that could be tested.

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.

6 participants