forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_utils_spec.ts
More file actions
176 lines (143 loc) · 5.33 KB
/
Copy pathproject_utils_spec.ts
File metadata and controls
176 lines (143 loc) · 5.33 KB
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
import Chai from 'chai'
import path from 'path'
import sinon from 'sinon'
import { fs } from '../../lib/util/fs'
import { getSpecUrl, checkSupportFile, getDefaultConfigFilePath } from '../../lib/project_utils'
import Fixtures from '@tooling/system-tests/lib/fixtures'
const todosPath = Fixtures.projectPath('todos')
const defaultProps = {
browserUrl: 'http://localhost:8888/__/',
componentFolder: path.join(todosPath, 'component'),
integrationFolder: path.join(todosPath, 'tests'),
projectRoot: todosPath,
specType: 'integration',
} as const
const expect = Chai.expect
describe('lib/project_utils', () => {
describe('getSpecUrl', () => {
it('normalizes to __all when absoluteSpecUrl is undefined', () => {
const str = getSpecUrl({
...defaultProps,
absoluteSpecPath: undefined,
})
expect(str).to.eq('http://localhost:8888/__/#/tests/__all')
})
it('normalizes to __all when absoluteSpecUrl is __all', () => {
const str = getSpecUrl({
...defaultProps,
absoluteSpecPath: '__all',
})
expect(str).to.eq('http://localhost:8888/__/#/tests/__all')
})
it('normalizes to __all when absoluteSpecUrl is __all', () => {
const str = getSpecUrl({
...defaultProps,
absoluteSpecPath: '__all',
})
expect(str).to.eq('http://localhost:8888/__/#/tests/__all')
})
it('returns fully qualified url when spec exists', function () {
const str = getSpecUrl({
...defaultProps,
absoluteSpecPath: 'cypress/integration/bar.js',
})
expect(str).to.eq('http://localhost:8888/__/#/tests/cypress/integration/bar.js')
})
it('returns fully qualified url on absolute path to spec', function () {
const todosSpec = path.join(todosPath, 'tests/sub/sub_test.coffee')
const str = getSpecUrl({
...defaultProps,
absoluteSpecPath: todosSpec,
})
expect(str).to.eq('http://localhost:8888/__/#/tests/integration/sub/sub_test.coffee')
})
it('escapses %, &', function () {
const todosSpec = path.join(todosPath, 'tests/sub/a&b%c.js')
const str = getSpecUrl({
...defaultProps,
absoluteSpecPath: todosSpec,
})
expect(str).to.eq('http://localhost:8888/__/#/tests/integration/sub/a%26b%25c.js')
})
// ? is invalid in Windows, but it can be tested here
// because it's a unit test and doesn't check the existence of files
it('escapes ?', function () {
const todosSpec = path.join(todosPath, 'tests/sub/a?.spec.js')
const str = getSpecUrl({
...defaultProps,
absoluteSpecPath: todosSpec,
})
expect(str).to.eq('http://localhost:8888/__/#/tests/integration/sub/a%3F.spec.js')
})
it('escapes %, &, ? in the url dir', function () {
const todosSpec = path.join(todosPath, 'tests/s%&?ub/a.spec.js')
const str = getSpecUrl({
...defaultProps,
absoluteSpecPath: todosSpec,
})
expect(str).to.eq('http://localhost:8888/__/#/tests/integration/s%25%26%3Fub/a.spec.js')
})
})
describe('checkSupportFile', () => {
it('does nothing when {supportFile: false}', async () => {
const ret = await checkSupportFile({
configFile: 'cypress.json',
supportFile: false,
})
expect(ret).to.be.undefined
})
it('throws when support file does not exist', async () => {
try {
await checkSupportFile({
configFile: 'cypress.json',
supportFile: '/this/file/does/not/exist/foo/bar/cypress/support/index.js',
})
} catch (e) {
expect(e.message).to.include('The support file is missing or invalid.')
}
})
})
describe('getDefaultConfigFilePath', () => {
let readdirStub
const projectRoot = '/a/project/root'
beforeEach(() => {
readdirStub = sinon.stub(fs, 'readdir')
})
afterEach(() => {
readdirStub.restore()
})
it('finds cypress.json when present', async () => {
readdirStub.withArgs(projectRoot).resolves(['cypress.json'])
const ret = await getDefaultConfigFilePath(projectRoot)
expect(ret).to.equal('cypress.json')
})
it('defaults to cypress.config.js when present', async () => {
readdirStub.withArgs(projectRoot).resolves(['cypress.config.js'])
const ret = await getDefaultConfigFilePath(projectRoot)
expect(ret).to.equal('cypress.config.js')
})
it('defaults to cypress.json when no file is returned', async () => {
readdirStub.withArgs(projectRoot).resolves([])
const ret = await getDefaultConfigFilePath(projectRoot)
expect(ret).to.equal('cypress.json')
})
it('errors if two default files are present', async () => {
readdirStub.withArgs(projectRoot).resolves(['cypress.config.js', 'cypress.json'])
try {
await getDefaultConfigFilePath(projectRoot)
throw Error('should have failed')
} catch (err) {
expect(err).to.have.property('type', 'CONFIG_FILES_LANGUAGE_CONFLICT')
}
})
it('errors if no file is present and we asked not to create any', async () => {
readdirStub.withArgs(projectRoot).resolves([])
try {
await getDefaultConfigFilePath(projectRoot, false)
throw Error('should have failed')
} catch (err) {
expect(err).to.have.property('type', 'NO_DEFAULT_CONFIG_FILE_FOUND')
}
})
})
})