javascript / expert
Snippet
Unit Testing Typed Array In-Place Sorting inside Pinia Store Actions
Testing high-performance applications that rely on JavaScript TypedArrays inside Pinia state requires isolating the Pinia instance before each unit test and validating that in-place array transformations update correctly.
snippet.js
javascript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { setActivePinia, createPinia } from 'pinia';import { describe, beforeEach, it, expect } from 'vitest';import { useDataStore } from './dataStore';describe('DataStore Float64Array Operations', () => {beforeEach(() => {setActivePinia(createPinia());});it('mutates Float64Array buffer in-place without breaking reactivity observers', () => {const store = useDataStore();const buffer = new Float64Array([3.14, 1.41, 2.71]);store.loadBuffer(buffer);store.sortBufferInPlace();expect(Array.from(store.rawBuffer)).toEqual([1.41, 2.71, 3.14]);});});
vue
Breakdown
1
setActivePinia(createPinia());
Instantiates a fresh Pinia instance to prevent state leakage between individual test runs.
2
const buffer = new Float64Array([3.14, 1.41, 2.71]);
Creates a binary Float64Array instance for high-performance numerical computing.
3
store.sortBufferInPlace();
Executes a store action performing in-place sorting on the underlying typed array.
4
expect(Array.from(store.rawBuffer)).toEqual([1.41, 2.71, 3.14]);
Converts TypedArray to standard Array format to verify exact numerical element sorting.