expo-sqlite-mock 1.0.0 → 2.0.1

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/README.md CHANGED
@@ -5,7 +5,23 @@
5
5
 
6
6
  Use expo-sqlite with jest.
7
7
 
8
+ ## Notice
9
+
10
+ - **~2.0.0** is for expo-sqlite ~52.
11
+ - **~1.0.0** is for expo-sqlite ~51.
12
+
8
13
  ## Usage
9
14
 
10
15
  1. `npm install -D expo-sqlite-mock` or `bun add -D expo-sqlite-mock`
11
- 2. Add `"setupFilesAfterEnv": ["expo-sqlite-mock/src/setup.ts"]` to your jest config (It's in `package.json` for default expo project).
16
+ 2. Add `"setupFilesAfterEnv": ["expo-sqlite-mock/src/setup.ts"]` and `"testTimeout": 10000` to your jest config (It's in `package.json` for default expo project).
17
+
18
+ ## Changelog
19
+
20
+ - **2.0.1**
21
+ - Fix setup.
22
+
23
+ - **2.0.0**
24
+ - Update for expo-sqlite ~52.
25
+
26
+ - **1.0.0**
27
+ - Initial version.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expo-sqlite-mock",
3
- "version": "1.0.0",
3
+ "version": "2.0.1",
4
4
  "license": "MIT",
5
5
  "homepage": "https://github.com/zfben/expo-sqlite-mock",
6
6
  "repository": {
@@ -11,7 +11,7 @@
11
11
  "url": "https://github.com/zfben/expo-sqlite-mock/issues"
12
12
  },
13
13
  "scripts": {
14
- "test": "jest"
14
+ "test": "jest --coverage --forceExit --detectOpenHandles --testTimeout=10000"
15
15
  },
16
16
  "jest": {
17
17
  "preset": "jest-expo",
@@ -24,7 +24,8 @@
24
24
  "peerDependencies": {
25
25
  "better-sqlite3": "*",
26
26
  "jest": "*",
27
- "expo-sqlite": "*"
27
+ "expo-sqlite": "*",
28
+ "expo": "~52"
28
29
  },
29
30
  "devDependencies": {
30
31
  "typescript": "*",
@@ -34,9 +35,9 @@
34
35
  "@types/better-sqlite3": "*",
35
36
  "@types/react": "*",
36
37
  "react": "*",
37
- "react-test-renderer": "18.2.0",
38
+ "react-test-renderer": "18.3.1",
38
39
  "@faasjs/jest": "*",
39
40
  "@testing-library/react-native": "*",
40
- "jest-expo": "*"
41
+ "jest-expo": "~52"
41
42
  }
42
43
  }
@@ -11,8 +11,12 @@ import type {
11
11
  SQLiteRunResult,
12
12
  } from 'expo-sqlite/src/NativeStatement'
13
13
 
14
+ const dbs: NativeDatabase[] = []
15
+
14
16
  export const mockedExpoSqliteNext = {
15
- deleteDatabaseAsync: jest.fn(),
17
+ deleteDatabaseAsync: async () => {
18
+ for (const db of dbs) await db.closeAsync()
19
+ },
16
20
 
17
21
  NativeDatabase: jest
18
22
  .fn()
@@ -22,6 +26,11 @@ export const mockedExpoSqliteNext = {
22
26
  ),
23
27
 
24
28
  NativeStatement: jest.fn().mockImplementation(() => new NativeStatement()),
29
+
30
+ defaultDatabaseDirectory: '.',
31
+
32
+ ensureDatabasePathExistsAsync: jest.fn().mockImplementation(async (databasePath: string) => {}),
33
+ ensureDatabasePathExistsSync: jest.fn().mockImplementation((databasePath: string) => {}),
25
34
  }
26
35
 
27
36
  //#region async sqlite3
@@ -38,42 +47,32 @@ class NativeDatabase {
38
47
  } else {
39
48
  this.sqlite3Db = new sqlite3(':memory:')
40
49
  }
50
+ dbs.push(this)
41
51
  }
42
52
 
43
53
  //#region Asynchronous API
44
54
 
45
55
  initAsync = jest.fn().mockResolvedValue(null)
46
- isInTransactionAsync = jest.fn().mockImplementation(async () => {
47
- return this.sqlite3Db.inTransaction
48
- })
49
- closeAsync = jest.fn().mockImplementation(async () => {
50
- return this.sqlite3Db.close()
51
- })
52
- execAsync = jest.fn().mockImplementation(async (source: string) => {
53
- return this.sqlite3Db.exec(source)
54
- })
55
- serializeAsync = jest.fn().mockImplementation(async (databaseName: string) => {
56
- return this.sqlite3Db.serialize({ attached: databaseName })
57
- })
58
- prepareAsync = jest.fn().mockImplementation(async (nativeStatement: NativeStatement, source: string) => {
56
+ isInTransactionAsync = async () => this.sqlite3Db.inTransaction
57
+ closeAsync = async () => this.sqlite3Db.close()
58
+ execAsync = async (source: string) => this.sqlite3Db.exec(source)
59
+ serializeAsync = async (databaseName: string) => this.sqlite3Db.serialize({ attached: databaseName })
60
+ prepareAsync = async (nativeStatement: NativeStatement, source: string) => {
59
61
  nativeStatement.sqlite3Stmt = this.sqlite3Db.prepare(source)
60
- })
62
+ }
61
63
 
62
64
  //#endregion
63
65
 
64
66
  //#region Synchronous API
65
67
 
66
68
  initSync = jest.fn()
67
- isInTransactionSync = jest.fn().mockImplementation(() => this.sqlite3Db.inTransaction)
68
- closeSync = jest.fn().mockImplementation(() => this.sqlite3Db.close())
69
- execSync = jest.fn().mockImplementation((source: string) => this.sqlite3Db.exec(source))
70
- serializeSync = jest.fn().mockImplementation((databaseName: string) => {
71
- return this.sqlite3Db.serialize({ attached: databaseName })
72
- })
73
- prepareSync = jest.fn().mockImplementation((nativeStatement: NativeStatement, source: string) => {
69
+ isInTransactionSync = () => this.sqlite3Db.inTransaction
70
+ closeSync = () => this.sqlite3Db.close()
71
+ execSync = (source: string) => this.sqlite3Db.exec(source)
72
+ serializeSync = (databaseName: string) => this.sqlite3Db.serialize({ attached: databaseName })
73
+ prepareSync = (nativeStatement: NativeStatement, source: string) => {
74
74
  nativeStatement.sqlite3Stmt = this.sqlite3Db.prepare(source)
75
- })
76
-
75
+ }
77
76
  //#endregion
78
77
  }
