Skip to content

Commit 28a9123

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

File tree

65 files changed

+7170
-327
lines changed

Some content is hidden

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

65 files changed

+7170
-327
lines changed

circle.yml

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

158+
restore_cached_system_tests_deps:
159+
description: 'Restore the cached node_modules for projects in "system-tests/projects/**"'
160+
steps:
161+
- run:
162+
name: Generate Circle Cache key for system tests
163+
command: ./system-tests/scripts/cache-key.sh > system_tests_cache_key
164+
- restore_cache:
165+
name: Restore system tests node_modules cache
166+
keys:
167+
- v{{ .Environment.CACHE_VERSION }}-{{ arch }}-system-tests-projects-node-modules-cache-{{ checksum "system_tests_cache_key" }}
168+
- v{{ .Environment.CACHE_VERSION }}-{{ arch }}-system-tests-projects-node-modules-cache-
169+
170+
update_cached_system_tests_deps:
171+
description: 'Update the cached node_modules for projects in "system-tests/projects/**"'
172+
steps:
173+
- run:
174+
name: Generate Circle Cache key for system tests
175+
command: ./system-tests/scripts/cache-key.sh > system_tests_cache_key
176+
- restore_cache:
177+
name: Restore cache state, to check for known modules cache existence
178+
keys:
179+
- v{{ .Environment.CACHE_VERSION }}-{{ arch }}-system-tests-projects-node-modules-cache-state-{{ checksum "system_tests_cache_key" }}
180+
- run:
181+
name: Bail if specific cache exists
182+
command: |
183+
if [[ -f "system_tests_node_modules_installed" ]]; then
184+
echo "No updates to system tests node modules, exiting"
185+
circleci-agent step halt
186+
fi
187+
- restore_cache:
188+
name: Restore system tests node_modules cache
189+
keys:
190+
- v{{ .Environment.CACHE_VERSION }}-{{ arch }}-system-tests-projects-node-modules-cache-{{ checksum "system_tests_cache_key" }}
191+
- v{{ .Environment.CACHE_VERSION }}-{{ arch }}-system-tests-projects-node-modules-cache-
192+
- run:
193+
name: Update system-tests node_modules cache
194+
command: yarn workspace @tooling/system-tests projects:yarn:install
195+
- save_cache:
196+
name: Save system tests node_modules cache
197+
key: v{{ .Environment.CACHE_VERSION }}-{{ arch }}-system-tests-projects-node-modules-cache-{{ checksum "system_tests_cache_key" }}
198+
paths:
199+
- ~/.cache/cy-system-tests-node-modules
200+
- run: touch system_tests_node_modules_installed
201+
- save_cache:
202+
name: Save system tests node_modules cache state key
203+
key: v{{ .Environment.CACHE_VERSION }}-{{ arch }}-system-tests-projects-node-modules-cache-state-{{ checksum "system_tests_cache_key" }}
204+
paths:
205+
- system_tests_node_modules_installed
206+
158207
caching-dependency-installer:
159208
description: 'Installs & caches the dependencies based on yarn lock & package json dependencies'
160209
parameters:
@@ -468,6 +517,7 @@ commands:
468517
type: string
469518
steps:
470519
- restore_cached_workspace
520+
- restore_cached_system_tests_deps
471521
- run:
472522
name: Run system tests
473523
command: |
@@ -1154,6 +1204,12 @@ jobs:
11541204
path: /tmp/artifacts
11551205
- store-npm-logs
11561206

1207+
system-tests-node-modules-install:
1208+
<<: *defaults
1209+
steps:
1210+
- restore_cached_workspace
1211+
- update_cached_system_tests_deps
1212+
11571213
system-tests-chrome:
11581214
<<: *defaults
11591215
resource_class: medium
@@ -2125,19 +2181,22 @@ linux-workflow: &linux-workflow
21252181
- server-performance-tests:
21262182
requires:
21272183
- build
2128-
- system-tests-chrome:
2184+
- system-tests-node-modules-install:
21292185
requires:
21302186
- build
2187+
- system-tests-chrome:
2188+
requires:
2189+
- system-tests-node-modules-install
21312190
- system-tests-electron:
21322191
requires:
2133-
- build
2192+
- system-tests-node-modules-install
21342193
- system-tests-firefox:
21352194
requires:
2136-
- build
2195+
- system-tests-node-modules-install
21372196
- system-tests-non-root:
21382197
executor: non-root-docker-user
21392198
requires:
2140-
- build
2199+
- system-tests-node-modules-install
21412200
- driver-integration-tests-chrome:
21422201
requires:
21432202
- 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
@@ -39,7 +39,7 @@ const registerHandler = (handler) => {
3939
const init = (config, options, ctx) => {
4040
// test and warn for incompatible plugin
4141
try {
42-
const retriesPluginPath = path.dirname(resolve.sync('cypress-plugin-retries', {
42+
const retriesPluginPath = path.dirname(resolve.sync('cypress-plugin-retries/package.json', {
4343
basedir: options.projectRoot,
4444
}))
4545

packages/server/test/integration/http_requests_spec.js

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

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

4949
const replaceAbsolutePaths = (content) => {

packages/server/test/integration/plugins_spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ const { makeLegacyDataContext } = require('../../lib/makeDataContext')
77
let ctx
88

99
describe('lib/plugins', () => {
10+
<<<<<<< HEAD
1011
beforeEach(() => {
1112
ctx = makeLegacyDataContext()
1213
Fixtures.scaffold()
1314

1415
ctx.actions.project.setActiveProjectForTestSetup(Fixtures.projectPath('plugin-before-browser-launch-deprecation'))
16+
=======
17+
beforeEach(async () => {
18+
Fixtures.scaffoldProject('plugin-before-browser-launch-deprecation')
19+
await Fixtures.scaffoldCommonNodeModules()
20+
>>>>>>> 71d92e0e49 (test: node_modules installs for system-tests, other improvements (#18574))
1521
})
1622

1723
afterEach(() => {

packages/server/test/unit/files_spec.js

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

35-
expect(filePath).to.include('/.projects/todos/tests/_fixtures/message.txt')
35+
expect(filePath).to.include('/cy-projects/todos/tests/_fixtures/message.txt')
3636
})
3737
})
3838

@@ -76,7 +76,7 @@ describe('lib/files', () => {
7676
return files.readFile(this.projectRoot, '.projects/write_file.txt').then(({ contents, filePath }) => {
7777
expect(contents).to.equal('foo')
7878

79-
expect(filePath).to.include('/.projects/todos/.projects/write_file.txt')
79+
expect(filePath).to.include('/cy-projects/todos/.projects/write_file.txt')
8080
})
8181
})
8282
})

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)