Skip to content

Commit e26d85e

Browse files
committed
feat: adapt tests to new error types
1 parent 4606806 commit e26d85e

File tree

3 files changed

+30
-22
lines changed

3 files changed

+30
-22
lines changed

example/src/tests/unit/common.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { Chance } from 'chance'
2-
import { enableSimpleNullHandling } from 'react-native-nitro-sqlite'
2+
import {
3+
enableSimpleNullHandling,
4+
NitroSQLiteError,
5+
} from 'react-native-nitro-sqlite'
36
import { resetTestDb } from '../db'
47
import chai from 'chai'
58

6-
export function isError(e: unknown): e is Error {
7-
return e instanceof Error
9+
export function isNitroSQLiteError(e: unknown): e is NitroSQLiteError {
10+
return e instanceof NitroSQLiteError
811
}
912

1013
export const expect = chai.expect

example/src/tests/unit/specs/execute.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { chance, expect, isError } from '../common'
1+
import { chance, expect, isNitroSQLiteError } from '../common'
22
import {
33
enableSimpleNullHandling,
44
NITRO_SQLITE_NULL,
@@ -94,7 +94,7 @@ export default function registerExecuteUnitTests() {
9494
[id, name, age, networth],
9595
)
9696
} catch (e: unknown) {
97-
if (isError(e)) {
97+
if (isNitroSQLiteError(e)) {
9898
expect(e.message).to.include(
9999
'cannot store TEXT value in REAL column User.age',
100100
)

example/src/tests/unit/specs/transaction.spec.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import { chance, expect, isError } from '../common'
1+
import { chance, expect, isNitroSQLiteError } from '../common'
22
import { describe, it } from '../../MochaRNAdapter'
33
import type { User } from '../../../model/User'
44
import { testDb, testDbQueue } from '../../db'
55

6+
const DUMMY_ERROR_NAME = 'Transaction Rejection Error'
7+
const DUMMY_ERROR_MESSAGE = 'Error from callback'
8+
69
export default function registerTransactionUnitTests() {
710
describe('transaction', () => {
811
it('Transaction, auto commit', async () => {
@@ -199,17 +202,18 @@ export default function registerTransactionUnitTests() {
199202

200203
it('Transaction, rejects on callback error', async () => {
201204
const promised = testDb.transaction(() => {
202-
throw new Error('Error from callback')
205+
throw new Error(DUMMY_ERROR_MESSAGE)
203206
})
204207

205208
// ASSERT: should return a promise that eventually rejects
206209
expect(promised).to.have.property('then').that.is.a('function')
207210
try {
208211
await promised
209-
expect.fail('Should not resolve')
212+
expect.fail(DUMMY_ERROR_NAME)
210213
} catch (e) {
211-
if (isError(e)) expect(e.message).to.equal('Error from callback')
212-
else expect.fail('Should have thrown a valid NitroSQLiteException')
214+
if (isNitroSQLiteError(e))
215+
expect(e.message).to.include(DUMMY_ERROR_MESSAGE)
216+
else expect.fail('Should have thrown a valid NitroSQLiteError')
213217
}
214218
})
215219

@@ -221,11 +225,11 @@ export default function registerTransactionUnitTests() {
221225
expect(promised).to.have.property('then').that.is.a('function')
222226
try {
223227
await promised
224-
expect.fail('Should not resolve')
228+
expect.fail(DUMMY_ERROR_NAME)
225229
} catch (e) {
226-
if (isError(e))
230+
if (isNitroSQLiteError(e))
227231
expect(e.message).to.include('no such table: tableThatDoesNotExist')
228-
else expect.fail('Should have thrown a valid NitroSQLiteException')
232+
else expect.fail('Should have thrown a valid NitroSQLiteError')
229233
}
230234
})
231235

@@ -289,15 +293,15 @@ export default function registerTransactionUnitTests() {
289293
)
290294
})
291295
} catch (e) {
292-
if (isError(e)) {
296+
if (isNitroSQLiteError(e)) {
293297
expect(e.message)
294298
.to.include('SqlExecutionError')
295299
.and.to.include('cannot store TEXT value in REAL column User.id')
296300

297301
const res = testDb.execute('SELECT * FROM User')
298302
expect(res.rows?._array).to.eql([])
299303
} else {
300-
expect.fail('Should have thrown a valid NitroSQLiteException')
304+
expect.fail('Should have thrown a valid NitroSQLiteError')
301305
}
302306
}
303307
})
@@ -401,17 +405,18 @@ export default function registerTransactionUnitTests() {
401405

402406
it('Async transaction, rejects on callback error', async () => {
403407
const promised = testDb.transaction(() => {
404-
throw new Error('Error from callback')
408+
throw new Error(DUMMY_ERROR_MESSAGE)
405409
})
406410

407411
// ASSERT: should return a promise that eventually rejects
408412
expect(promised).to.have.property('then').that.is.a('function')
409413
try {
410414
await promised
411-
expect.fail('Should not resolve')
415+
expect.fail(DUMMY_ERROR_NAME)
412416
} catch (e) {
413-
if (isError(e)) expect(e.message).to.equal('Error from callback')
414-
else expect.fail('Should have thrown a valid NitroSQLiteException')
417+
if (isNitroSQLiteError(e))
418+
expect(e.message).to.include(DUMMY_ERROR_MESSAGE)
419+
else expect.fail('Should have thrown a valid NitroSQLiteError')
415420
}
416421
})
417422

@@ -424,11 +429,11 @@ export default function registerTransactionUnitTests() {
424429
expect(promised).to.have.property('then').that.is.a('function')
425430
try {
426431
await promised
427-
expect.fail('Should not resolve')
432+
expect.fail(DUMMY_ERROR_NAME)
428433
} catch (e) {
429-
if (isError(e))
434+
if (isNitroSQLiteError(e))
430435
expect(e.message).to.include('no such table: tableThatDoesNotExist')
431-
else expect.fail('Should have thrown a valid NitroSQLiteException')
436+
else expect.fail('Should have thrown a valid NitroSQLiteError')
432437
}
433438
})
434439

0 commit comments

Comments
 (0)