Skip to content

Commit 0f789fe

Browse files
committed
fix(testing): correct order of plugin installation
1 parent 4aeb0a5 commit 0f789fe

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

packages/testing/src/testing.spec.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,61 @@ describe('Testing', () => {
224224
store.n++
225225
expect(store.double).toBe(6)
226226
})
227+
228+
it('actions are stubbed even when replaced by other plugins', () => {
229+
const spy = jest.fn()
230+
mount(Counter, {
231+
global: {
232+
plugins: [
233+
createTestingPinia({
234+
plugins: [
235+
({ store }) => {
236+
const { increment } = store.increment
237+
store.increment = spy
238+
spy.mockImplementation(increment)
239+
},
240+
],
241+
}),
242+
],
243+
},
244+
})
245+
const counter = useCounter()
246+
247+
counter.increment()
248+
counter.increment(5)
249+
expect(counter.n).toBe(0)
250+
expect(counter.increment).toHaveBeenCalledTimes(2)
251+
expect(counter.increment).toHaveBeenLastCalledWith(5)
252+
// the actual spy is never called because we stub the action
253+
expect(spy).toHaveBeenCalledTimes(0)
254+
})
255+
256+
it('pass through replaced actions in plugins', () => {
257+
const spy = jest.fn()
258+
mount(Counter, {
259+
global: {
260+
plugins: [
261+
createTestingPinia({
262+
stubActions: false,
263+
plugins: [
264+
({ store }) => {
265+
const { increment } = store.increment
266+
store.increment = spy
267+
spy.mockImplementation(increment)
268+
},
269+
],
270+
}),
271+
],
272+
},
273+
})
274+
const counter = useCounter()
275+
276+
counter.increment()
277+
counter.increment(5)
278+
expect(counter.n).toBe(0)
279+
expect(counter.increment).toHaveBeenCalledTimes(2)
280+
expect(counter.increment).toHaveBeenLastCalledWith(5)
281+
expect(spy).toHaveBeenCalledTimes(2)
282+
expect(spy).toHaveBeenLastCalledWith(5)
283+
})
227284
})

packages/testing/src/testing.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ export function createTestingPinia({
109109
// allow computed to be manually overridden
110110
pinia._p.push(WritableComputed)
111111

112-
plugins.forEach((plugin) => pinia.use(plugin))
112+
// bypass waiting for the app to be installed to ensure the action stubbing happens last
113+
plugins.forEach((plugin) => pinia._p.push(plugin))
113114

114115
const createSpy =
115116
_createSpy ||

0 commit comments

Comments
 (0)