Skip to content
This repository was archived by the owner on May 25, 2025. It is now read-only.

Commit d3cbbf5

Browse files
fix: fixing vitest import for dbTest/daoTest
1 parent 882c581 commit d3cbbf5

File tree

4 files changed

+53
-28
lines changed

4 files changed

+53
-28
lines changed

src/testing/daoTest.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { Readable } from 'node:stream'
2-
import { _deepCopy, _omit, _pick, _sortBy, localTime } from '@naturalcycles/js-lib'
2+
import { _deepCopy, _filterObject, _omit, _pick, _sortBy, localTime } from '@naturalcycles/js-lib'
33
import { _pipeline } from '@naturalcycles/nodejs-lib'
4-
import { expect, test } from 'vitest'
54
import { CommonDaoLogLevel, DBQuery } from '..'
65
import { CommonDB } from '../common.db'
76
import { CommonDao } from '../commondao/common.dao'
87
import { TestItemBM } from '.'
9-
import { CommonDBImplementationQuirks, expectMatch } from './dbTest'
8+
import { CommonDBImplementationQuirks } from './dbTest'
109
import {
1110
createTestItemBM,
1211
createTestItemsBM,
@@ -15,7 +14,13 @@ import {
1514
testItemBMSchema,
1615
} from './test.model'
1716

18-
export function runCommonDaoTest(db: CommonDB, quirks: CommonDBImplementationQuirks = {}): void {
17+
export async function runCommonDaoTest(
18+
db: CommonDB,
19+
quirks: CommonDBImplementationQuirks = {},
20+
): Promise<void> {
21+
// this is because vitest cannot be "required" from cjs
22+
const { test, expect } = await import('vitest')
23+
1924
const { support } = db
2025
const dao = new CommonDao({
2126
table: TEST_TABLE,
@@ -359,4 +364,21 @@ export function runCommonDaoTest(db: CommonDB, quirks: CommonDBImplementationQui
359364
})
360365
}
361366
}
367+
368+
function expectMatch(expected: any[], actual: any[], quirks: CommonDBImplementationQuirks): void {
369+
// const expectedSorted = sortObjectDeep(expected)
370+
// const actualSorted = sortObjectDeep(actual)
371+
372+
if (quirks.allowBooleansAsUndefined) {
373+
expected = expected.map(r => {
374+
return typeof r !== 'object' ? r : _filterObject(r, (_k, v) => v !== false)
375+
})
376+
}
377+
378+
if (quirks.allowExtraPropertiesInResponse) {
379+
expect(actual).toMatchObject(expected)
380+
} else {
381+
expect(actual).toEqual(expected)
382+
}
383+
}
362384
}

src/testing/dbTest.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { _deepFreeze, _filterObject, _pick, _sortBy, localTime, pMap } from '@naturalcycles/js-lib'
2-
import { expect, test } from 'vitest'
32
import { CommonDB, CommonDBType } from '../common.db'
43
import { DBQuery } from '../query/dbQuery'
54
import {
@@ -25,7 +24,13 @@ export interface CommonDBImplementationQuirks {
2524
allowBooleansAsUndefined?: boolean
2625
}
2726

28-
export function runCommonDBTest(db: CommonDB, quirks: CommonDBImplementationQuirks = {}): void {
27+
export async function runCommonDBTest(
28+
db: CommonDB,
29+
quirks: CommonDBImplementationQuirks = {},
30+
): Promise<void> {
31+
// this is because vitest cannot be "required" from cjs
32+
const { test, expect } = await import('vitest')
33+
2934
const { support } = db
3035
const items = createTestItemsDBM(3)
3136
_deepFreeze(items)
@@ -380,25 +385,21 @@ export function runCommonDBTest(db: CommonDB, quirks: CommonDBImplementationQuir
380385
await db.deleteByQuery(queryAll())
381386
})
382387
}
383-
}
384388

385-
export function expectMatch(
386-
expected: any[],
387-
actual: any[],
388-
quirks: CommonDBImplementationQuirks,
389-
): void {
390-
// const expectedSorted = sortObjectDeep(expected)
391-
// const actualSorted = sortObjectDeep(actual)
392-
393-
if (quirks.allowBooleansAsUndefined) {
394-
expected = expected.map(r => {
395-
return typeof r !== 'object' ? r : _filterObject(r, (_k, v) => v !== false)
396-
})
397-
}
389+
function expectMatch(expected: any[], actual: any[], quirks: CommonDBImplementationQuirks): void {
390+
// const expectedSorted = sortObjectDeep(expected)
391+
// const actualSorted = sortObjectDeep(actual)
398392

399-
if (quirks.allowExtraPropertiesInResponse) {
400-
expect(actual).toMatchObject(expected)
401-
} else {
402-
expect(actual).toEqual(expected)
393+
if (quirks.allowBooleansAsUndefined) {
394+
expected = expected.map(r => {
395+
return typeof r !== 'object' ? r : _filterObject(r, (_k, v) => v !== false)
396+
})
397+
}
398+
399+
if (quirks.allowExtraPropertiesInResponse) {
400+
expect(actual).toMatchObject(expected)
401+
} else {
402+
expect(actual).toEqual(expected)
403+
}
403404
}
404405
}

src/testing/keyValueDBTest.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { _range, _sortBy, KeyValueTuple } from '@naturalcycles/js-lib'
2-
import { afterAll, beforeAll, expect, test } from 'vitest'
32
import { CommonKeyValueDB } from '../kv/commonKeyValueDB'
43
import { TEST_TABLE } from './test.model'
54

@@ -10,7 +9,9 @@ const testEntries: KeyValueTuple<string, Buffer>[] = testIds.map(id => [
109
Buffer.from(`${id}value`),
1110
])
1211

13-
export function runCommonKeyValueDBTest(db: CommonKeyValueDB): void {
12+
export async function runCommonKeyValueDBTest(db: CommonKeyValueDB): Promise<void> {
13+
const { afterAll, beforeAll, expect, test } = await import('vitest')
14+
1415
beforeAll(async () => {
1516
// Tests in this suite are not isolated,
1617
// and failing tests can leave the DB in an unexpected state for other tests,

src/testing/keyValueDaoTest.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { _sortBy } from '@naturalcycles/js-lib'
2-
import { afterAll, beforeAll, expect, test } from 'vitest'
32
import { CommonKeyValueDao } from '../kv/commonKeyValueDao'
43
import { CommonKeyValueDB, KeyValueDBTuple } from '../kv/commonKeyValueDB'
54
import { createTestItemsBM, TEST_TABLE } from './test.model'
@@ -8,7 +7,9 @@ const testItems = createTestItemsBM(4)
87
const testIds = testItems.map(e => e.id)
98
const testEntries: KeyValueDBTuple[] = testItems.map(e => [e.id, Buffer.from(`${e.id}value`)])
109

11-
export function runCommonKeyValueDaoTest(db: CommonKeyValueDB): void {
10+
export async function runCommonKeyValueDaoTest(db: CommonKeyValueDB): Promise<void> {
11+
const { afterAll, beforeAll, expect, test } = await import('vitest')
12+
1213
const dao = new CommonKeyValueDao({
1314
db,
1415
table: TEST_TABLE,

0 commit comments

Comments
 (0)