forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect.spec.ts
426 lines (305 loc) · 13.9 KB
/
detect.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import Module from 'module'
import { expect } from 'chai'
import fs from 'fs-extra'
import globby from 'globby'
import type { ProjectFixtureDir } from '@tooling/system-tests'
import { detectFramework, detectLanguage, PkgJson } from '../../src'
import Fixtures from '@tooling/system-tests'
import path from 'path'
beforeEach(() => {
// @ts-ignore
Module._cache = Object.create(null)
// @ts-ignore
Module._pathCache = Object.create(null)
require.cache = Object.create(null)
})
export async function scaffoldMigrationProject (project: ProjectFixtureDir) {
const projectPath = Fixtures.projectPath(project)
Fixtures.clearFixtureNodeModules(project)
Fixtures.removeProject(project)
await Fixtures.scaffoldProject(project)
return projectPath
}
interface DepToFake {
dependency: string
version: string
}
interface DevDepToFake {
devDependency: string
version: string
}
/**
* The way we detect dependencies is by using resolve-from (https://www.npmjs.com/package/resolve-from).
* In these unit tests, we don't want to actually run `npm install`, since it is slow,
* so this function fakes that the dependencies are installed by creating pretend dependency like this:
* `node_modules/<dependency>/package.json.
* Inside `package.json` we add the minimal:
*
* {
* "version": "5.0.0",
* "main": "index.js"
* }
*
* We have some real e2e tests that actually run `npm install`.
* Those are in launchpad/cypress/e2e/scaffold-component-testing.cy.ts.
*/
function fakeDepsInNodeModules (cwd: string, deps: Array<DepToFake | DevDepToFake>) {
fs.mkdirSync(path.join(cwd, 'node_modules'))
for (const dep of deps) {
const depName = 'dependency' in dep ? dep.dependency : dep.devDependency
const nodeModules = path.join(cwd, 'node_modules', depName)
fs.mkdirpSync(nodeModules)
fs.writeJsonSync(
path.join(cwd, 'node_modules', depName, 'package.json'),
{ main: 'index.js', version: dep.version },
)
fs.writeFileSync(
path.join(cwd, 'node_modules', depName, 'index.js'),
'export STUB = true',
)
}
}
describe('detectFramework', () => {
it('Create React App v4', async () => {
const projectPath = await scaffoldMigrationProject('create-react-app-unconfigured')
fakeDepsInNodeModules(projectPath, [{ dependency: 'react-scripts', version: '5.0.0' }])
const actual = await detectFramework(projectPath)
expect(actual.framework?.type).to.eq('reactscripts')
})
it('Create React App v5', async () => {
const projectPath = await scaffoldMigrationProject('create-react-app-unconfigured')
fakeDepsInNodeModules(projectPath, [{ dependency: 'react-scripts', version: '4.0.0' }])
const actual = await detectFramework(projectPath)
expect(actual.framework?.type).to.eq('reactscripts')
})
it('React App with webpack 5', async () => {
const projectPath = await scaffoldMigrationProject('react-app-webpack-5-unconfigured')
fakeDepsInNodeModules(projectPath, [
{ dependency: 'react', version: '16.0.0' },
{ devDependency: 'webpack', version: '5.0.0' },
])
const actual = await detectFramework(projectPath)
expect(actual.framework?.type).to.eq('react')
expect(actual.bundler?.type).to.eq('webpack')
})
it(`Vue CLI w/ Vue 2`, async () => {
const projectPath = await scaffoldMigrationProject('vueclivue2-unconfigured')
fakeDepsInNodeModules(projectPath, [
{ devDependency: '@vue/cli-service', version: '4.0.0' },
{ dependency: 'vue', version: '2.5.0' },
])
const actual = await detectFramework(projectPath)
expect(actual.framework?.type).to.eq('vueclivue2')
expect(actual.bundler?.type).to.eq('webpack')
})
it(`Vue CLI 5 w/ Vue 3`, async () => {
const projectPath = await scaffoldMigrationProject('vuecli5vue3-unconfigured')
fakeDepsInNodeModules(projectPath, [
{ devDependency: '@vue/cli-service', version: '5.0.0' },
{ dependency: 'vue', version: '3.2.0' },
])
const actual = await detectFramework(projectPath)
expect(actual.framework?.type).to.eq('vueclivue3')
expect(actual.bundler?.type).to.eq('webpack')
})
it(`Vue CLI w/ Vue 3`, async () => {
const projectPath = await scaffoldMigrationProject('vueclivue3-unconfigured')
fakeDepsInNodeModules(projectPath, [
{ devDependency: '@vue/cli-service', version: '5.0.0' },
{ dependency: 'vue', version: '3.2.0' },
])
const actual = await detectFramework(projectPath)
expect(actual.framework?.type).to.eq('vueclivue3')
expect(actual.bundler?.type).to.eq('webpack')
})
it(`React with Vite`, async () => {
const projectPath = await scaffoldMigrationProject('react-vite-ts-unconfigured')
fakeDepsInNodeModules(projectPath, [
{ devDependency: 'vite', version: '2.0.0' },
{ dependency: 'react', version: '17.0.0' },
])
const actual = await detectFramework(projectPath)
expect(actual.framework?.type).to.eq('react')
expect(actual.bundler?.type).to.eq('vite')
})
it(`React with Vite using pre-release version`, async () => {
const projectPath = await scaffoldMigrationProject('react-vite-ts-unconfigured')
fakeDepsInNodeModules(projectPath, [
{ devDependency: 'vite', version: '2.5.0-alpha.4' },
{ dependency: 'react', version: '17.0.0' },
])
const actual = await detectFramework(projectPath)
expect(actual.framework?.type).to.eq('react')
expect(actual.bundler?.type).to.eq('vite')
})
it(`Vue with Vite`, async () => {
const projectPath = await scaffoldMigrationProject('vue3-vite-ts-unconfigured')
fakeDepsInNodeModules(projectPath, [
{ devDependency: 'vite', version: '2.0.0' },
{ dependency: 'vue', version: '3.0.0' },
])
const actual = await detectFramework(projectPath)
expect(actual.framework?.type).to.eq('vue3')
expect(actual.bundler?.type).to.eq('vite')
})
;['10.0.0', '11.0.0', '12.0.0'].forEach((v) => {
it(`Next.js v${v}`, async () => {
const projectPath = await scaffoldMigrationProject('nextjs-unconfigured')
fakeDepsInNodeModules(projectPath, [
{ dependency: 'react', version: '18.0.0' },
{ dependency: 'next', version: v },
])
const actual = await detectFramework(projectPath)
expect(actual.framework?.type).to.eq('nextjs')
expect(actual.bundler?.type).to.eq('webpack')
})
})
;['13.0.0', '14.0.0'].forEach((v) => {
it(`Angular CLI v${v}`, async () => {
const projectPath = await scaffoldMigrationProject('angular-cli-unconfigured')
fakeDepsInNodeModules(projectPath, [
{ dependency: '@angular/cli', version: v },
])
const actual = await detectFramework(projectPath)
expect(actual.framework?.type).to.eq('angular')
expect(actual.bundler?.type).to.eq('webpack')
})
})
;['2.0.0', '3.0.0'].forEach((v) => {
it(`Svelte and Vite v${v}`, async () => {
const projectPath = await scaffoldMigrationProject('svelte-vite-unconfigured')
fakeDepsInNodeModules(projectPath, [
{ dependency: 'svelte', version: '3.0.0' },
{ dependency: 'vite', version: v },
])
const actual = await detectFramework(projectPath)
expect(actual.framework?.type).to.eq('svelte')
expect(actual.bundler?.type).to.eq('vite')
})
})
it(`Svelte and Webpack`, async () => {
const projectPath = await scaffoldMigrationProject('svelte-webpack-unconfigured')
fakeDepsInNodeModules(projectPath, [
{ dependency: 'svelte', version: '3.0.0' },
{ dependency: 'webpack', version: '5.0.0' },
])
const actual = await detectFramework(projectPath)
expect(actual.framework?.type).to.eq('svelte')
expect(actual.bundler?.type).to.eq('webpack')
})
it(`no framework or library`, async () => {
const projectPath = await scaffoldMigrationProject('pristine')
// remove common node_modules or we will find a bunch of frameworks
// we want to simulate someone having nothing installed, including
// monorepo like situations where there can be multiple levels of
// node_modules above the projectPath.
fs.rmSync(path.join(Fixtures.cyTmpDir, 'node_modules'), { recursive: true, force: true })
const actual = await detectFramework(projectPath)
expect(actual.framework).to.be.undefined
expect(actual.bundler).to.be.undefined
})
})
describe('detectLanguage', () => {
it('existing project with `cypress.config.ts`', async () => {
const projectRoot = await scaffoldMigrationProject('config-with-ts')
const actual = detectLanguage({ projectRoot, pkgJson: {} as PkgJson })
expect(actual).to.eq('ts')
})
it('existing project with `cypress.config.js`', async () => {
const projectRoot = await scaffoldMigrationProject('config-with-js')
const actual = detectLanguage({ projectRoot, pkgJson: {} as PkgJson })
expect(actual).to.eq('js')
})
it('pristine project with typescript in package.json', async () => {
const projectRoot = await scaffoldMigrationProject('pristine-yarn')
fakeDepsInNodeModules(projectRoot, [{ devDependency: 'typescript', version: '4.3.6' }])
const pkgJson = fs.readJsonSync(path.join(projectRoot, 'package.json'))
const actual = detectLanguage({ projectRoot, pkgJson })
expect(actual).to.eq('ts')
})
it('pristine project with root level tsconfig.json', async () => {
const projectRoot = await scaffoldMigrationProject('pristine-npm')
fakeDepsInNodeModules(projectRoot, [{ devDependency: 'typescript', version: '4.3.6' }])
const actual = detectLanguage({ projectRoot, pkgJson: {} as PkgJson })
expect(actual).to.eq('ts')
})
it('detects js if typescript is not resolvable when there is a tsconfig.json', async () => {
let projectRoot = await scaffoldMigrationProject('pristine-npm')
const actual = detectLanguage({ projectRoot, pkgJson: {} as PkgJson })
expect(actual).to.eq('js')
projectRoot = await scaffoldMigrationProject('pristine-npm')
fakeDepsInNodeModules(projectRoot, [{ devDependency: 'typescript', version: '4.3.6' }])
const actualTypescript = detectLanguage({ projectRoot, pkgJson: {} as PkgJson })
expect(actualTypescript).to.eq('ts')
})
it('pre-migration project with tsconfig.json in cypress directory', async () => {
const projectRoot = await scaffoldMigrationProject('migration')
fakeDepsInNodeModules(projectRoot, [{ devDependency: 'typescript', version: '4.3.6' }])
const actual = detectLanguage({ projectRoot, pkgJson: {} as PkgJson })
expect(actual).to.eq('ts')
})
const joinPosix = (...s: string[]) => path.join(...s).split(path.sep).join(path.posix.sep)
function removeAllTsFilesExcept (projectRoot: string, filename?: string) {
const files = globby.sync(joinPosix(projectRoot, '**/*.{ts,tsx}'), { onlyFiles: true })
for (const f of files) {
if (!filename) {
fs.rmSync(f)
} else if (!f.includes(filename)) {
fs.rmSync(f)
}
}
}
it('cypress.json project with only .d.ts files', async () => {
const projectRoot = await scaffoldMigrationProject('migration-dts-files-only')
fakeDepsInNodeModules(projectRoot, [{ devDependency: 'typescript', version: '4.3.6' }])
const actual = detectLanguage({ projectRoot, pkgJson: {} as PkgJson, isMigrating: true })
expect(actual).to.eq('js')
})
it('cypress.json project with a TypeScript supportFile', async () => {
const projectRoot = await scaffoldMigrationProject('migration-ts-files-only')
fakeDepsInNodeModules(projectRoot, [{ devDependency: 'typescript', version: '4.3.6' }])
removeAllTsFilesExcept(projectRoot, 'support')
const actual = detectLanguage({ projectRoot, pkgJson: {} as PkgJson })
expect(actual).to.eq('ts')
})
it('cypress.json project with a TypeScript pluginsFile', async () => {
const projectRoot = await scaffoldMigrationProject('migration-ts-files-only')
fakeDepsInNodeModules(projectRoot, [{ devDependency: 'typescript', version: '4.3.6' }])
removeAllTsFilesExcept(projectRoot, 'plugins')
const actual = detectLanguage({ projectRoot, pkgJson: {} as PkgJson })
expect(actual).to.eq('ts')
})
it('cypress.json project with a TypeScript integration specs', async () => {
const projectRoot = await scaffoldMigrationProject('migration-ts-files-only')
fakeDepsInNodeModules(projectRoot, [{ devDependency: 'typescript', version: '4.3.6' }])
// detected based on `integration/**/*.tsx
removeAllTsFilesExcept(projectRoot, 'integration')
const actual = detectLanguage({ projectRoot, pkgJson: {} as PkgJson })
expect(actual).to.eq('ts')
})
it('cypress.json project with a TypeScript integration spec', async () => {
const projectRoot = await scaffoldMigrationProject('migration-ts-files-only')
fakeDepsInNodeModules(projectRoot, [{ devDependency: 'typescript', version: '4.3.6' }])
// detected based on `integration/**/*.tsx
removeAllTsFilesExcept(projectRoot, 'integration')
const actual = detectLanguage({ projectRoot, pkgJson: {} as PkgJson })
expect(actual).to.eq('ts')
})
it('cypress.json project with a TypeScript commponent spec', async () => {
const projectRoot = await scaffoldMigrationProject('migration-ts-files-only')
fakeDepsInNodeModules(projectRoot, [{ devDependency: 'typescript', version: '4.3.6' }])
// detected based on `integration/**/*.tsx
removeAllTsFilesExcept(projectRoot, 'component')
const actual = detectLanguage({ projectRoot, pkgJson: {} as PkgJson })
expect(actual).to.eq('ts')
})
it('ignores node_modules when checking for tsconfig.json', async () => {
const projectRoot = await scaffoldMigrationProject('pristine-cjs-project')
fakeDepsInNodeModules(projectRoot, [{ devDependency: 'typescript', version: '4.3.6' }])
await fs.mkdirp(path.join(projectRoot, 'node_modules', 'some-node-module'))
await fs.writeFile(path.join(projectRoot, 'node_modules', 'some-node-module', 'tsconfig.json'), '')
const pkgJson = fs.readJsonSync(path.join(projectRoot, 'package.json'))
const actual = detectLanguage({ projectRoot, pkgJson })
expect(actual).to.eq('js')
})
})