velocious 1.0.96 → 1.0.97

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.
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "velocious": "bin/velocious.js"
4
4
  },
5
5
  "name": "velocious",
6
- "version": "1.0.96",
6
+ "version": "1.0.97",
7
7
  "main": "index.js",
8
8
  "scripts": {
9
9
  "lint": "eslint",
@@ -1,3 +1,5 @@
1
+ // @ts-check
2
+
1
3
  import BaseCommand from "../../../../cli/base-command.js"
2
4
  import TestFilesFinder from "../../../../testing/test-files-finder.js"
3
5
  import TestRunner from "../../../../testing/test-runner.js"
@@ -6,15 +8,27 @@ export default class VelociousCliCommandsTest extends BaseCommand {
6
8
  async execute() {
7
9
  this.getConfiguration().setEnvironment("test")
8
10
 
9
- const directory = process.env.VELOCIOUS_TEST_DIR || this.directory()
10
- const testFilesFinder = new TestFilesFinder({directory, processArgs: this.processArgs})
11
+ let directory
12
+ const directories = []
13
+
14
+ if (process.env.VELOCIOUS_TEST_DIR) {
15
+ directory = process.env.VELOCIOUS_TEST_DIR
16
+ directories.push(process.env.VELOCIOUS_TEST_DIR)
17
+ } else {
18
+ directory = this.directory()
19
+ directories.push(`${this.directory()}/__tests__`)
20
+ directories.push(`${this.directory()}/tests`)
21
+ directories.push(`${this.directory()}/spec`)
22
+ }
23
+
24
+ const testFilesFinder = new TestFilesFinder({directory, directories, processArgs: this.processArgs})
11
25
  const testFiles = await testFilesFinder.findTestFiles()
12
26
  const testRunner = new TestRunner({configuration: this.getConfiguration(), testFiles})
13
27
 
14
28
  await testRunner.prepare()
15
29
 
16
30
  if (testRunner.getTestsCount() === 0) {
17
- throw new Error("No tests has been found")
31
+ throw new Error(`${testRunner.getTestsCount()} tests was found in ${testFiles.length} file(s)`)
18
32
  }
19
33
 
20
34
  await testRunner.run()
@@ -1,21 +1,75 @@
1
+ // @ts-check
2
+
1
3
  import fs from "fs/promises"
2
4
 
5
+ import fileExists from "../utils/file-exists.js"
6
+ import restArgsError from "../utils/rest-args-error.js"
7
+
3
8
  // Incredibly complex class to find files in multiple simultanious running promises to do it as fast as possible.
4
9
  export default class TestFilesFinder {
5
10
  static IGNORED_NAMES = [".git", "node_modules"]
6
11
 
7
- constructor({directory, processArgs}) {
12
+ /**
13
+ * @param {object} args
14
+ * @param {string} args.directory
15
+ * @param {string[]} args.directories
16
+ * @param {string[]} args.processArgs
17
+ */
18
+ constructor({directory, directories, processArgs, ...restArgs}) {
19
+ restArgsError(restArgs)
20
+
8
21
  this.directory = directory
9
- this.foundFiles = []
22
+
23
+ if (directories) {
24
+ this.directories = directories
25
+ } else {
26
+ this.directories = [
27
+ `${this.directory}/__tests__`,
28
+ `${this.directory}/tests`,
29
+ `${this.directory}/spec`
30
+ ]
31
+ }
32
+
10
33
  this.findingCount = 0
11
- this.findingPromises = {}
12
34
  this.processArgs = processArgs
35
+
36
+ /** @type {string[]} */
37
+ this.foundFiles = []
38
+
39
+ /** @type {Record<number, Promise<void>>} */
40
+ this.findingPromises = {}
41
+
42
+ /** @type {string[]} */
13
43
  this.testArgs = this.processArgs.filter((processArg, index) => index != 0)
44
+
45
+ /** @type {string[]} */
46
+ this.directoryArgs = []
47
+
48
+ /** @type {string[]} */
49
+ this.fileArgs = []
50
+
51
+ for (const testArg of this.testArgs) {
52
+ if (testArg.endsWith("/")) {
53
+ this.directoryArgs.push(testArg)
54
+ } else {
55
+ this.fileArgs.push(testArg)
56
+ }
57
+ }
14
58
  }
15
59
 
60
+ /**
61
+ * @returns {Promise<string[]>}
62
+ */
16
63
  async findTestFiles() {
17
64
  await this.withFindingCount(async () => {
18
- await this.findTestFilesInDir(this.directory)
65
+ for (const directory of this.directories) {
66
+ console.log({directory})
67
+
68
+ if (await fileExists(directory)) {
69
+ console.log("Exists!")
70
+ await this.findTestFilesInDir(directory)
71
+ }
72
+ }
19
73
  })
20
74
 
21
75
  await this.waitForFindingPromises()
@@ -23,6 +77,9 @@ export default class TestFilesFinder {
23
77
  return this.foundFiles
24
78
  }
25
79
 
80
+ /**
81
+ * @returns {number}
82
+ */
26
83
  findingPromisesLength() { return Object.keys(this.findingPromises).length }
27
84
 
28
85
  async waitForFindingPromises() {
@@ -31,6 +88,9 @@ export default class TestFilesFinder {
31
88
  }
32
89
  }
33
90
 
91
+ /**
92
+ * @returns {Promise<void>}
93
+ */
34
94
  async waitForFindingPromisesIteration() {
35
95
  const unfinishedPromises = []
36
96
 
@@ -43,6 +103,9 @@ export default class TestFilesFinder {
43
103
  await Promise.all(unfinishedPromises)
44
104
  }
45
105
 
106
+ /**
107
+ * @param {function() : Promise<void>} callback
108
+ */
46
109
  withFindingCount(callback) {
47
110
  return new Promise((resolve) => {
48
111
  const findingPromise = callback()
@@ -54,11 +117,15 @@ export default class TestFilesFinder {
54
117
  findingPromise.finally(() => {
55
118
  delete this.findingPromises[findingCount]
56
119
 
57
- resolve()
120
+ resolve(undefined)
58
121
  })
59
122
  })
60
123
  }
61
124
 
125
+ /**
126
+ * @param {string} dir
127
+ * @returns {Promise<void>}
128
+ */
62
129
  async findTestFilesInDir(dir) {
63
130
  await this.withFindingCount(async () => {
64
131
  const files = await fs.readdir(dir)
@@ -89,16 +156,32 @@ export default class TestFilesFinder {
89
156
  * @returns {boolean}
90
157
  */
91
158
  isFileMatchingRequirements(file, localPath) {
92
- if (this.testArgs.length > 0) {
93
- for (const testArg of this.testArgs) {
94
- if (testArg == localPath) {
159
+ if (this.directoryArgs.length > 0) {
160
+ for (const directoryArg of this.directoryArgs) {
161
+ if (localPath.startsWith(directoryArg) && this.looksLikeTestFile(file)) {
95
162
  return true
96
163
  }
97
164
  }
98
- } else if (file.match(/-(spec|test)\.(m|)js$/)) {
165
+ }
166
+
167
+ if (this.fileArgs.length > 0) {
168
+ for (const fileArg of this.fileArgs) {
169
+ if (fileArg == localPath) {
170
+ return true
171
+ }
172
+ }
173
+ } else if (this.looksLikeTestFile(file)) {
99
174
  return true
100
175
  }
101
176
 
102
177
  return false
103
178
  }
179
+
180
+ /**
181
+ * @param {string} file
182
+ * @returns {boolean}
183
+ */
184
+ looksLikeTestFile(file) {
185
+ return Boolean(file.match(/-(spec|test)\.(m|)js$/))
186
+ }
104
187
  }
@@ -26,7 +26,7 @@ export default class TestRunner {
26
26
  getConfiguration() { return this._configuration }
27
27
 
28
28
  /**
29
- * @returns {Array<string>}
29
+ * @returns {string[]}
30
30
  */
31
31
  getTestFiles() { return this._testFiles }
32
32