1
+ import * as path from 'node:path' ;
2
+ import * as fs from 'node:fs/promises' ;
1
3
import { Worker } from 'node:worker_threads' ;
2
- import fs from 'node:fs/promises' ;
3
4
4
- import { vi , expect , test , onTestFinished } from 'vitest' ;
5
- import { AppSchema , createTempDir , databaseTest } from './utils' ;
5
+ import { vi , expect , test } from 'vitest' ;
6
+ import { AppSchema , databaseTest , tempDirectoryTest } from './utils' ;
6
7
import { PowerSyncDatabase } from '../lib' ;
7
8
import { WorkerOpener } from '../lib/db/options' ;
8
9
@@ -12,15 +13,14 @@ test('validates options', async () => {
12
13
schema : AppSchema ,
13
14
database : {
14
15
dbFilename : '/dev/null' ,
15
- readWorkerCount : 0 ,
16
+ readWorkerCount : 0
16
17
}
17
18
} ) ;
18
19
await database . init ( ) ;
19
20
} ) . rejects . toThrowError ( 'Needs at least one worker for reads' ) ;
20
21
} ) ;
21
22
22
- test ( 'can customize loading workers' , async ( ) => {
23
- const directory = await createTempDir ( ) ;
23
+ tempDirectoryTest ( 'can customize loading workers' , async ( { tmpdir } ) => {
24
24
const defaultWorker : WorkerOpener = ( ...args ) => new Worker ( ...args ) ;
25
25
26
26
const openFunction = vi . fn ( defaultWorker ) ; // Wrap in vi.fn to count invocations
@@ -29,7 +29,7 @@ test('can customize loading workers', async () => {
29
29
schema : AppSchema ,
30
30
database : {
31
31
dbFilename : 'test.db' ,
32
- dbLocation : directory ,
32
+ dbLocation : tmpdir ,
33
33
openWorker : openFunction ,
34
34
readWorkerCount : 2
35
35
}
@@ -38,8 +38,6 @@ test('can customize loading workers', async () => {
38
38
await database . get ( 'SELECT 1;' ) ; // Make sure the database is ready and works
39
39
expect ( openFunction ) . toHaveBeenCalledTimes ( 3 ) ; // One writer, two readers
40
40
await database . close ( ) ;
41
-
42
- onTestFinished ( async ( ) => fs . rm ( directory , { recursive : true } ) ) ;
43
41
} ) ;
44
42
45
43
databaseTest ( 'links powersync' , async ( { database } ) => {
@@ -95,6 +93,22 @@ databaseTest('can watch tables', async ({ database }) => {
95
93
await expect . poll ( ( ) => fn ) . toHaveBeenCalledTimes ( 2 ) ;
96
94
} ) ;
97
95
96
+ tempDirectoryTest ( 'throws error if target directory does not exist' , async ( { tmpdir } ) => {
97
+ const directory = path . join ( tmpdir , 'some' , 'nested' , 'location' , 'that' , 'does' , 'not' , 'exist' ) ;
98
+
99
+ expect ( async ( ) => {
100
+ const database = new PowerSyncDatabase ( {
101
+ schema : AppSchema ,
102
+ database : {
103
+ dbFilename : 'test.db' ,
104
+ dbLocation : directory ,
105
+ readWorkerCount : 2
106
+ }
107
+ } ) ;
108
+ await database . waitForReady ( ) ;
109
+ } ) . rejects . toThrowError ( / T h e d b L o c a t i o n d i r e c t o r y a t " .+ " d o e s n o t e x i s t / ) ;
110
+ } ) ;
111
+
98
112
databaseTest . skip ( 'can watch queries' , async ( { database } ) => {
99
113
const query = await database . watch ( 'SELECT * FROM todos;' , [ ] ) [ Symbol . asyncIterator ] ( ) ;
100
114
expect ( ( await query . next ( ) ) . value . rows ) . toHaveLength ( 0 ) ;
0 commit comments