Skip to content

Commit 71d92e0

Browse files
test: node_modules installs for system-tests, other improvements (#18574)
Co-authored-by: Emily Rohrbough <emilyrohrbough@users.noreply.github.com>
1 parent cbfb3ca commit 71d92e0

File tree

66 files changed

+7134
-418
lines changed

Some content is hidden

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

66 files changed

+7134
-418
lines changed

circle.yml

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,55 @@ commands:
149149
name: Restore all node_modules to proper workspace folders
150150
command: node scripts/circle-cache.js --action unpack
151151

152+
restore_cached_system_tests_deps:
153+
description: 'Restore the cached node_modules for projects in "system-tests/projects/**"'
154+
steps:
155+
- run:
156+
name: Generate Circle Cache key for system tests
157+
command: ./system-tests/scripts/cache-key.sh > system_tests_cache_key
158+
- restore_cache:
159+
name: Restore system tests node_modules cache
160+
keys:
161+
- v{{ .Environment.CACHE_VERSION }}-{{ arch }}-system-tests-projects-node-modules-cache-{{ checksum "system_tests_cache_key" }}
162+
- v{{ .Environment.CACHE_VERSION }}-{{ arch }}-system-tests-projects-node-modules-cache-
163+
164+
update_cached_system_tests_deps:
165+
description: 'Update the cached node_modules for projects in "system-tests/projects/**"'
166+
steps:
167+
- run:
168+
name: Generate Circle Cache key for system tests
169+
command: ./system-tests/scripts/cache-key.sh > system_tests_cache_key
170+
- restore_cache:
171+
name: Restore cache state, to check for known modules cache existence
172+
keys:
173+
- v{{ .Environment.CACHE_VERSION }}-{{ arch }}-system-tests-projects-node-modules-cache-state-{{ checksum "system_tests_cache_key" }}
174+
- run:
175+
name: Bail if specific cache exists
176+
command: |
177+
if [[ -f "system_tests_node_modules_installed" ]]; then
178+
echo "No updates to system tests node modules, exiting"
179+
circleci-agent step halt
180+
fi
181+
- restore_cache:
182+
name: Restore system tests node_modules cache
183+
keys:
184+
- v{{ .Environment.CACHE_VERSION }}-{{ arch }}-system-tests-projects-node-modules-cache-{{ checksum "system_tests_cache_key" }}
185+
- v{{ .Environment.CACHE_VERSION }}-{{ arch }}-system-tests-projects-node-modules-cache-
186+
- run:
187+
name: Update system-tests node_modules cache
188+
command: yarn workspace @tooling/system-tests projects:yarn:install
189+
- save_cache:
190+
name: Save system tests node_modules cache
191+
key: v{{ .Environment.CACHE_VERSION }}-{{ arch }}-system-tests-projects-node-modules-cache-{{ checksum "system_tests_cache_key" }}
192+
paths:
193+
- ~/.cache/cy-system-tests-node-modules
194+
- run: touch system_tests_node_modules_installed
195+
- save_cache:
196+
name: Save system tests node_modules cache state key
197+
key: v{{ .Environment.CACHE_VERSION }}-{{ arch }}-system-tests-projects-node-modules-cache-state-{{ checksum "system_tests_cache_key" }}
198+
paths:
199+
- system_tests_node_modules_installed
200+
152201
caching-dependency-installer:
153202
description: 'Installs & caches the dependencies based on yarn lock & package json dependencies'
154203
parameters:
@@ -427,6 +476,7 @@ commands:
427476
type: string
428477
steps:
429478
- restore_cached_workspace
479+
- restore_cached_system_tests_deps
430480
- run:
431481
name: Run system tests
432482
command: |
@@ -1083,6 +1133,12 @@ jobs:
10831133
path: /tmp/artifacts
10841134
- store-npm-logs
10851135

1136+
system-tests-node-modules-install:
1137+
<<: *defaults
1138+
steps:
1139+
- restore_cached_workspace
1140+
- update_cached_system_tests_deps
1141+
10861142
system-tests-chrome:
10871143
<<: *defaults
10881144
resource_class: medium
@@ -2007,19 +2063,22 @@ linux-workflow: &linux-workflow
20072063
- server-performance-tests:
20082064
requires:
20092065
- build
2010-
- system-tests-chrome:
2066+
- system-tests-node-modules-install:
20112067
requires:
20122068
- build
2069+
- system-tests-chrome:
2070+
requires:
2071+
- system-tests-node-modules-install
20132072
- system-tests-electron:
20142073
requires:
2015-
- build
2074+
- system-tests-node-modules-install
20162075
- system-tests-firefox:
20172076
requires:
2018-
- build
2077+
- system-tests-node-modules-install
20192078
- system-tests-non-root:
20202079
executor: non-root-docker-user
20212080
requires:
2022-
- build
2081+
- system-tests-node-modules-install
20232082
- driver-integration-tests-chrome:
20242083
requires:
20252084
- build

npm/webpack-preprocessor/lib/typescript-overrides.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ export const overrideSourceMaps = (sourceMap: boolean, typescriptPath?: string)
1717
return
1818
}
1919

