@sprucelabs/spruce-cli 15.1.8 → 15.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (24) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/build/__tests__/behavioral/AddingADependency.test.js.map +1 -1
  3. package/build/__tests__/behavioral/UpgradingANodeModule.test.js.map +1 -1
  4. package/build/__tests__/behavioral/events/CreatingAnEvent.test.js.map +1 -1
  5. package/build/__tests__/behavioral/tests/CreatingATest.test.d.ts +9 -1
  6. package/build/__tests__/behavioral/tests/CreatingATest.test.js +316 -26
  7. package/build/__tests__/behavioral/tests/CreatingATest.test.js.map +1 -1
  8. package/build/__tests__/behavioral/tests/SelectingAnAbstractTestClass.test.d.ts +5 -0
  9. package/build/__tests__/behavioral/tests/SelectingAnAbstractTestClass.test.js +331 -160
  10. package/build/__tests__/behavioral/tests/SelectingAnAbstractTestClass.test.js.map +1 -1
  11. package/build/features/test/actions/CreateAction.d.ts +2 -0
  12. package/build/features/test/actions/CreateAction.js +147 -46
  13. package/build/features/test/actions/CreateAction.js.map +1 -1
  14. package/build/tests/utilities/uiAssert.utility.d.ts +4 -3
  15. package/build/tests/utilities/uiAssert.utility.js +13 -3
  16. package/build/tests/utilities/uiAssert.utility.js.map +1 -1
  17. package/package.json +26 -26
  18. package/src/__tests__/behavioral/AddingADependency.test.ts +2 -2
  19. package/src/__tests__/behavioral/UpgradingANodeModule.test.ts +2 -2
  20. package/src/__tests__/behavioral/events/CreatingAnEvent.test.ts +2 -2
  21. package/src/__tests__/behavioral/tests/CreatingATest.test.ts +125 -11
  22. package/src/__tests__/behavioral/tests/SelectingAnAbstractTestClass.test.ts +62 -23
  23. package/src/features/test/actions/CreateAction.ts +83 -28
  24. package/src/tests/utilities/uiAssert.utility.ts +13 -2
@@ -1,7 +1,9 @@
1
+ import { diskUtil } from '@sprucelabs/spruce-skill-utils'
1
2
  import { test, assert } from '@sprucelabs/test'
2
3
  import LintService from '../../../services/LintService'
3
4
  import AbstractTestTest from '../../../tests/AbstractTestTest'
4
5
  import testUtil from '../../../tests/utilities/test.utility'
6
+ import uiAssert from '../../../tests/utilities/uiAssert.utility'
5
7
 
