Skip to content
This repository was archived by the owner on Mar 5, 2022. It is now read-only.

Commit 4fb01c0

Browse files
Create interactive installation wizard (#433)
1 parent 8981602 commit 4fb01c0

29 files changed

+20531
-13139
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ package-lock.json
88
.nyc_output
99
coverage
1010
*.generated.css
11-
.next
11+
.next
12+
bin

.vscode/launch.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch init script",
11+
"runtimeArgs": ["--nolazy", "-r", "ts-node/register"],
12+
"skipFiles": ["<node_internals>/**"],
13+
"cwd": "${workspaceRoot}",
14+
"program": "${workspaceFolder}/init/init.ts",
15+
"outFiles": ["${workspaceFolder}/**/*.js"]
16+
},
17+
{
18+
"type": "node",
19+
"request": "launch",
20+
"name": "Jest Current File",
21+
"program": "${workspaceFolder}/node_modules/.bin/jest",
22+
"args": ["--runInBand", "${fileBasenameNoExtension}"],
23+
"sourceMaps": true,
24+
"console": "integratedTerminal",
25+
"internalConsoleOptions": "neverOpen",
26+
"disableOptimisticBPs": true
27+
}
28+
]
29+
}

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ Requires [Node](https://nodejs.org/en/) version 8 or above.
7070
npm install --save-dev cypress cypress-react-unit-test
7171
```
7272

73+
## Init
74+
75+
You can use our command line wizard to give you instructions on configuring this plugin. It will try to determine which framework or bundling tool you are using and give you instructions on right configuration.
76+
77+
```sh
78+
cypress-react-unit-test init
79+
```
80+
81+
Or continue with manual installation:
82+
7383
1. Include this plugin from your project's `cypress/support/index.js`
7484

7585
```js

__mocks__/fs.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// __mocks__/fs.js
2+
process.chdir('/')
3+
module.exports = require('memfs').fs

circle.yml

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@ workflows:
2525
- run:
2626
name: Build folder 🏗
2727
command: npm run build
28+
- run:
29+
name: Run unit tests 👷
30+
command: npm run test:unit
2831

2932
- cypress/run:
3033
name: Example A11y
3134
requires:
3235
- Install
3336
executor: cypress/base-12
3437
# each example installs "cypress-react-unit-test" as a local dependency (symlink)
35-
install-command: npm install
38+
install-command: npm install --no-bin-links
3639
verify-command: echo 'Already verified'
3740
no-workspace: true
3841
working_directory: examples/a11y
@@ -45,9 +48,9 @@ workflows:
4548
- Install
4649
executor: cypress/base-12
4750
# each example installs "cypress-react-unit-test" as a local dependency (symlink)
48-
install-command: npm install
51+
install-command: npm install --no-bin-links
4952
verify-command: echo 'Already verified'
50-
no-workspace: true
53+
no-workspace: false
5154
working_directory: examples/using-babel
5255
command: npm test
5356
store_artifacts: true
@@ -58,7 +61,7 @@ workflows:
5861
- Install
5962
executor: cypress/base-12
6063
# each example installs "cypress-react-unit-test" as a local dependency (symlink)
61-
install-command: npm install
64+
install-command: npm install --no-bin-links
6265
verify-command: echo 'Already verified'
6366
no-workspace: true
6467
working_directory: examples/using-babel-typescript
@@ -71,7 +74,7 @@ workflows:
7174
- Install
7275
executor: cypress/base-12
7376
# each example installs "cypress-react-unit-test" as a local dependency (symlink)
74-
install-command: npm install
77+
install-command: npm install --no-bin-links
7578
verify-command: echo 'Already verified'
7679
no-workspace: true
7780
working_directory: examples/react-scripts
@@ -91,7 +94,7 @@ workflows:
9194
- Install
9295
executor: cypress/base-12
9396
# each example installs "cypress-react-unit-test" as a local dependency (symlink)
94-
install-command: npm install
97+
install-command: npm install --no-bin-links
9598
verify-command: echo 'Already verified'
9699
no-workspace: true
97100
working_directory: examples/nextjs
@@ -113,7 +116,7 @@ workflows:
113116
requires:
114117
- Install
115118
# each example installs "cypress-react-unit-test" as a local dependency (symlink)
116-
install-command: npm install
119+
install-command: npm install --no-bin-links
117120
verify-command: echo 'Already verified'
118121
no-workspace: true
119122
working_directory: examples/react-scripts-folder
@@ -133,7 +136,7 @@ workflows:
133136
- Install
134137
# each example installs "cypress-react-unit-test" as a local dependency (symlink)
135138
executor: cypress/base-12
136-
install-command: npm install
139+
install-command: npm install --no-bin-links
137140
verify-command: echo 'Already verified'
138141
no-workspace: true
139142
working_directory: examples/tailwind
@@ -155,7 +158,7 @@ workflows:
155158
- Install
156159
# each example installs "cypress-react-unit-test" as a local dependency (symlink)
157160
executor: cypress/base-12
158-
install-command: npm install
161+
install-command: npm install --no-bin-links
159162
verify-command: echo 'Already verified'
160163
no-workspace: true
161164
working_directory: examples/webpack-file
@@ -175,7 +178,7 @@ workflows:
175178
- Install
176179
# each example installs "cypress-react-unit-test" as a local dependency (symlink)
177180
executor: cypress/base-12
178-
install-command: npm install
181+
install-command: npm install --no-bin-links
179182
verify-command: echo 'Already verified'
180183
no-workspace: true
181184
working_directory: examples/webpack-options
@@ -197,7 +200,7 @@ workflows:
197200
# because we will use native Sass dependency
198201
executor: cypress/base-12
199202
# each example installs "cypress-react-unit-test" as a local dependency (symlink)
200-
install-command: npm install
203+
install-command: npm install --no-bin-links
201204
verify-command: echo 'Already verified'
202205
no-workspace: true
203206
working_directory: examples/sass-and-ts
@@ -217,7 +220,7 @@ workflows:
217220
- Install
218221
executor: cypress/base-12
219222
# each example installs "cypress-react-unit-test" as a local dependency (symlink)
220-
install-command: npm install
223+
install-command: npm install --no-bin-links
221224
verify-command: echo 'Already verified'
222225
no-workspace: true
223226
working_directory: examples/snapshots
@@ -230,7 +233,7 @@ workflows:
230233
requires:
231234
- Install
232235
# each example installs "cypress-react-unit-test" as a local dependency (symlink)
233-
install-command: npm install
236+
install-command: npm install --no-bin-links
234237
verify-command: echo 'Already verified'
235238
no-workspace: true
236239
working_directory: examples/visual-sudoku
@@ -246,7 +249,7 @@ workflows:
246249
requires:
247250
- Install
248251
# each example installs "cypress-react-unit-test" as a local dependency (symlink)
249-
install-command: npm install
252+
install-command: npm install --no-bin-links
250253
verify-command: echo 'Already verified'
251254
no-workspace: true
252255
working_directory: examples/visual-testing-with-applitools
@@ -268,7 +271,7 @@ workflows:
268271
requires:
269272
- Install
270273
# each example installs "cypress-react-unit-test" as a local dependency (symlink)
271-
install-command: npm install
274+
install-command: npm install --no-bin-links
272275
verify-command: echo 'Already verified'
273276
no-workspace: true
274277
working_directory: examples/visual-testing-with-percy
@@ -283,7 +286,7 @@ workflows:
283286
requires:
284287
- Install
285288
# each example installs "cypress-react-unit-test" as a local dependency (symlink)
286-
install-command: npm install
289+
install-command: npm install --no-bin-links
287290
verify-command: echo 'Already verified'
288291
no-workspace: true
289292
working_directory: examples/visual-testing-with-happo

docs/recipes.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,37 @@ When loading your `.babelrc` settings, `cypress-react-unit-test` sets `BABEL_ENV
124124
```
125125

126126
See [examples/using-babel](examples/using-babel) folder for full example.
127+
128+
### Using rollup config
129+
130+
If you are using rollup for bundling – we can use it as well for the bundling. Check the example:
131+
132+
```js
133+
// // cypress/plugins/index.js
134+
const rollupPreprocessor = require('@bahmutov/cy-rollup')
135+
136+
module.exports = (on, config) => {
137+
on(
138+
'file:preprocessor',
139+
rollupPreprocessor({
140+
// this is the default value
141+
configFile: 'rollup.config.js',
142+
}),
143+
)
144+
145+
require('@cypress/code-coverage/task')(on, config)
146+
}
147+
```
148+
149+
But make sure that several rollup plugins are required in order to bundle the code for cypress.
150+
151+
```js
152+
// bundle node_modules
153+
nodeResolve(),
154+
// process commonjs modules
155+
commonjs(),
156+
// required for react (prop-types) sources
157+
replace({ 'process.env.NODE_ENV': JSON.stringify('development') }),
158+
```
159+
160+
See [examples/rollup](examples/rollup) folder for full example.

init/Template.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export interface Template<T = unknown> {
2+
message: string
3+
getExampleUrl: ({ componentFolder }: { componentFolder: string }) => string
4+
recommendedComponentFolder: string
5+
test(rootPath: string): { success: boolean; payload?: T }
6+
getPluginsCode: (
7+
payload: T | null,
8+
options: { cypressProjectRoot: string },
9+
) => string
10+
printHelper?: () => void
11+
}

init/findPackageJson.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import path from 'path'
2+
import fs from 'fs'
3+
import findUp from 'find-up'
4+
5+
type PackageJsonLike = {
6+
name?: string
7+
scripts?: Record<string, string>
8+
dependencies?: Record<string, string>
9+
devDependencies?: Record<string, string>
10+
[key: string]: unknown
11+
}
12+
13+
type FindPackageJsonResult =
14+
| {
15+
packageData: PackageJsonLike
16+
filename: string
17+
done: false
18+
}
19+
| {
20+
packageData: undefined
21+
filename: undefined
22+
done: true
23+
}
24+
25+
/**
26+
* Return the parsed package.json that we find in a parent folder.
27+
*
28+
* @returns {Object} Value, filename and indication if the iteration is done.
29+
*/
30+
export function createFindPackageJsonIterator(rootPath = process.cwd()) {
31+
function scanForPackageJson(cwd: string): FindPackageJsonResult {
32+
const packageJsonPath = findUp.sync('package.json', { cwd })
33+
if (!packageJsonPath) {
34+
return {
35+
packageData: undefined,
36+
filename: undefined,
37+
done: true,
38+
}
39+
}
40+
41+
const packageData = JSON.parse(
42+
fs.readFileSync(packageJsonPath, {
43+
encoding: 'utf-8',
44+
}),
45+
)
46+
47+
return {
48+
packageData,
49+
filename: packageJsonPath,
50+
done: false,
51+
}
52+
}
53+
54+
return {
55+
map: <TPayload>(
56+
cb: (
57+
data: PackageJsonLike,
58+
packageJsonPath: string,
59+
) => { continue: boolean; payload?: TPayload },
60+
) => {
61+
let stepPathToScan = rootPath
62+
63+
while (true) {
64+
const result = scanForPackageJson(stepPathToScan)
65+
66+
if (result.done) {
67+
// didn't find the package.json
68+
return { success: false }
69+
}
70+
71+
if (result.packageData) {
72+
const cbResult = cb(result.packageData, result.filename)
73+
if (!cbResult.continue) {
74+
return { success: true, payload: cbResult.payload }
75+
}
76+
}
77+
78+
const nextStepPathToScan = path.resolve(stepPathToScan, '..')
79+
if (nextStepPathToScan === stepPathToScan) {
80+
// we are at the root. Give up
81+
return { success: false }
82+
}
83+
84+
stepPathToScan = nextStepPathToScan
85+
}
86+
},
87+
}
88+
}

0 commit comments

Comments
 (0)