Skip to content

chore: convert cache.js file within server to TypeScript #31471

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

Merged
merged 9 commits into from
Apr 10, 2025

Conversation

jennifer-shehane
Copy link
Member

@jennifer-shehane jennifer-shehane commented Apr 8, 2025

Additional details

Part of converting our repository from JS to TS. This converts the cache.js file in the server package to TypeScript.

  • These cache methods nearly all return Promises, so I updated the calls to those to expect a Promise.

Steps to test

yarn check-ts

How has the user experience changed?

Hopefully nothing!

PR Tasks

@jennifer-shehane jennifer-shehane self-assigned this Apr 8, 2025
@jennifer-shehane jennifer-shehane requested a review from Copilot April 8, 2025 19:58
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (1)

packages/server/lib/cache.ts:95

  • Using 'memo.push(path)' returns the new length of the array rather than the updated memo array, which can be confusing. Consider refactoring by pushing the path separately and then returning memo to improve clarity.
return memo.push(path)

Copy link

cypress bot commented Apr 8, 2025

cypress    Run #61525

Run Properties:  status check failed Failed #61525  •  git commit 4c728b13a0: Merge branch 'develop' into server-js-to-ts-cache
Project cypress
Branch Review server-js-to-ts-cache
Run status status check failed Failed #61525
Run duration 19m 19s
Commit git commit 4c728b13a0: Merge branch 'develop' into server-js-to-ts-cache
Committer Jennifer Shehane
View all properties for this run ↗︎

Test results
Tests that failed  Failures 1
Tests that were flaky  Flaky 15
Tests that did not run due to a developer annotating a test with .skip  Pending 1232
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 32139
View all changes introduced in this branch ↗︎
UI Coverage  46.59%
  Untested elements 183  
  Tested elements 164  
Accessibility  92.63%
  Failed rules  3 critical   8 serious   2 moderate   2 minor
  Failed elements 887  

Tests for review

Failed  cypress/e2e/studio/studio.cy.ts • 1 failed test • app-e2e

View Output

Test Artifacts
Cypress Studio > remains in studio mode when the test name is changed on the file system and file watching is disabled Test Replay Screenshots
Flakiness  commands/waiting.cy.js • 1 flaky test • 5x-driver-electron

View Output

Test Artifacts
... > errors > throws when waiting for 2nd response to route Test Replay
Flakiness  issues/28527.cy.ts • 1 flaky test • 5x-driver-electron

View Output

Test Artifacts
issue 28527 > fails and then retries and verifies about:blank is not displayed Test Replay Screenshots
Flakiness  commands/waiting.cy.js • 1 flaky test • 5x-driver-chrome

View Output

Test Artifacts
... > errors > throws when waiting for 2nd response to route Test Replay
Flakiness  e2e/origin/commands/waiting.cy.ts • 1 flaky test • 5x-driver-chrome

View Output

Test Artifacts
... > throws when foo cannot resolve Test Replay
Flakiness  issues/28527.cy.ts • 1 flaky test • 5x-driver-chrome

View Output

Test Artifacts
issue 28527 > fails and then retries and verifies about:blank is not displayed Test Replay Screenshots

The first 5 flaky specs are shown, see all 15 specs in Cypress Cloud.

@jennifer-shehane jennifer-shehane requested a review from Copilot April 8, 2025 21:37
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

packages/server/test/unit/cache_spec.js:202

  • [nitpick] Consider refactoring the test to consistently use async/await rather than mixing it with .then chaining. For example, after awaiting cache.removeProjectPreferences, await the subsequent call to __get rather than chaining .then to improve readability.
await cache.removeProjectPreferences(testProjectTitle)

@jennifer-shehane jennifer-shehane requested a review from Copilot April 9, 2025 16:35
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 14 out of 14 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (3)

packages/types/src/cache.ts:6

  • [nitpick] Consider providing a more specific type than 'any' for PROJECTS_CONFIG to improve type safety.
PROJECTS_CONFIG: Record<string, any>

packages/server/lib/cache.ts:101

  • Returning the result of memo.push (a number) could be misleading; consider refactoring to push the path and then explicitly return the memo.
return memo.push(path)

packages/data-context/src/data/coreDataShape.ts:25

  • The return type for savedState has changed from using a dedicated SavedStateShape to Partial; ensure that all consumers of this interface are updated accordingly.
savedState?: () => Promise<Partial<AllowedState>>