6
8
  export default class CreatingBehavioralTestsTest extends AbstractTestTest {
7
9
  @test()
@@ -10,7 +12,7 @@ export default class CreatingBehavioralTestsTest extends AbstractTestTest {
10
12
  }
11
13
 
12
14
  @test()
13
- protected static async requiresInstallIfSkilLNotInstalled() {
15
+ protected static async requiresInstallIfFeatureNotInstalled() {
14
16
  await this.installTests('testsInNodeModule')
15
17
 
16
18
  const testFeature = this.getFeatureInstaller().getFeature('test')
@@ -27,20 +29,12 @@ export default class CreatingBehavioralTestsTest extends AbstractTestTest {
27
29
  'AbstractSpruceFixtureTest'
28
30
  )
29
31
  @test(
30
- 'can create behavioral test with AbstractSpruceFixtureTest',
32
+ 'can create behavioral test with AbstractStoreTest',
31
33
  'AbstractStoreTest (requires install)'
32
34
  )
33
35
  protected static async canCreateBehavioralTest(testName: string) {
34
36
  LintService.enableLinting()
35
- await this.installTests()
36
- const promise = this.Action('test', 'create').execute({
37
- type: 'behavioral',
38
- nameReadable: 'Can book appointment',
39
- nameCamel: 'canBookAppointment',
40
- namePascal: 'CanBookAppointment',
41
- })
42
-
43
- await this.waitForInput()
37
+ const { promise } = await this.installAndStartTestActionAndWaitForInput()
44
38
 
45
39
  this.selectOptionBasedOnLabel(testName)
46
40
 
@@ -60,4 +54,124 @@ export default class CreatingBehavioralTestsTest extends AbstractTestTest {
60
54
  /false.*?does not equal.*?true/gis
61
55
  )
62
56
  }
57
+
58
+ @test('finds folders inside behavioral', 'behavioral')
59
+ @test('finds folders inside implementation', 'implementation')
60
+ protected static async promptsToSelectFolderIfInsideTestDir(
61
+ testType: string
62
+ ) {
63
+ await this.installTests()
64
+
65
+ this.createTestSubDir(testType, 'dummy1')
66
+
67
+ const { promise } = await this.installAndStartTestActionAndWaitForInput(
68
+ testType
69
+ )
70
+
71
+ uiAssert.assertSelectRenderChoice(this.ui, '.', testType)
72
+ uiAssert.assertSelectRenderChoice(this.ui, `dummy1`, `${testType}/dummy1`)
73
+
74
+ await this.ui.sendInput('.')
75
+ await this.waitAndSelectSubClass()
76
+
77
+ await promise
78
+ }
79
+
80
+ @test()
81
+ protected static async listsManyDirsIfExistInsideTestDir() {
82
+ await this.installTests()
83
+
84
+ const dirs = ['dir1', 'dir2', 'dir3']
85
+
86
+ for (const dir of dirs) {
87
+ this.createTestSubDir('behavioral', dir)
88
+ }
89
+
90
+ const { promise } = await this.installAndStartTestActionAndWaitForInput()
91
+
92
+ for (const dir of dirs) {
93
+ uiAssert.assertSelectRenderChoice(this.ui, `${dir}`, `behavioral/${dir}`)
94
+ }
95
+
96
+ await this.ui.sendInput('.')
97
+
98
+ await this.waitAndSelectSubClass()
99
+
100
+ await promise
101
+ }
102
+
103
+ @test('can select subdir 1', 'test')
104
+ @test('can select subdir 2', 'test-2')
105
+ protected static async selectingAnOptionRendersToSubDir(dirName: string) {
106
+ await this.installTests()
107
+ this.createTestSubDir('behavioral', dirName)
108
+
109
+ const { promise } = await this.installAndStartTestActionAndWaitForInput(
110
+ 'behavioral'
111
+ )
112
+
113
+ await this.ui.sendInput(`${dirName}`)
114
+
115
+ await this.waitAndSelectSubClass()
116
+
117
+ const results = await promise
118
+
119
+ const expectedPath = this.resolvePath(
120
+ 'src',
121
+ '__tests__',
122
+ 'behavioral',
123
+ dirName,
124
+ 'CanBookAppointment.test.ts'
125
+ )
126
+
127
+ assert.isEqual(expectedPath, results.files?.[0]?.path)
128
+ }
129
+
130
+ @test()
131
+ protected static async doesNotListFiles() {
132
+ await this.installTests()
133
+ this.createTestSubDir('behavioral', 'subdir')
134
+
135
+ const file = this.resolveTestDir('behavioral', 'test.ts')
136
+ diskUtil.writeFile(file, 'what the!?')
137
+
138
+ await this.installAndStartTestActionAndWaitForInput()
139
+
140
+ uiAssert.assertSelectDidNotRenderChoice(
141
+ this.ui,
142
+ 'test',
143
+ `behavioral/test/test.ts`
144
+ )
145
+
146
+ this.ui.reset()
147
+ }
148
+
149
+ private static createTestSubDir(...testDirs: string[]) {
150
+ const newDir = this.resolveTestDir(...testDirs)
151
+ diskUtil.createDir(newDir)
152
+ }
153
+
154
+ private static resolveTestDir(...testDirs: string[]) {
155
+ return this.resolvePath('src', '__tests__', ...testDirs)
156
+ }
157
+
158
+ private static async installAndStartTestActionAndWaitForInput(
159
+ testType = 'behavioral'
160
+ ) {
161
+ await this.installTests()
162
+ const promise = this.Action('test', 'create').execute({
163
+ type: testType,
164
+ nameReadable: 'Can book appointment',
165
+ nameCamel: 'canBookAppointment',
166
+ namePascal: 'CanBookAppointment',
167
+ })
168
+
169
+ await this.waitForInput()
170
+ return { promise }
171
+ }
172
+
173
+ private static async waitAndSelectSubClass(selectedSubClass?: string) {
174
+ await this.waitForInput()
175
+ await this.ui.sendInput(selectedSubClass ?? '')
176
+ }
63
177
  }
@@ -27,17 +27,7 @@ const featuresWithRegisteredTests: {
27
27
  export default class SelectingAnAbstractTestClassTest extends AbstractTestTest {
28
28
  @test()
29
29
  protected static async asksForYouToSelectABaseClass() {
30
- await this.installTests()
31
- await this.copyTestFiles()
32
-
33
- const { choices, promise } =
34
- await this.executeCreateUntilAbstractClassSelection()
35
-
36
- for (const expected of expectedAbstractTests) {
37
- assert.doesInclude(choices, { label: expected })
38
- }
39
-
40
- this.selectOptionBasedOnLabel('AbstractBananaTestDifferentThanFileName')
30
+ const { promise } = await this.installCopyTestFilesSelectLocalAbstractTest()
41
31
 
42
32
  const results = await promise
43
33
 
@@ -46,12 +36,27 @@ export default class SelectingAnAbstractTestClassTest extends AbstractTestTest {
46
36
  results.files
47
37
  )
48
38
 
49
- await this.Service('build').build()
39
+ await this.buildAndAssertTestFailsAsExpected()
40
+ }
50
41
 
51
- await assert.doesThrowAsync(
52
- () => this.Service('command').execute('yarn test'),
53
- /false.*?does not equal.*?true/gis
54
- )
42
+ @test()
43
+ protected static async canSelectAbstractClassWhileSelectingSubDir() {
44
+ const testDir = this.resolvePath('src', '__tests__', 'behavioral', 'taco')
45
+ diskUtil.createDir(testDir)
46
+
47
+ await this.installAndCopyTestFiles()
48
+
49
+ const { promise } = await this.invokeCreateActionAndWaitForInput()
50
+
51
+ await this.ui.sendInput('taco')
52
+
53
+ await this.waitForInput()
54
+
55
+ this.selectOptionBasedOnLabel('AbstractBananaTestDifferentThanFileName')
56
+
57
+ await promise
58
+
59
+ await this.buildAndAssertTestFailsAsExpected()
55
60
  }
56
61
 
57
62
  @test()
@@ -169,6 +174,35 @@ export default class SelectingAnAbstractTestClassTest extends AbstractTestTest {
169
174
  }
170
175
  }
171
176
 
177
+ private static async buildAndAssertTestFailsAsExpected() {
178
+ await this.Service('build').build()
179
+
180
+ await assert.doesThrowAsync(
181
+ () => this.Service('command').execute('yarn test'),
182
+ /false.*?does not equal.*?true/gis
183
+ )
184
+ }
185
+
186
+ private static async installCopyTestFilesSelectLocalAbstractTest() {
187
+ await this.installAndCopyTestFiles()
188
+
189
+ const { choices, promise } =
190
+ await this.executeCreateUntilAbstractClassSelection()
191
+
192
+ for (const expected of expectedAbstractTests) {
193
+ assert.doesInclude(choices, { label: expected })
194
+ }
195
+
196
+ this.selectOptionBasedOnLabel('AbstractBananaTestDifferentThanFileName')
197
+
198
+ return { promise }
199
+ }
200
+
201
+ private static async installAndCopyTestFiles() {
202
+ await this.installTests()
203
+ await this.copyTestFiles()
204
+ }
205
+
172
206
  private static async copyTestFiles() {
173
207
  const source = this.resolveTestPath('abstract_tests')
174
208
  const destination = this.resolvePath('src')
@@ -177,13 +211,7 @@ export default class SelectingAnAbstractTestClassTest extends AbstractTestTest {
177
211
  }
178
212
 
179
213
  private static async executeCreateUntilAbstractClassSelection() {
180
- const promise = this.Action('test', 'create').execute({
181
- type: 'behavioral',
182
- nameReadable: 'Can book appointment',
183
- nameCamel: 'canBookAppointment',
184
- })
185
-
186
- await this.waitForInput()
214
+ const { promise } = await this.invokeCreateActionAndWaitForInput()
187
215
 
188
216
  const last = this.ui.getLastInvocation()
189
217
  const { choices } = last.options.options ?? {}
@@ -193,4 +221,15 @@ export default class SelectingAnAbstractTestClassTest extends AbstractTestTest {
193
221
  choices: SelectChoice[]
194
222
  }
195
223
  }
224
+
225
+ private static async invokeCreateActionAndWaitForInput() {
226
+ const promise = this.Action('test', 'create').execute({
227
+ type: 'behavioral',
228
+ nameReadable: 'Can book appointment',
229
+ nameCamel: 'canBookAppointment',
230
+ })
231
+
232
+ await this.waitForInput()
233
+ return { promise }
234
+ }
196
235
  }
@@ -19,7 +19,7 @@ export default class CreateAction extends AbstractAction<OptionsSchema> {
19
19
  const { testDestinationDir, namePascal, nameCamel, type } =
20
20
  normalizedOptions
21
21
 
22
- const resolvedDestination = diskUtil.resolvePath(
22
+ let resolvedDestination = diskUtil.resolvePath(
23
23
  this.cwd,
24
24
  testDestinationDir,
25
25
  type
@@ -28,7 +28,6 @@ export default class CreateAction extends AbstractAction<OptionsSchema> {
28
28
  this.ui.startLoading('Checking potential parent test classes')
29
29
 
30
30
  const testFeature = this.parent as TestFeature
31
- const candidates = await testFeature.buildParentClassCandidates()
32
31
 
33
32
  this.ui.stopLoading()
34
33
 
@@ -36,34 +35,22 @@ export default class CreateAction extends AbstractAction<OptionsSchema> {
36
35
  | undefined
37
36
  | { name: string; importPath: string; isDefaultExport: boolean }
38
37
 
39
- if (candidates.length > 0) {
40
- const idx = await this.ui.prompt({
41
- type: 'select',
42
- isRequired: true,
43
- label: 'Which abstract test class do you want to extend?',
44
- options: {
45
- choices: [
46
- { value: '', label: 'AbstractSpruceTest (default)' },
47
- ...candidates.map((candidate, idx) => ({
48
- value: `${idx}`,
49
- label: candidate.label,
50
- })),
51
- ],
52
- },
53
- })
54
-
55
- if (idx !== '' && candidates[+idx]) {
56
- const match = candidates[+idx]
38
+ const candidates = await testFeature.buildParentClassCandidates()
57
39
 
58
- if (match) {
59
- await this.optionallyInstallFeatureBasedOnSelection(match)
40
+ if (diskUtil.doesDirExist(resolvedDestination)) {
41
+ resolvedDestination = await this.promptForSubDir(
42
+ resolvedDestination,
43
+ type
44
+ )
45
+ }
60
46
 
61
- parentTestClass = this.buildParentClassFromCandidate(
62
- match,
63
- resolvedDestination
64
- )
65
- }
66
- }
47
+ if (candidates.length > 0) {
48
+ parentTestClass =
49
+ await this.promptForParentTestClassAndOptionallyInstallDependencies(
50
+ candidates,
51
+ parentTestClass,
52
+ resolvedDestination
53
+ )
67
54
  }
68
55
 
69
56
  this.ui.startLoading('Generating test file...')
@@ -83,6 +70,74 @@ export default class CreateAction extends AbstractAction<OptionsSchema> {
83
70
  hints: ["run `spruce test` in your skill when you're ready!"],
84
71
  }
85
72
  }
73
+ private async promptForSubDir(resolvedDestination: string, type: string) {
74
+ const subdirs = diskUtil
75
+ .readDir(resolvedDestination)
76
+ .filter((d) =>
77
+ diskUtil.isDir(diskUtil.resolvePath(resolvedDestination, d))
78
+ )
79
+
80
+ if (subdirs.length > 0) {
81
+ const match = await this.ui.prompt({
82
+ type: 'select',
83
+ label: 'Where should I write this test?',
84
+ isRequired: true,
85
+ options: {
86
+ choices: [
87
+ {
88
+ value: '.',
89
+ label: `${type}`,
90
+ },
91
+ ...subdirs.map((dir) => ({
92
+ value: `${dir}`,
93
+ label: `${type}/${dir}`,
94
+ })),
95
+ ],
96
+ },
97
+ })
98
+
99
+ resolvedDestination = diskUtil.resolvePath(resolvedDestination, match)
100
+ }
101
+ return resolvedDestination
102
+ }
103
+
104
+ private async promptForParentTestClassAndOptionallyInstallDependencies(
105
+ candidates: ParentClassCandidate[],
106
+ parentTestClass:
107
+ | { name: string; importPath: string; isDefaultExport: boolean }
108
+ | undefined,
109
+ resolvedDestination: string
110
+ ) {
111
+ const idx = await this.ui.prompt({
112
+ type: 'select',
113
+ isRequired: true,
114
+ label: 'Which abstract test class do you want to extend?',
115
+ options: {
116
+ choices: [
117
+ { value: '', label: 'AbstractSpruceTest (default)' },
118
+ ...candidates.map((candidate, idx) => ({
119
+ value: `${idx}`,
120
+ label: candidate.label,
121
+ })),
122
+ ],
123
+ },
124
+ })
125
+
126
+ if (idx !== '' && candidates[+idx]) {
127
+ const match = candidates[+idx]
128
+
129
+ if (match) {
130
+ await this.optionallyInstallFeatureBasedOnSelection(match)
131
+
132
+ parentTestClass = this.buildParentClassFromCandidate(
133
+ match,
134
+ resolvedDestination
135
+ )
136
+ }
137
+ }
138
+ return parentTestClass
139
+ }
140
+
86
141
  private async optionallyInstallFeatureBasedOnSelection(
87
142
  match: ParentClassCandidate
88
143
  ) {
@@ -1,7 +1,7 @@
1
1
  import { assert } from '@sprucelabs/test'
2
2
  import SpyInterface from '../../interfaces/SpyInterface'
3
3
 
4
- const uiAssertUtil = {
4
+ const uiAssert = {
5
5
  async assertRendersSelect(ui: SpyInterface) {
6
6
  await ui.waitForInput()
7
7
 
@@ -10,6 +10,8 @@ const uiAssertUtil = {
10
10
  last.options.options.choices,
11
11
  `I expected a select, I did not find one!`
12
12
  )
13
+
14
+ return last.options
13
15
  },
14
16
 
15
17
  assertSelectDidNotRenderChoice(
@@ -25,6 +27,15 @@ const uiAssertUtil = {
25
27
  })
26
28
  },
27
29
 
30
+ assertSelectRenderChoice(ui: SpyInterface, value: string, label: string) {
31
+ const last = ui.getLastInvocation()
32
+
33
+ assert.doesInclude(last.options.options.choices, {
34
+ value,
35
+ label,
36
+ })
37
+ },
38
+
28
39
  async assertRendersConfirmWriteFile(ui: SpyInterface) {
29
40
  await ui.waitForInput()
30
41
 
@@ -42,4 +53,4 @@ const uiAssertUtil = {
42
53
  },
43
54
  }
44
55
 
45
- export default uiAssertUtil
56
+ export default uiAssert