@testsmith/testblocks 0.8.1 → 0.8.2
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/dist/cli/index.js +77 -2
- package/package.json +1 -1
package/dist/cli/index.js
CHANGED
|
@@ -97,6 +97,75 @@ function findPluginsDir(startDir) {
|
|
|
97
97
|
}
|
|
98
98
|
return null;
|
|
99
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* Load folder hooks from a test file's directory up to the root globals directory
|
|
102
|
+
* Returns hooks in order from outermost folder to innermost
|
|
103
|
+
*/
|
|
104
|
+
function loadFolderHooks(testFilePath, globalsDir) {
|
|
105
|
+
let currentDir = path.dirname(path.resolve(testFilePath));
|
|
106
|
+
const stopDir = globalsDir ? path.resolve(globalsDir) : null;
|
|
107
|
+
// Collect hooks from innermost to outermost
|
|
108
|
+
const collectedHooks = [];
|
|
109
|
+
while (currentDir) {
|
|
110
|
+
const hooksPath = path.join(currentDir, '_hooks.testblocks.json');
|
|
111
|
+
if (fs.existsSync(hooksPath)) {
|
|
112
|
+
try {
|
|
113
|
+
const content = fs.readFileSync(hooksPath, 'utf-8');
|
|
114
|
+
const hooksFile = JSON.parse(content);
|
|
115
|
+
collectedHooks.push(hooksFile);
|
|
116
|
+
}
|
|
117
|
+
catch (e) {
|
|
118
|
+
console.warn(`Warning: Could not load hooks from ${hooksPath}: ${e.message}`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
// Stop at globals directory or root
|
|
122
|
+
if (stopDir && currentDir === stopDir)
|
|
123
|
+
break;
|
|
124
|
+
const parentDir = path.dirname(currentDir);
|
|
125
|
+
if (parentDir === currentDir)
|
|
126
|
+
break; // Reached root
|
|
127
|
+
currentDir = parentDir;
|
|
128
|
+
}
|
|
129
|
+
// Reverse to get outermost-to-innermost order
|
|
130
|
+
return collectedHooks.reverse();
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Merge folder hooks into a test file
|
|
134
|
+
* - beforeAll/beforeEach: parent hooks run first
|
|
135
|
+
* - afterAll/afterEach: child hooks run first (reverse order)
|
|
136
|
+
*/
|
|
137
|
+
function mergeFolderHooksIntoTestFile(testFile, folderHooks) {
|
|
138
|
+
if (!folderHooks || folderHooks.length === 0) {
|
|
139
|
+
return testFile;
|
|
140
|
+
}
|
|
141
|
+
const beforeAllSteps = [];
|
|
142
|
+
const afterAllSteps = [];
|
|
143
|
+
const beforeEachSteps = [];
|
|
144
|
+
const afterEachSteps = [];
|
|
145
|
+
// Parent to child order for beforeAll/beforeEach
|
|
146
|
+
for (const hooks of folderHooks) {
|
|
147
|
+
if (hooks.beforeAll)
|
|
148
|
+
beforeAllSteps.push(...hooks.beforeAll);
|
|
149
|
+
if (hooks.beforeEach)
|
|
150
|
+
beforeEachSteps.push(...hooks.beforeEach);
|
|
151
|
+
}
|
|
152
|
+
// Child to parent order for afterAll/afterEach
|
|
153
|
+
for (let i = folderHooks.length - 1; i >= 0; i--) {
|
|
154
|
+
const hooks = folderHooks[i];
|
|
155
|
+
if (hooks.afterAll)
|
|
156
|
+
afterAllSteps.unshift(...hooks.afterAll);
|
|
157
|
+
if (hooks.afterEach)
|
|
158
|
+
afterEachSteps.unshift(...hooks.afterEach);
|
|
159
|
+
}
|
|
160
|
+
// Merge with test file hooks
|
|
161
|
+
return {
|
|
162
|
+
...testFile,
|
|
163
|
+
beforeAll: [...beforeAllSteps, ...(testFile.beforeAll || [])],
|
|
164
|
+
afterAll: [...(testFile.afterAll || []), ...afterAllSteps],
|
|
165
|
+
beforeEach: [...beforeEachSteps, ...(testFile.beforeEach || [])],
|
|
166
|
+
afterEach: [...(testFile.afterEach || []), ...afterEachSteps],
|
|
167
|
+
};
|
|
168
|
+
}
|
|
100
169
|
const program = new commander_1.Command();
|
|
101
170
|
program
|
|
102
171
|
.name('testblocks')
|
|
@@ -219,12 +288,18 @@ program
|
|
|
219
288
|
}
|
|
220
289
|
console.log(`Running: ${basename}`);
|
|
221
290
|
const content = fs.readFileSync(file, 'utf-8');
|
|
222
|
-
|
|
291
|
+
let testFile = JSON.parse(content);
|
|
223
292
|
// Skip files that have no tests array (e.g., hooks-only files)
|
|
224
293
|
if (!testFile.tests || !Array.isArray(testFile.tests)) {
|
|
225
294
|
console.log(' (no tests in file)\n');
|
|
226
295
|
continue;
|
|
227
296
|
}
|
|
297
|
+
// Load and merge folder hooks
|
|
298
|
+
const globalsDir = fs.existsSync(globalsPath) ? path.dirname(globalsPath) : null;
|
|
299
|
+
const folderHooks = loadFolderHooks(file, globalsDir);
|
|
300
|
+
if (folderHooks.length > 0) {
|
|
301
|
+
testFile = mergeFolderHooksIntoTestFile(testFile, folderHooks);
|
|
302
|
+
}
|
|
228
303
|
// Apply filter if specified
|
|
229
304
|
if (options.filter) {
|
|
230
305
|
const filterRegex = new RegExp(options.filter, 'i');
|
|
@@ -359,7 +434,7 @@ program
|
|
|
359
434
|
'test:junit': 'testblocks run tests/**/*.testblocks.json -r junit -o reports',
|
|
360
435
|
},
|
|
361
436
|
devDependencies: {
|
|
362
|
-
'@testsmith/testblocks': '^0.8.
|
|
437
|
+
'@testsmith/testblocks': '^0.8.2',
|
|
363
438
|
},
|
|
364
439
|
};
|
|
365
440
|
fs.writeFileSync(packagePath, JSON.stringify(packageJson, null, 2));
|