Skip to content

Commit 302444c

Browse files
authored
feat: assert beforeAll all (#7)
* feat: assert beforeAll all * chore: rm console.log
1 parent 11793be commit 302444c

File tree

5 files changed

+93
-134
lines changed

5 files changed

+93
-134
lines changed

src/basic.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type TestConfigBasic = Record<AdapterTestNameBasic, () => void | Promise<void>>
2626
export default (options: BasicTestOptions) => {
2727
const { test, app, serviceName, idProp } = options
2828

29-
describe('Basic Functionality', () => {
29+
describe('Basic', () => {
3030
let service: any
3131

3232
beforeEach(() => {

src/index.ts

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { AdapterTestNameMethods } from './methods.js'
55
import methodTests from './methods.js'
66
import type { AdapterTestNameSyntax } from './syntax.js'
77
import syntaxTests from './syntax.js'
8-
import { describe, it, afterAll } from 'vitest'
8+
import { describe, it } from 'vitest'
99

1010
export type TestSuiteOptions = {
1111
app: Application
@@ -32,9 +32,6 @@ export const defineTestSuite = (defineOptions?: DefineTestSuiteOptions) => {
3232
return (options: TestSuiteOptions) => {
3333
const { app, serviceName, idProp = 'id' } = options
3434

35-
const skippedTests: AdapterTestName[] = []
36-
const allTests: AdapterTestName[] = []
37-
3835
const test = (name: string, runner: any) => {
3936
let skip = false
4037
if (defineOptions?.blacklist?.includes(name as AdapterTestName)) {
@@ -46,25 +43,60 @@ export const defineTestSuite = (defineOptions?: DefineTestSuiteOptions) => {
4643
) {
4744
skip = true
4845
}
49-
const its = skip ? it.skip : it
46+
const maybeSkip = skip ? it.skip : it
5047

51-
if (skip) {
52-
skippedTests.push(name as AdapterTestName)
53-
}
48+
maybeSkip(name, runner)
49+
}
5450

55-
allTests.push(name as AdapterTestName)
51+
describe(`app.service('${serviceName}') with options.id: '${idProp}'`, () => {
52+
beforeAll(async () => {
53+
const service = app.service(serviceName)
5654

57-
its(name, runner)
58-
}
55+
// test create
56+
const doug = await service.create({
57+
name: 'Doug',
58+
age: 32,
59+
})
60+
61+
assert.ok(
62+
doug[idProp] !== null,
63+
`simple 'create' failed (no ${idProp}). Before you start to test the adapter make sure simple create works.`,
64+
)
65+
assert.strictEqual(
66+
doug.name,
67+
'Doug',
68+
"simple 'create' failed (no name). Before you start to test the adapter make sure simple create works.",
69+
)
70+
assert.strictEqual(
71+
doug.age,
72+
32,
73+
"simple 'create' failed (no age). Before you start to test the adapter make sure simple create works.",
74+
)
75+
76+
// test delete
5977

60-
describe(`Adapter tests for '${serviceName}' service with '${idProp}' id property`, () => {
61-
afterAll(() => {
62-
if (skippedTests.length) {
63-
console.log(
64-
`\nSkipped the following ${skippedTests.length} Feathers adapter test(s) out of ${allTests.length} total:`,
65-
)
66-
console.log(JSON.stringify(skippedTests, null, ' '))
67-
}
78+
const items = await service.find({ paginate: false })
79+
assert.ok(
80+
Array.isArray(items),
81+
'find with paginate:false did not return an array. Before you start to test the adapter make sure simple find works.',
82+
)
83+
assert.strictEqual(
84+
items.length,
85+
1,
86+
'find should return an item. Before you start to test the adapter make sure simple find works.',
87+
)
88+
assert.ok(
89+
idProp in items[0],
90+
`'find' should return an item with ${idProp}. Before you start to test the adapter make sure simple find works.`,
91+
)
92+
await Promise.all(
93+
items.map((item: any) => service.remove(item[idProp])),
94+
)
95+
const itemsAfterRemove = await service.find({ paginate: false })
96+
assert.ok(
97+
itemsAfterRemove.length === 0,
98+
"'remove' does not work. Before you start to test the adapter make sure simple remove works.",
99+
)
68100
})
69101

70102
basicTests({ test, app, serviceName, idProp })

src/methods.ts

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -84,50 +84,6 @@ export default (options: MethodTestOptions) => {
8484

8585
beforeAll(async () => {
8686
service = app.service(serviceName)
87-
88-
// test create
89-
doug = await service.create({
90-
name: 'Doug',
91-
age: 32,
92-
})
93-
94-
assert.ok(
95-
doug[idProp] !== null,
96-
`simple 'create' failed (no ${idProp}). Before you start to test the adapter make sure simple create works.`,
97-
)
98-
assert.strictEqual(
99-
doug.name,
100-
'Doug',
101-
"simple 'create' failed (no name). Before you start to test the adapter make sure simple create works.",
102-
)
103-
assert.strictEqual(
104-
doug.age,
105-
32,
106-
"simple 'create' failed (no age). Before you start to test the adapter make sure simple create works.",
107-
)
108-
109-
// test delete
110-
111-
const items = await service.find({ paginate: false })
112-
assert.ok(
113-
Array.isArray(items),
114-
'find with paginate:false did not return an array. Before you start to test the adapter make sure simple find works.',
115-
)
116-
assert.strictEqual(
117-
items.length,
118-
1,
119-
'find should return an item. Before you start to test the adapter make sure simple find works.',
120-
)
121-
assert.ok(
122-
idProp in items[0],
123-
`'find' should return an item with ${idProp}. Before you start to test the adapter make sure simple find works.`,
124-
)
125-
await Promise.all(items.map((item: any) => service.remove(item[idProp])))
126-
const itemsAfterRemove = await service.find({ paginate: false })
127-
assert.ok(
128-
itemsAfterRemove.length === 0,
129-
"'remove' does not work. Before you start to test the adapter make sure simple remove works.",
130-
)
13187
})
13288

13389
beforeEach(async () => {

0 commit comments

Comments
 (0)