@@ -279,58 +279,88 @@ const filesystemTests = [
279
279
] ;
280
280
281
281
function updateTestFile ( filePath : string ) : void {
282
- let content = fs . readFileSync ( filePath , "utf-8" ) ;
282
+ console . log ( `Processing file: ${ filePath } ` ) ;
283
283
284
- const beforeAllAddition = `
285
- beforeAll(async () => {
286
- this.testPath = await ${ generateUniqueTestPath ( ) } ;
287
- await fs.promises.mkdir(this.testPath, { recursive: true });
288
- });
289
- ` ;
284
+ let content = fs . readFileSync ( filePath , "utf-8" ) ;
285
+ console . log ( "Initial content:\n" , content ) ;
290
286
291
- const afterAllAddition = `
292
- afterAll(async () => {
293
- if (this.testPath && await fs.promises.access(this.testPath).then(() => true).catch(() => false)) {
294
- await fs.promises.rm(this.testPath, { recursive: true, force: true });
295
- }
296
- });
297
- ` ;
287
+ // Add BaseTest import if not present
288
+ if ( ! content . includes ( "import { BaseTest }" ) ) {
289
+ content = `import { BaseTest } from "../../helpers/testHelper/baseTest";\n${ content } ` ;
290
+ }
298
291
299
- // Add imports if needed
300
- if ( ! content . includes ( "import * as os " ) ) {
301
- content = `import * as os from 'os ';\n${ content } ` ;
292
+ // Add Vitest imports if not present
293
+ if ( ! content . includes ( "import { describe, beforeAll, afterAll } " ) ) {
294
+ content = `import { test } from 'vitest ';\n${ content } ` ;
302
295
}
303
296
304
- content = content
305
- . replace ( / f s \. p r o m i s e s \. ( r e a d | w r i t e | m k d i r | r m ) S y n c / g, "await fs.promises.$1" )
306
- . replace ( / d e s c r i b e \( [ ^ { ] * { / , `$&\n let testPath: string;\n` )
307
- . replace ( / b e f o r e A l l \( [ ^ { ] * { / , ( match ) => {
308
- if ( match . includes ( "testPath" ) ) return match ;
309
- return `${ beforeAllAddition } ${ match } ` ;
310
- } )
311
- . replace ( / a f t e r A l l \( [ ^ { ] * { / , ( match ) => {
312
- if ( match . includes ( "testPath" ) ) return match ;
313
- return `${ afterAllAddition } ${ match } ` ;
314
- } ) ;
297
+ // // Add vi.setTimeout if not present
298
+ // if (!content.includes("vi.setTimeout")) {
299
+ // content = `vi.setTimeout(15000);\n${content}`;
300
+ // }
301
+
302
+ // Add test instance and data declarations
303
+ const testInstanceDeclaration = `
304
+ let testInstance: BaseTest;
305
+ let testData: {
306
+ testUser: { name: string; email: string };
307
+ testOrg: { name: string };
308
+ };` ;
315
309
316
- content = content
317
- . replace ( / d e s c r i b e \. c o n c u r r e n t / g, "describe" )
318
- . replace ( / i t \. c o n c u r r e n t / g, "it" ) ;
310
+ if ( ! content . includes ( "let testInstance: BaseTest" ) ) {
311
+ content = content . replace (
312
+ / d e s c r i b e \( [ ^ { ] * { / ,
313
+ `$&\n${ testInstanceDeclaration } ` ,
314
+ ) ;
315
+ }
316
+
317
+ // Add BaseTest beforeAll hook if not present
318
+ const baseTestBeforeAll = `
319
+ beforeAll(async () => {
320
+ testInstance = new BaseTest();
321
+ try {
322
+ testData = await testInstance.beforeEach();
323
+ } catch (error) {
324
+ console.error('Error in beforeAll:', error);
325
+ throw error;
326
+ }
327
+ }, { timeout: 30000 });` ;
319
328
320
- // Add the beforeAll and afterAll hooks if not already present
321
329
if ( ! content . includes ( "beforeAll(async () =>" ) ) {
322
- content = content . replace ( / d e s c r i b e \( [ ^ { ] * { / , `$&${ beforeAllAddition } ` ) ;
330
+ content = content . replace ( / d e s c r i b e \( [ ^ { ] * { / , `$&\n ${ baseTestBeforeAll } ` ) ;
323
331
}
332
+
333
+ // Add BaseTest afterAll hook if not present
334
+ const baseTestAfterAll = `
335
+ afterAll(async () => {
336
+ try {
337
+ await testInstance.afterEach();
338
+ } catch (error) {
339
+ console.error('Error in afterAll:', error);
340
+ throw error;
341
+ }
342
+ }, { timeout: 30000 });` ;
343
+
324
344
if ( ! content . includes ( "afterAll(async () =>" ) ) {
325
- content = content . replace ( / d e s c r i b e \( [ ^ { ] * { / , `$&${ afterAllAddition } ` ) ;
345
+ content = content . replace ( / d e s c r i b e \( [ ^ { ] * { / , `$&\n ${ baseTestAfterAll } ` ) ;
326
346
}
327
347
328
- fs . writeFileSync ( filePath , content , "utf-8" ) ;
329
- console . log ( `Updated: ${ filePath } ` ) ;
330
- }
348
+ // Replace `describe.concurrent` with `describe`
349
+ content = content . replace ( / d e s c r i b e \. c o n c u r r e n t / g, "describe" ) ;
331
350
332
- function generateUniqueTestPath ( ) : string {
333
- return `os.tmpdir() + '/test-path-' + Date.now()` ;
351
+ // Replace `it.concurrent` with `test`
352
+ content = content . replace ( / i t \. c o n c u r r e n t / g, "test" ) ;
353
+
354
+ // Replace `it` with `test` and add correct syntax
355
+ content = content . replace ( / \b i t \( / g, "test(" ) ;
356
+ content = content . replace (
357
+ / t e s t \( [ ' " ] ( .* ?) [ ' " ] \s * , \s * a s y n c \s * \( [ ^ ) ] * \) \s * = > \s * { / g,
358
+ 'test("$1", async () => {' ,
359
+ ) ;
360
+
361
+ // Write updated content back to the file
362
+ fs . writeFileSync ( filePath , content , "utf-8" ) ;
363
+ console . log ( `Updated file: ${ filePath } ` ) ;
334
364
}
335
365
336
366
try {
0 commit comments