@@ -22,7 +22,7 @@ export interface AuthenticatedUserShape {

export interface ProjectShape {
projectRoot: string
savedState?: () => Promise<Maybe<SavedStateShape>>
savedState?: () => Promise<Partial<AllowedState>>
Copy link
Contributor

Choose a reason for hiding this comment

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

AllowedState is already a Partial - see L#58 of types/src/preferences.ts - so this is redundant

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressed here: ce56bd9 (#31471)

insertProjectToCache (projectRoot: string) {
return cache.insertProject(projectRoot)
async insertProjectToCache (projectRoot: string) {
await cache.insertProject(projectRoot)
Copy link
Contributor

Choose a reason for hiding this comment

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

The original is actually a little bit better, because this wraps the call to insertProject in an extra no-op microtask. The same goes for other methods here that got updated to async/await unnecessarily.

Copy link
Member Author

Choose a reason for hiding this comment

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

K, well, I did do this manually, so that's on me 😝

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressed here: 7b302d4 (#31471)

this.originalEnv = originalEnv

nock.disableNetConnect()
nock.enableNetConnect(/localhost/)

// always clean up the cache
// before each test
return cache.remove()
await cache.remove()
Copy link
Contributor

Choose a reason for hiding this comment

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

Unlike the previous async/await changes, I think this one is good to keep, as it clarifies that the beforeEach is asynchronous.

The logic here is:

  • If the function just returns the value that another function returns, and that value is already wrapped in a promise, simply return the execution of that function rather than making the function async and awaiting it. This goes double for functions that only serve to invoke a single async function.
  • If the function requires the await for its other operations (like wrapping it in a try/catch, or performing additional operations subsequent to the await), prefer async/await
  • always have test hooks and definitions async/await even if there is only one async call, as readability is preferable over the (slight) performance hit of an unnecessary microtask

PROJECTS: [],
PROJECT_PREFERENCES: {},
PROJECTS_CONFIG: {},
COHORTS: {},
}
},

_applyRewriteRules (obj = {}) {
_applyRewriteRules (obj: Partial<Cache> = {}): Cache {
Copy link
Contributor

Choose a reason for hiding this comment

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

Copilot is correct about this, but it doesn't go far enough - there are further improvements!

  • inlining the convertProjectsToArray and renameSessionToken into _applyRewriteRules
  • removing the reduce
  • defining a LegacyCache interface and isLegacyCache type guard function
  • changing the signature to (cache: LegacyCache) => Cache, lifting the decision to call or not to the caller, and renaming to convertLegacyCacheToCache

But I don't actually see where _applyRewriteRules is invoked, other than test/unit/cache_spec.js. This may be dead code that can be removed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Removal of dead code here: f56e73e (#31471)

return this._getProjects(tx).then((projects) => {
const pathsToRemove = Promise.reduce(projects, (memo, path) => {
const pathsToRemove = Promise.reduce(projects, (memo: string[], path) => {
return fs.statAsync(path)
.catch(() => {
return memo.push(path)
Copy link
Contributor

@cacieprins cacieprins Apr 9, 2025

Choose a reason for hiding this comment

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

copilot was right in one of its low-confidence comments (probably also omitted because this specific line wasn't modified), but this is a function that could really benefit from unwrapping its promises. Returning memo.push here is, like it said, really weird, as it's not returning a promise that should be awaited, and its return value is otherwise unimportant. (it's overwritten by the .return())

Copy link
Member Author

Choose a reason for hiding this comment

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

@cacieprins Yah, Cursor also made this suggestion actually. Updated here: 75e03c9 (#31471)

@jennifer-shehane jennifer-shehane merged commit 6b28356 into develop Apr 10, 2025
82 of 88 checks passed
@jennifer-shehane jennifer-shehane deleted the server-js-to-ts-cache branch April 10, 2025 19:03
cypress-app-bot pushed a commit that referenced this pull request Apr 13, 2025
cypress-app-bot pushed a commit that referenced this pull request Apr 13, 2025
cypress-app-bot pushed a commit that referenced this pull request Apr 13, 2025
cypress-app-bot pushed a commit that referenced this pull request Apr 13, 2025
cypress-app-bot pushed a commit that referenced this pull request Apr 13, 2025
cypress-app-bot pushed a commit that referenced this pull request Apr 13, 2025
cypress-app-bot pushed a commit that referenced this pull request Apr 14, 2025
cypress-app-bot pushed a commit that referenced this pull request Apr 14, 2025
cypress-app-bot pushed a commit that referenced this pull request Apr 14, 2025
cypress-app-bot pushed a commit that referenced this pull request Apr 14, 2025
cypress-app-bot pushed a commit that referenced this pull request Apr 14, 2025
cypress-app-bot pushed a commit that referenced this pull request Apr 14, 2025
jennifer-shehane pushed a commit that referenced this pull request Apr 14, 2025
* chore: updating v8 snapshot cache

* index on develop: 6b28356 chore: convert cache.js file within server to TypeScript (#31471)

* index on develop: 6b28356 chore: convert cache.js file within server to TypeScript (#31471)

---------

Co-authored-by: cypress-bot[bot] <+cypress-bot[bot]@users.noreply.github.com>
jennifer-shehane pushed a commit that referenced this pull request Apr 14, 2025
* chore: updating v8 snapshot cache

* index on develop: 6b28356 chore: convert cache.js file within server to TypeScript (#31471)

* index on develop: 6b28356 chore: convert cache.js file within server to TypeScript (#31471)

---------

Co-authored-by: cypress-bot[bot] <+cypress-bot[bot]@users.noreply.github.com>
jennifer-shehane pushed a commit that referenced this pull request Apr 14, 2025
* chore: updating v8 snapshot cache

* index on develop: 6b28356 chore: convert cache.js file within server to TypeScript (#31471)

* index on develop: 6b28356 chore: convert cache.js file within server to TypeScript (#31471)

---------

Co-authored-by: cypress-bot[bot] <+cypress-bot[bot]@users.noreply.github.com>
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