Skip to content

Commit 24f87d7

Browse files
authored
chore: add test reports (#37)
* chore: add test reports * test: enhance coverage * test: enhance coverage
1 parent 3f53af1 commit 24f87d7

File tree

9 files changed

+85
-1
lines changed

9 files changed

+85
-1
lines changed

.github/workflows/test.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,10 @@ jobs:
3434
uses: codecov/codecov-action@v3
3535
env:
3636
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
37+
38+
- name: Upload test results to Codecov
39+
if: ${{ !cancelled() }}
40+
uses: codecov/test-results-action@v1
41+
with:
42+
token: ${{ secrets.CODECOV_TOKEN }}
43+
files: ./test-reports/junit.xml

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
dist/
22
node_modules/
33
coverage/
4+
test-reports/

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
},
5454
"scripts": {
5555
"build": "unbuild",
56-
"test": "vitest run --coverage",
56+
"test": "vitest run",
5757
"test:vue": "vue-demi-switch 3 && vitest run --dir ./src/vue",
5858
"test:vue-2": "vue-demi-switch 2 vue-2 && vitest run --dir ./src/vue",
5959
"test:vue-2.7": "vue-demi-switch 2.7 vue-2.7 && vitest run --dir ./src/vue",

src/core/index-v4.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,6 @@ describe('core (v4)', () => {
124124
})
125125

126126
expect(unsubscribe).toBeTypeOf('function')
127+
unsubscribe()
127128
})
128129
})

src/core/index.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,6 @@ describe('core', () => {
122122
})
123123

124124
expect(unsubscribe).toBeTypeOf('function')
125+
unsubscribe()
125126
})
126127
})

src/core/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,15 @@ export function subscribeQueryCallbacks<
5656

5757
switch (event.action.type) {
5858
case 'success': {
59+
/* istanbul ignore else */
5960
if (!event.action.manual) {
6061
onSuccess?.(event.action.data)
6162
onSettled?.(event.action.data, null)
6263
}
6364
break
6465
}
6566
case 'error': {
67+
/* istanbul ignore else */
6668
if (!isCancelledError(event.action.error)) {
6769
onError?.(event.action.error)
6870
onSettled?.(undefined, event.action.error)

src/vue/index-v4.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { QueryClient, useQuery, useQueryClient, VueQueryPlugin } from '@tanstack/vue-query-v4'
22
import { waitFor } from '@testing-library/vue'
33
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
4+
import { inject } from 'vue-demi'
45
import { cleanUp, mountSetup } from '../../test/vue-mount'
56
import { useQueryCallbacks } from './index'
67

@@ -144,6 +145,38 @@ describe('vue (v4)', () => {
144145
expect(onSuccess).toBeCalledTimes(1)
145146
expect(onSuccess).toBeCalledWith('bar')
146147
})
148+
149+
it('should call onSuccess with `queryClientKey`', async () => {
150+
const onSuccess = vi.fn()
151+
const QUERY_KEY = ['foo']
152+
const queryClientKey = '_vue_query_'
153+
154+
const { result: query } = mountSetup(() => {
155+
const queryClient = inject(queryClientKey) as QueryClient
156+
const result = useQuery({
157+
queryKey: QUERY_KEY,
158+
queryFn: () => Promise.resolve('bar'),
159+
queryClient,
160+
})
161+
162+
useQueryCallbacks({
163+
queryKey: QUERY_KEY,
164+
queryClientKey,
165+
onSuccess,
166+
})
167+
168+
return result
169+
}, (app) => {
170+
app.provide(queryClientKey, new QueryClient())
171+
})
172+
173+
expect(query.data.value).toBeUndefined()
174+
await waitFor(() => expect(query.data.value).not.toBeUndefined())
175+
176+
expect(query.data.value).toBe('bar')
177+
expect(onSuccess).toBeCalledTimes(1)
178+
expect(onSuccess).toBeCalledWith('bar')
179+
})
147180
})
148181

149182
function useQueryClientSetup<T>(

src/vue/index.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { QueryClient, useQuery, useQueryClient, VueQueryPlugin } from '@tanstack/vue-query'
22
import { waitFor } from '@testing-library/vue'
33
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
4+
import { inject } from 'vue-demi'
45
import { cleanUp, mountSetup } from '../../test/vue-mount'
56
import { useQueryCallbacks } from './index'
67

@@ -139,6 +140,37 @@ describe('vue', () => {
139140
expect(onSuccess).toBeCalledTimes(1)
140141
expect(onSuccess).toBeCalledWith('bar')
141142
})
143+
144+
it('should call onSuccess with `queryClientKey`', async () => {
145+
const onSuccess = vi.fn()
146+
const QUERY_KEY = ['foo']
147+
const queryClientKey = '_vue_query_'
148+
149+
const { result: query } = mountSetup(() => {
150+
const queryClient = inject(queryClientKey) as QueryClient
151+
const result = useQuery({
152+
queryKey: QUERY_KEY,
153+
queryFn: () => Promise.resolve('bar'),
154+
}, queryClient)
155+
156+
useQueryCallbacks({
157+
queryKey: QUERY_KEY,
158+
queryClientKey,
159+
onSuccess,
160+
})
161+
162+
return result
163+
}, (app) => {
164+
app.provide(queryClientKey, new QueryClient())
165+
})
166+
167+
expect(query.data.value).toBeUndefined()
168+
await waitFor(() => expect(query.data.value).not.toBeUndefined())
169+
170+
expect(query.data.value).toBe('bar')
171+
expect(onSuccess).toBeCalledTimes(1)
172+
expect(onSuccess).toBeCalledWith('bar')
173+
})
142174
})
143175

144176
function useQueryClientSetup<T>(

vitest.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ export default defineConfig({
55
dir: './src',
66
environment: 'happy-dom',
77
coverage: {
8+
enabled: true,
89
provider: 'istanbul',
910
},
11+
reporters: [
12+
'junit',
13+
],
14+
outputFile: {
15+
junit: './test-reports/junit.xml',
16+
},
1017
},
1118
})

0 commit comments

Comments
 (0)