79
78
 
@@ -108,17 +107,17 @@ class NativeStatement {
108
107
  const columnValues = result.done === false ? Object.values(result.value as Record<string, any>) : null
109
108
  return Promise.resolve(columnValues)
110
109
  })
111
- public getAllAsync = jest.fn().mockImplementation((database: NativeDatabase) => Promise.resolve(this._allValues()))
112
- public getColumnNamesAsync = jest.fn().mockImplementation(async (database: NativeDatabase) => {
110
+ public getAllAsync = (database: NativeDatabase) => Promise.resolve(this._allValues())
111
+ public getColumnNamesAsync = async (database: NativeDatabase) => {
113
112
  assert(this.sqlite3Stmt)
114
113
  return this.sqlite3Stmt.columns().map(column => column.name)
115
- })
116
- public resetAsync = jest.fn().mockImplementation(async (database: NativeDatabase) => {
114
+ }
115
+ public resetAsync = async (database: NativeDatabase) => {
117
116
  this._reset()
118
- })
119
- public finalizeAsync = jest.fn().mockImplementation(async (database: NativeDatabase) => {
117
+ }
118
+ public finalizeAsync = async (database: NativeDatabase) => {
120
119
  this._finalize()
121
- })
120
+ }
122
121
 
123
122
  //#endregion
124
123
 
@@ -135,7 +134,7 @@ class NativeStatement {
135
134
  ): SQLiteRunResult & { firstRowValues: SQLiteColumnValues } =>
136
135
  this._run(normalizeSQLite3Args(bindParams, bindBlobParams, shouldPassAsArray))
137
136
  )
138
- public stepSync = jest.fn().mockImplementation((database: NativeDatabase): any => {
137
+ public stepSync = (database: NativeDatabase): any => {
139
138
  assert(this.sqlite3Stmt)
140
139
  if (this.iterator == null) {
141
140
  this.iterator = this.sqlite3Stmt.iterate()
@@ -146,18 +145,18 @@ class NativeStatement {
146
145
  const result = this.iterator.next()
147
146
  const columnValues = result.done === false ? Object.values(result.value as Record<string, any>) : null
148
147
  return columnValues
149
- })
150
- public getAllSync = jest.fn().mockImplementation((database: NativeDatabase) => this._allValues())
151
- public getColumnNamesSync = jest.fn().mockImplementation((database: NativeDatabase) => {
148
+ }
149
+ public getAllSync = (database: NativeDatabase) => this._allValues()
150
+ public getColumnNamesSync = (database: NativeDatabase) => {
152
151
  assert(this.sqlite3Stmt)
153
152
  return this.sqlite3Stmt.columns().map(column => column.name)
154
- })
155
- public resetSync = jest.fn().mockImplementation((database: NativeDatabase) => {
153
+ }
154
+ public resetSync = (database: NativeDatabase) => {
156
155
  this._reset()
157
- })
158
- public finalizeSync = jest.fn().mockImplementation((database: NativeDatabase) => {
156
+ }
157
+ public finalizeSync = (database: NativeDatabase) => {
159
158
  this._finalize()
160
- })
159
+ }
161
160
 
162
161
  //#endregion
163
162
 
package/src/setup.ts CHANGED
@@ -1,3 +1,8 @@
1
1
  import { mockedExpoSqliteNext } from './ExpoSQLiteNext'
2
2
 
3
- jest.mock(`${__dirname}/../../expo-sqlite/build/ExpoSQLiteNext`, () => mockedExpoSqliteNext)
3
+ jest.mock(`${__dirname}/../../expo-sqlite/build/ExpoSQLite`, () => mockedExpoSqliteNext)
4
+ jest.mock(`${__dirname}/../../expo-sqlite/build/pathUtils`, () => ({
5
+ createDatabasePath: jest.fn().mockImplementation((databaseName: string) => {
6
+ return databaseName
7
+ }),
8
+ }))