Skip to content

Commit

Permalink
chore: upgrade vitest to 1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
geekact committed Feb 26, 2024
1 parent 1f7e3e5 commit b1771c3
Show file tree
Hide file tree
Showing 11 changed files with 860 additions and 992 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"@types/node": "^20.4.9",
"@types/react": "^18.2.20",
"@types/react-dom": "^18.2.7",
"@vitest/coverage-v8": "^0.34.3",
"@vitest/coverage-istanbul": "^1.3.1",
"browserslist": "^4.21.10",
"docsify-cli": "^4.4.4",
"fake-indexeddb": "^4.0.2",
Expand All @@ -91,6 +91,6 @@
"ts-expect": "^1.3.0",
"tsup": "^8.0.2",
"typescript": "^5.3.3",
"vitest": "^0.34.3"
"vitest": "^1.3.1"
}
}
1,758 changes: 797 additions & 961 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/model/clone-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const cloneModel = <
if (options) {
Object.assign(nextOpts, isFunction(options) ? options(nextOpts) : options);

/* istanbul ignore else -- @preserve */
if (process.env.NODE_ENV !== 'production') {
(Object.keys(nextOpts) as EditableKeys[]).forEach((key) => {
if (
Expand Down
2 changes: 2 additions & 0 deletions src/model/define-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const defineModel = <
? freeze(options.initialState, true)
: options.initialState;

/* istanbul ignore else -- @preserve */
if (process.env.NODE_ENV !== 'production') {
const items = [
{ name: 'reducers', value: reducers },
Expand All @@ -76,6 +77,7 @@ export const defineModel = <
validateUniqueMethod(1, 2);
}

/* istanbul ignore else -- @preserve */
if (process.env.NODE_ENV !== 'production') {
if (!deepEqual(parseState(stringifyState(initialState)), initialState)) {
throw new Error(
Expand Down
1 change: 1 addition & 0 deletions src/persist/persist-gate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const PersistGate: FC<PersistGateProps> = (props) => {
});
}, []);

/* istanbul ignore else -- @preserve */
if (process.env.NODE_ENV !== 'production') {
if (loading && isFunction(children)) {
console.error('[PersistGate] 当前children为函数类型,loading属性无效');
Expand Down
1 change: 1 addition & 0 deletions src/reactive/object-deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export class ObjectDeps<T = any> implements Deps {
});
}

/* istanbul ignore else -- @preserve */
if (process.env.NODE_ENV !== 'production') {
Object.freeze(nextState);
}
Expand Down
1 change: 1 addition & 0 deletions src/store/loading-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export class LoadingStore extends StoreBasic<LoadingStoreState> {
destroyLoadingInterceptor,
];

/* istanbul ignore else -- @preserve */
if (process.env.NODE_ENV !== 'production') {
middleware.push(freezeStateMiddleware);
}
Expand Down
4 changes: 2 additions & 2 deletions src/store/model-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export class ModelStore extends StoreBasic<Record<string, any>> {

if (firstInitialize) {
const middleware = (options.middleware || []).concat(modelInterceptor);
/* istanbul ignore else -- @preserve */
if (process.env.NODE_ENV !== 'production') {
middleware.unshift(actionInActionInterceptor);
middleware.push(freezeStateMiddleware);
Expand Down Expand Up @@ -146,7 +147,7 @@ export class ModelStore extends StoreBasic<Record<string, any>> {

protected getCompose(customCompose: CreateStoreOptions['compose']): Compose {
if (customCompose === 'redux-devtools') {
/* c8 ignore start */
/* istanbul ignore if -- @preserve */
if (process.env.NODE_ENV !== 'production') {
return (
/** @ts-expect-error */
Expand All @@ -157,7 +158,6 @@ export class ModelStore extends StoreBasic<Record<string, any>> {
: {})['__REDUX_DEVTOOLS_EXTENSION_COMPOSE__'] || compose
);
}
/* c8 ignore end */

return compose;
}
Expand Down
24 changes: 24 additions & 0 deletions test/computed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,30 @@ test('ComputedValue can remove duplicated deps', () => {
expect(computedValue.deps).toHaveLength(3);
});

test('计算属性包含子计算属性时,子计算属性被作为一个整体', () => {
const mockStore = {
getState() {
return {
[computedModel.name]: store.getState()[computedModel.name],
};
},
};

const computedValue = new ComputedValue(
mockStore,
computedModel.name,
'prop',
() => {
return computedModel.fullName() + 1;
},
);

expect(computedValue.value).toBe('ticktock1');
computedModel.changeFirstName('hello-');
expect(computedValue.isDirty()).toBeTruthy();
expect(computedValue.value).toBe('hello-tock1');
});

test('only execute computed function when deps changed', () => {
const spy = vitest.fn().mockImplementation(() => {
model.state.a;
Expand Down
43 changes: 22 additions & 21 deletions test/persist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,28 +501,29 @@ describe('merge method', () => {
models: [],
});

test.each(['replace', 'merge', 'deep-merge'] satisfies PersistMergeMode[])(
'array type',
(mode) => {
test('object + array', () => {
expect(
persistItem.merge({ hello: 'world' }, [{ foo: 'bar' }, {}], mode),
).toStrictEqual([{ foo: 'bar' }, {}]);
describe.each([
'replace',
'merge',
'deep-merge',
] satisfies PersistMergeMode[])('array type', (mode) => {
test('object + array', () => {
expect(
persistItem.merge({ hello: 'world' }, [{ foo: 'bar' }, {}], mode),
).toStrictEqual([{ foo: 'bar' }, {}]);
});
test('array + array', () => {
expect(
persistItem.merge([{ tick: 'tock' }], [{ foo: 'bar' }, {}], mode),
).toStrictEqual([{ tick: 'tock' }]);
});
test('array + object', () => {
expect(
persistItem.merge([{ tick: 'tock' }], { hello: 'world' }, mode),
).toStrictEqual({
hello: 'world',
});
test('array + array', () => {
expect(
persistItem.merge([{ tick: 'tock' }], [{ foo: 'bar' }, {}], mode),
).toStrictEqual([{ tick: 'tock' }]);
});
test('array + object', () => {
expect(
persistItem.merge([{ tick: 'tock' }], { hello: 'world' }, mode),
).toStrictEqual({
hello: 'world',
});
});
},
);
});
});

test('replace', () => {
expect(
Expand Down
13 changes: 7 additions & 6 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import { defineConfig } from 'vitest/config';

export default defineConfig({
test: {
threads: true,
coverage: {
provider: 'v8',
provider: 'istanbul',
enabled: true,
include: ['src/**'],
lines: 99,
functions: 99,
branches: 99,
statements: 99,
thresholds: {
lines: 99,
functions: 99,
branches: 99,
statements: 99,
},
reporter: ['html', 'lcovonly', 'text-summary'],
},
environment: 'jsdom',
Expand Down

0 comments on commit b1771c3

Please sign in to comment.