20-
const typescript = require(typescriptPath || 'typescript') as typeof import('typescript')
20+
// when using webpack-preprocessor as a local filesystem dependency (`file:...`),
21+
// require(typescript) will resolve to this repo's `typescript` devDependency, not the
22+
// targeted project's `typescript`, which breaks monkeypatching. resolving from the
23+
// CWD avoids this issue.
24+
const projectTsPath = require.resolve(typescriptPath || 'typescript', {
25+
paths: [process.cwd()],
26+
})
27+
28+
const typescript = require(projectTsPath) as typeof import('typescript')
2129
const { createProgram } = typescript
2230

2331
debug('typescript found, overriding typescript.createProgram()')

npm/webpack-preprocessor/test/unit/typescript-overrides.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ describe('./lib/typescript-overrides', () => {
147147
const err = typescriptOverrides.overrideSourceMaps(true)
148148

149149
expect(err).to.be.instanceOf(Error)
150-
expect(err.message).to.eq(`Cannot find module 'typescript'`)
150+
expect(err.message).to.match(/Cannot find module '.*typescript\.js'/)
151151
})
152152
})
153153
})

packages/server/lib/plugins/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const init = (config, options) => {
4242

4343
// test and warn for incompatible plugin
4444
try {
45-
const retriesPluginPath = path.dirname(resolve.sync('cypress-plugin-retries', {
45+
const retriesPluginPath = path.dirname(resolve.sync('cypress-plugin-retries/package.json', {
4646
basedir: options.projectRoot,
4747
}))
4848

packages/server/test/integration/http_requests_spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ zlib = Promise.promisifyAll(zlib)
4242
// force supertest-session to use promises provided in supertest
4343
const session = proxyquire('supertest-session', { supertest })
4444

45-
const absolutePathRegex = /"\/[^{}]*?\.projects/g
45+
const absolutePathRegex = /"\/[^{}]*?cy-projects/g
4646
let sourceMapRegex = /\n\/\/# sourceMappingURL\=.*/
4747

4848
const replaceAbsolutePaths = (content) => {

packages/server/test/integration/plugins_spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ const Fixtures = require('@tooling/system-tests/lib/fixtures')
66
const pluginsFile = Fixtures.projectPath('plugin-before-browser-launch-deprecation/cypress/plugins/index.js')
77

88
describe('lib/plugins', () => {
9-
beforeEach(() => {
10-
Fixtures.scaffold()
9+
beforeEach(async () => {
10+
Fixtures.scaffoldProject('plugin-before-browser-launch-deprecation')
11+
await Fixtures.scaffoldCommonNodeModules()
1112
})
1213

1314
afterEach(() => {

packages/server/test/unit/files_spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('lib/files', () => {
2525
return files.readFile(this.projectRoot, 'tests/_fixtures/message.txt').then(({ contents, filePath }) => {
2626
expect(contents).to.eq('foobarbaz')
2727

28-
expect(filePath).to.include('/.projects/todos/tests/_fixtures/message.txt')
28+
expect(filePath).to.include('/cy-projects/todos/tests/_fixtures/message.txt')
2929
})
3030
})
3131

@@ -69,7 +69,7 @@ describe('lib/files', () => {
6969
return files.readFile(this.projectRoot, '.projects/write_file.txt').then(({ contents, filePath }) => {
7070
expect(contents).to.equal('foo')
7171

72-
expect(filePath).to.include('/.projects/todos/.projects/write_file.txt')
72+
expect(filePath).to.include('/cy-projects/todos/.projects/write_file.txt')
7373
})
7474
})
7575
})

scripts/binary/smoke.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,21 +159,24 @@ const runFailingProjectTest = function (buildAppExecutable, e2e) {
159159
.then(verifyScreenshots)
160160
}
161161

162-
const test = function (buildAppExecutable) {
163-
Fixtures.scaffold()
164-
162+
const test = async function (buildAppExecutable) {
163+
await Fixtures.scaffoldCommonNodeModules()
164+
Fixtures.scaffoldProject('e2e')
165165
const e2e = Fixtures.projectPath('e2e')
166166

167-
return runSmokeTest(buildAppExecutable)
168-
.then(() => {
169-
return runProjectTest(buildAppExecutable, e2e)
170-
}).then(() => {
171-
return runFailingProjectTest(buildAppExecutable, e2e)
172-
}).then(() => {
173-
return Fixtures.remove()
174-
})
167+
await runSmokeTest(buildAppExecutable)
168+
await runProjectTest(buildAppExecutable, e2e)
169+
await runFailingProjectTest(buildAppExecutable, e2e)
170+
Fixtures.remove()
175171
}
176172

177173
module.exports = {
178174
test,
179175
}
176+
177+
if (require.main === module) {
178+
const buildAppExecutable = path.join(__dirname, `../../build/${os.platform()}-unpacked/Cypress`)
179+
180+
console.log('Script invoked directly, running smoke tests.')
181+
test(buildAppExecutable)
182+
}

system-tests/README.md

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ These tests launch the [Cypress server](../packages/server) process for each tes
77

88
These tests run in CI in Electron, Chrome, and Firefox under the `system-tests` job family.
99

10-
## Running system tests
10+
## Running System Tests
1111

1212
```bash
1313
yarn test <path/to/test>
@@ -28,14 +28,14 @@ To debug the Cypress process under test, you can pass `--cypress-inspect-brk`:
2828
yarn test test/go_spec.js --browser chrome --no-exit
2929
```
3030

31-
## Developing tests
31+
## Developing Tests
3232

3333
System tests cover the entire Cypress run, so they are good for testing features that do not fit into a normal integration or unit test. However, they do take more resources to run, so consider carefully if you really *need* to write a system test, or if you could achieve 100% coverage via an integration or unit test instead.
3434

3535
There are two parts to a system test:
3636

3737
1. A test written using the [`systemTests`](./lib/system-tests) Mocha wrapper that lives in [`./test`](./test), and
38-
2. A matching Cypress project that lives in the [`./projects`](./projects) directory.
38+
2. A matching Cypress [test project](#Test-Projects) that lives in the [`./projects`](./projects) directory.
3939

4040
For example, if you initialized a new project in `./projects/my-new-project`, and you wanted to assert that 2 tests fail and take a snapshot of the `stdout`, you'd write a test like this:
4141

@@ -61,10 +61,32 @@ From here, you could run this test with `yarn test my-new-project`.
6161

6262
There are many more options available for `systemTests.it` and `systemTests.setup`. You can massage the stdout, do pre-run tasks, set up HTTP/S servers, and more. Explore the typedocs in [`./lib/system-tests`](./lib/system-tests) for more information.
6363

64-
## Updating snaphots
64+
### Updating Snaphots
6565

6666
Prepend `SNAPSHOT_UPDATE=1` to any test command. See [`snap-shot-it` instructions](https://github.com/bahmutov/snap-shot-it#advanced-use) for more info.
6767

6868
```bash
6969
SNAPSHOT_UPDATE=1 yarn test go_spec
7070
```
71+
72+
### Test Projects
73+
74+
Every folder in [`./projects`](./lib/projects) represents a self-contained Cypress project. When you pass the `project` property to `systemTests.it` or `systemTests.exec`, Cypress launches using this project.
75+
76+
If a test project has a `package.json` file, the `systemTests.exec` helper will attempt to install the correct `node_modules` by running `yarn install` against the project. This is cached in CI and locally to speed up test times.
77+
78+
`systemTests.exec` *copies* the project directory to a temporary folder outside of the monorepo root. This means that temporary projects will not inherit the `node_modules` from this package or the monorepo. So, you must add the dependencies required for your project in `dependencies` or `devDependencies`.
79+
80+
The exception is some commonly used packages that are scaffolded for all projects, like `lodash` and `debug`. You can see the list by looking at `scaffoldCommonNodeModules` in [`./lib/fixtures.ts`](./lib/fixtures.ts) These packages do not need to be added to a test project's `package.json`.
81+
82+
You can also set special properties in a test project's `package.json` to influence the helper's behavior when running `yarn`:
83+
84+
`package.json` Property Name | Type | Description
85+
--- | --- | ---
86+
`_cySkipYarnInstall` | `boolean` | If `true`, skip the automatic `yarn install` for this package, even though it has a `package.json`.
87+
`_cyYarnV2` | `boolean` | Run the yarn v2-style install command instead of yarn v1-style.
88+
`_cyRunScripts` | `boolean` | By default, the automatic `yarn install` will not run postinstall scripts. This option, if set, will cause postinstall scripts to run for this project.
89+
90+
Run `yarn projects:yarn:install` to run `yarn install` for all projects with a `package.json`.
91+
92+
Use the `UPDATE_YARN_LOCK=1` environment variable with `yarn test` or `yarn projects:yarn:install` to allow the `yarn.lock` to be updated and synced back to the monorepo from the temp dir.

system-tests/__snapshots__/busted_support_file_spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Error: Webpack Compilation Error
2525
./cypress/support/index.js
2626
Module not found: Error: Can't resolve './does/not/exist' in '/foo/bar/.projects/busted-support-file/cypress/support'
2727
Looked for and couldn't find the file at the following paths:
28+
[/foo/bar/.projects/busted-support-file/cypress/support/package.json]
29+
[/foo/bar/.projects/busted-support-file/cypress/support/does/not/exist/package.json]
2830
[/foo/bar/.projects/busted-support-file/cypress/support/does/not/exist]
2931
[/foo/bar/.projects/busted-support-file/cypress/support/does/not/exist.js]
3032
[/foo/bar/.projects/busted-support-file/cypress/support/does/not/exist.json]

0 commit comments

Comments
 (0)