Skip to content

Commit 504b153

Browse files
authored
chore: convert @packages/data-context from mocha to jest (#32598)
* start jest conversion and covnert authaction to jest * chore: conver codegenaction to jest * chore: convert cohorts action spec to vitest * chore: convert dataEmitterActions to jest * chore: convert event collector action to vitest * chore: convert local settings action spec tojest * chore: convert notification action to vitest * chor: convert project actions to vitest and fix pretty-formnat * chore: convert code-generator spec to jest * chore: convert spec options spec to jest * chore: convert project config ipc to jest * chore: convert projectconfigmanager tests to jest * chore: convert project lifecycle manager tests to jest * chore: conver poller spec to jest * chore: convert browser data source to jest * chore: convert cloud dats source to jest * chore: convert gitdatasource unit tests to jest * chore: convert FileDataSource to jest * chore: convert git data source to jest * chore: convert graphql data source to jest * chore: convert project data source to jest * chore: convert revelvant runs data source to jest * chore: convert relevant run specs data source to jest * chore: convert remote request data source to jest * chore: convert versions data source spec to jest * chore: convert wizard data source spec to jest * chore: convert document node builder to jest * chore: convert has TypeScript to jest * chore: convert test counts spec to jest * chore: convert weighted choice spec to jest * chore: convert config file updater to jest * clean up files to allow tests to run in band but out of order, add README, cleanup unused scripts and dependencies * fix deprecation warning from ts-jest via isolated modules * chore: adjust expected result count * chore: clean up types * turn back on no implicit any and reinstall graphql package in frontend shared * fix isFocusSupported mock * chore: copy over the old data-context-helper into the server as the one in the data-context package is written in jest now * chore: fix code review findings from cursor * fix incorrect expect async throw * chore: remove use of this in projectData source * add docs to copied data-context helper and fix cursor detected issue
1 parent f9c3f75 commit 504b153

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2656
-1335
lines changed

.circleci/src/workflows/@workflows.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1784,7 +1784,7 @@ jobs:
17841784
source ./scripts/ensure-node.sh
17851785
yarn lerna run types
17861786
- sanitize-verify-and-store-mocha-results:
1787-
expectedResultCount: 14
1787+
expectedResultCount: 13
17881788

17891789
verify-release-readiness:
17901790
<<: *defaults

.circleci/workflows.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3486,7 +3486,7 @@ jobs:
34863486
yarn lerna run types
34873487
name: Test types
34883488
- sanitize-verify-and-store-mocha-results:
3489-
expectedResultCount: 14
3489+
expectedResultCount: 13
34903490
working_directory: ~/cypress
34913491
v8-integration-tests:
34923492
environment:

guides/esm-migration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
##### Binary Packages
8888

8989
- [x] packages/config ✅ **COMPLETED**
90-
- [ ] packages/data-context
90+
- [x] packages/data-context **COMPLETED** (migrated from `mocha`/`sinon`/`chai` to `jest`). See package README for more details as to why `jest` over `vitest`
9191
- [x] packages/driver ✅ **COMPLETED**
9292
- [x] packages/electron ✅ **COMPLETED**
9393
- [x] packages/error ✅ **COMPLETED**

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,6 @@
274274
"**/@types/cheerio": "0.22.21",
275275
"**/@types/enzyme": "3.10.5",
276276
"**/jquery": "3.7.1",
277-
"**/pretty-format": "26.4.0",
278277
"**/socket.io-parser": "4.0.5",
279278
"**/ua-parser-js": "0.7.33",
280279
"@definitelytyped/typescript-versions": "0.1.7",

packages/data-context/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo-project/src/components/App.jsx

packages/data-context/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,10 @@ export const Query = objectType({
157157
})
158158
}
159159
})
160-
```
160+
```
161+
162+
### Testing
163+
164+
This package uses [jest](https://jestjs.io/) to integration/unit test this package. [vitest](https://vitest.dev/) currently cannot be used due to how `graphql` in this package is loaded into the vitest service worker, which is likely due to multiple instances of the GraphQL server being available. Since `jest` runs in a shared process, we are currently leveraging `jest` in place of `vitest`.
165+
166+
Additionally, Because the [ProjectLifecycleManager](https://github.com/cypress-io/cypress/blob/v15.3.0/packages/data-context/src/data/ProjectLifecycleManager.ts#L436) changes the current working directory for the process, we cannot run the tests in parallel, hence why the `--runInBand` option is utilized for running the tests.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import type { Config } from 'jest'
2+
// @see https://kulshekhar.github.io/ts-jest/docs for documentation on ts-jest
3+
import { createDefaultPreset } from 'ts-jest'
4+
5+
const tsJestTransformCfg = createDefaultPreset({
6+
tsconfig: 'tsconfig.test.json',
7+
}).transform
8+
9+
export default async (): Promise<Config> => {
10+
return {
11+
testMatch: ['<rootDir>/test/**/*.spec.ts'],
12+
testEnvironment: 'node',
13+
transform: {
14+
...tsJestTransformCfg,
15+
},
16+
}
17+
}

packages/data-context/package.json

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"lint": "eslint --ext .js,.ts,.json, .",
1616
"nexus-build": "ts-node ./scripts/nexus-build.ts",
1717
"test": "yarn test-unit",
18-
"test-unit": "mocha -r @packages/ts/register --config ./test/.mocharc.js --reporter mocha-multi-reporters --reporter-options configFile=../../mocha-reporter-config.json",
18+
"test-debug": "node --experimental-vm-modules --inspect-brk node_modules/.bin/jest --runInBand --testTimeout=600000",
19+
"test-unit": "yarn node --experimental-vm-modules node_modules/.bin/jest --runInBand",
1920
"tslint": "tslint --config ../ts/tslint.json --project . --exclude ./src/gen/nxs.gen.ts"
2021
},
2122
"dependencies": {
@@ -101,24 +102,17 @@
101102
"@types/ejs": "^3.1.2",
102103
"@types/fs-extra": "^8.0.1",
103104
"@types/graphql-resolve-batch": "1.1.6",
105+
"@types/jest": "^30.0.0",
104106
"@types/micromatch": "4.0.9",
105-
"@types/mocha": "^8.0.3",
106107
"@types/parse-glob": "3.0.29",
107108
"@types/prettier": "2.4.3",
108109
"@types/server-destroy": "^1.0.1",
109-
"@types/sinon": "10.0.11",
110-
"@types/sinon-chai": "3.2.12",
111110
"@types/stringify-object": "^3.0.0",
112-
"chai": "^4.2.0",
113111
"chokidar": "3.6.0",
114112
"fs-extra": "9.1.0",
115-
"mocha": "7.0.1",
116-
"mocha-junit-reporter": "2.2.0",
117-
"mocha-multi-reporters": "1.5.1",
113+
"jest": "^30.1.3",
118114
"nexus": "^1.2.0-next.15",
119-
"sinon": "13.0.2",
120-
"sinon-chai": "3.7.0",
121-
"snap-shot-it": "7.9.10",
115+
"ts-jest": "^29.4.4",
122116
"tslint": "^6.1.3"
123117
},
124118
"files": [

packages/data-context/test/.mocharc.js

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,133 @@
1+
import { describe, expect, jest, it } from '@jest/globals'
12
import type { DataContext } from '../../../src'
23
import { AuthActions } from '../../../src/actions/AuthActions'
34
import { createTestDataContext } from '../helper'
4-
import sinon, { SinonStub } from 'sinon'
5-
import sinonChai from 'sinon-chai'
6-
import chai, { expect } from 'chai'
75
import { FoundBrowser } from '@packages/types'
86

9-
chai.use(sinonChai)
10-
117
describe('AuthActions', () => {
12-
context('.login', () => {
8+
describe('.login', () => {
139
let ctx: DataContext
1410
let actions: AuthActions
1511

1612
beforeEach(() => {
17-
sinon.restore()
18-
1913
ctx = createTestDataContext('open')
20-
21-
;(ctx._apis.authApi.logIn as SinonStub)
22-
.resolves({ name: 'steve', email: '[email protected]', authToken: 'foo' })
14+
jest.mocked(ctx._apis.authApi.logIn).mockResolvedValue({ name: 'steve', email: '[email protected]', authToken: 'foo' })
2315

2416
actions = new AuthActions(ctx)
2517
})
2618

2719
it('sets coreData.user', async () => {
20+
// @ts-expect-error - incorrect number of arguments
2821
await actions.login()
29-
expect(ctx.coreData.user).to.include({ name: 'steve', email: '[email protected]', authToken: 'foo' })
22+
expect(ctx.coreData.user).toEqual(expect.objectContaining({ name: 'steve', email: '[email protected]', authToken: 'foo' }))
3023
})
3124

3225
it('focuses the main window if there is no activeBrowser', async () => {
3326
ctx.coreData.activeBrowser = null
3427

28+
// @ts-expect-error - incorrect number of arguments
3529
await actions.login()
3630

37-
expect(ctx._apis.electronApi.focusMainWindow).to.be.calledOnce
38-
expect(ctx._apis.browserApi.focusActiveBrowserWindow).to.not.be.called
31+
expect(ctx._apis.electronApi.focusMainWindow).toHaveBeenCalledTimes(1)
32+
expect(ctx._apis.browserApi.focusActiveBrowserWindow).not.toHaveBeenCalled()
3933
})
4034

4135
it('focuses the main window if the activeBrowser does not support focus', async () => {
4236
const browser = ctx.coreData.activeBrowser = { name: 'foo' } as FoundBrowser
4337

44-
sinon.stub(ctx.browser, 'isFocusSupported').withArgs(browser).resolves(false)
38+
jest.spyOn(ctx.browser, 'isFocusSupported').mockImplementation((args) => {
39+
if (args === browser) {
40+
return Promise.resolve(false)
41+
}
42+
43+
return Promise.resolve(true)
44+
})
4545

46+
// @ts-expect-error - incorrect number of arguments
4647
await actions.login()
4748

48-
expect(ctx._apis.electronApi.focusMainWindow).to.be.calledOnce
49-
expect(ctx._apis.browserApi.focusActiveBrowserWindow).to.not.be.called
49+
expect(ctx._apis.electronApi.focusMainWindow).toHaveBeenCalledTimes(1)
50+
expect(ctx._apis.browserApi.focusActiveBrowserWindow).not.toHaveBeenCalled()
5051
})
5152

5253
it('focuses the main window if the activeBrowser supports focus, but browser is closed', async () => {
5354
const browser = ctx.coreData.activeBrowser = { name: 'foo' } as FoundBrowser
5455

5556
ctx.coreData.app.browserStatus = 'closed'
5657

57-
sinon.stub(ctx.browser, 'isFocusSupported').withArgs(browser).resolves(true)
58+
jest.spyOn(ctx.browser, 'isFocusSupported').mockImplementation((args) => {
59+
if (args === browser) {
60+
return Promise.resolve(true)
61+
}
5862

63+
return Promise.resolve(false)
64+
})
65+
66+
// @ts-expect-error - incorrect number of arguments
5967
await actions.login()
6068

61-
expect(ctx._apis.electronApi.focusMainWindow).to.be.calledOnce
62-
expect(ctx._apis.browserApi.focusActiveBrowserWindow).to.not.be.called
69+
expect(ctx._apis.electronApi.focusMainWindow).toHaveBeenCalledTimes(1)
70+
expect(ctx._apis.browserApi.focusActiveBrowserWindow).not.toHaveBeenCalled()
6371
})
6472

6573
it('focuses the main window if the activeBrowser supports focus, but browser is opening', async () => {
6674
const browser = ctx.coreData.activeBrowser = { name: 'foo' } as FoundBrowser
6775

6876
ctx.coreData.app.browserStatus = 'opening'
6977

70-
sinon.stub(ctx.browser, 'isFocusSupported').withArgs(browser).resolves(true)
78+
jest.spyOn(ctx.browser, 'isFocusSupported').mockImplementation((args) => {
79+
if (args === browser) {
80+
return Promise.resolve(true)
81+
}
82+
83+
return Promise.resolve(false)
84+
})
7185

86+
// @ts-expect-error - incorrect number of arguments
7287
await actions.login()
7388

74-
expect(ctx._apis.electronApi.focusMainWindow).to.be.calledOnce
75-
expect(ctx._apis.browserApi.focusActiveBrowserWindow).to.not.be.called
89+
expect(ctx._apis.electronApi.focusMainWindow).toHaveBeenCalledTimes(1)
90+
expect(ctx._apis.browserApi.focusActiveBrowserWindow).not.toHaveBeenCalled()
7691
})
7792

7893
it('focuses the browser if the activeBrowser does support focus and browser is open', async () => {
7994
const browser = ctx.coreData.activeBrowser = { name: 'foo' } as FoundBrowser
8095

8196
ctx.coreData.app.browserStatus = 'open'
8297

83-
sinon.stub(ctx.browser, 'isFocusSupported').withArgs(browser).resolves(true)
98+
jest.spyOn(ctx.browser, 'isFocusSupported').mockImplementation((args) => {
99+
if (args === browser) {
100+
return Promise.resolve(true)
101+
}
84102

103+
return Promise.resolve(false)
104+
})
105+
106+
// @ts-expect-error - incorrect number of arguments
85107
await actions.login()
86108

87-
expect(ctx._apis.electronApi.focusMainWindow).to.not.be.called
88-
expect(ctx._apis.browserApi.focusActiveBrowserWindow).to.be.calledOnce
109+
expect(ctx._apis.electronApi.focusMainWindow).not.toHaveBeenCalled()
110+
expect(ctx._apis.browserApi.focusActiveBrowserWindow).toHaveBeenCalledTimes(1)
89111
})
90112

91113
it('does not focus anything if the activeBrowser does support focus but the main window is focused', async () => {
92114
const browser = ctx.coreData.activeBrowser = { name: 'foo' } as FoundBrowser
93115

94-
sinon.stub(ctx.browser, 'isFocusSupported').withArgs(browser).resolves(true)
116+
jest.spyOn(ctx.browser, 'isFocusSupported').mockImplementation((args) => {
117+
if (args === browser) {
118+
return Promise.resolve(true)
119+
}
120+
121+
return Promise.resolve(false)
122+
})
95123

96-
;(ctx._apis.electronApi.isMainWindowFocused as SinonStub).returns(true)
124+
jest.spyOn(ctx._apis.electronApi, 'isMainWindowFocused').mockReturnValue(true)
97125

126+
// @ts-expect-error - incorrect number of arguments
98127
await actions.login()
99128

100-
expect(ctx._apis.electronApi.focusMainWindow).to.not.be.called
101-
expect(ctx._apis.browserApi.focusActiveBrowserWindow).to.not.be.called
129+
expect(ctx._apis.electronApi.focusMainWindow).not.toHaveBeenCalled()
130+
expect(ctx._apis.browserApi.focusActiveBrowserWindow).not.toHaveBeenCalled()
102131
})
103132
})
104133
})

0 commit comments

Comments
 (0)