Skip to content

Commit d4e6052

Browse files
refactor(store-devtools): migrate unit tests to Vitest (#5041)
Closes #5019
1 parent 213a8fe commit d4e6052

File tree

16 files changed

+183
-170
lines changed

16 files changed

+183
-170
lines changed

modules/store-devtools/eslint.config.mjs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ const compat = new FlatCompat({
1111

1212
export default [
1313
{
14-
ignores: ['**/dist', '**/jest.config.ts', '**/schematics-core/**/*.ts'],
14+
ignores: [
15+
'**/dist',
16+
'**/schematics-core/**/*.ts',
17+
'**/vite.config.*.timestamp*',
18+
'**/vitest.config.*.timestamp*',
19+
],
1520
},
1621
...baseConfig,
1722
...compat

modules/store-devtools/jest.config.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

modules/store-devtools/migrations/17_0_0-beta/index.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import { waitForAsync } from '@angular/core/testing';
99

1010
describe('DevTools Migration 17_0_0-beta', () => {
1111
let appTree: UnitTestTree;
12-
const collectionPath = path.join(__dirname, '../migration.json');
12+
const collectionPath = path.join(
13+
process.cwd(),
14+
'dist/modules/store-devtools/migrations/migration.json'
15+
);
1316
const pkgName = 'store-devtools';
1417
const migrationname = `ngrx-${pkgName}-migration-17-0-0-beta`;
1518

modules/store-devtools/migrations/6_0_0/index.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import {
1313
versionPrefixes,
1414
} from '@ngrx/schematics-core/testing/update';
1515

16-
const collectionPath = path.join(__dirname, '../migration.json');
16+
const collectionPath = path.join(
17+
process.cwd(),
18+
'dist/modules/store-devtools/migrations/migration.json'
19+
);
1720

1821
describe('Store Devtools Migration 6_0_0', () => {
1922
let appTree;

modules/store-devtools/project.json

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,24 @@
3232
{
3333
"command": "pnpm exec tsc -p modules/store-devtools/tsconfig.schematics.json"
3434
},
35+
{
36+
"command": "pnpm exec rimraf node_modules/@ngrx/store-devtools"
37+
},
38+
{
39+
"command": "pnpm exec mkdirp node_modules/@ngrx/store-devtools"
40+
},
41+
{
42+
"command": "ncp dist/modules/store-devtools node_modules/@ngrx/store-devtools"
43+
},
3544
{
3645
"command": "cpy LICENSE dist/modules/store-devtools"
3746
}
3847
]
3948
},
40-
"outputs": ["{workspaceRoot}/dist/modules/store-devtools"]
49+
"outputs": [
50+
"{workspaceRoot}/dist/modules/store-devtools",
51+
"{workspaceRoot}/node_modules/@ngrx/store-devtools"
52+
]
4153
},
4254
"lint": {
4355
"executor": "@nx/eslint:lint",
@@ -50,12 +62,8 @@
5062
"outputs": ["{options.outputFile}"]
5163
},
5264
"test": {
53-
"executor": "@nx/jest:jest",
54-
"options": {
55-
"jestConfig": "modules/store-devtools/jest.config.ts",
56-
"runInBand": true,
57-
"passWithNoTests": false
58-
},
65+
"executor": "@analogjs/vitest-angular:test",
66+
"dependsOn": ["build"],
5967
"outputs": ["{workspaceRoot}/coverage/modules/store-devtools"]
6068
}
6169
}

modules/store-devtools/schematics-core/jest.config.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

modules/store-devtools/schematics-core/jest.config.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

modules/store-devtools/schematics/ng-add/__snapshots__/index.spec.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`Store-Devtools ng-add Schematic Store Devtools ng-add Schematic for standalone application provides initial setup 1`] = `
3+
exports[`Store-Devtools ng-add Schematic > Store Devtools ng-add Schematic for standalone application > provides initial setup 1`] = `
44
"import { ApplicationConfig, provideBrowserGlobalErrorListeners, isDevMode } from '@angular/core';
55
import { provideStoreDevtools } from '@ngrx/store-devtools';
66

modules/store-devtools/schematics/ng-add/index.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import {
1212
describe('Store-Devtools ng-add Schematic', () => {
1313
const schematicRunner = new SchematicTestRunner(
1414
'@ngrx/store-devtools',
15-
path.join(__dirname, '../collection.json')
15+
path.join(
16+
process.cwd(),
17+
'dist/modules/store-devtools/schematics/collection.json'
18+
)
1619
);
1720
const defaultOptions: StoreDevtoolsOptions = {
1821
skipPackageJson: false,

modules/store-devtools/spec/extension.spec.ts

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { LiftedActions, ComputedState, LiftedAction } from './../src/reducer';
2-
import { PerformAction, PERFORM_ACTION } from './../src/actions';
1+
import { ComputedState, LiftedAction, LiftedActions } from './../src/reducer';
2+
import { PERFORM_ACTION, PerformAction } from './../src/actions';
33
import {
4-
ReduxDevtoolsExtensionConnection,
5-
ReduxDevtoolsExtensionConfig,
6-
REDUX_DEVTOOLS_EXTENSION,
74
ExtensionActionTypes,
5+
REDUX_DEVTOOLS_EXTENSION,
6+
ReduxDevtoolsExtensionConfig,
7+
ReduxDevtoolsExtensionConnection,
88
} from './../src/extension';
99
import { Action } from '@ngrx/store';
1010

@@ -18,7 +18,7 @@ import {
1818
import { unliftState } from '../src/utils';
1919
import { TestBed } from '@angular/core/testing';
2020
import { DevtoolsDispatcher } from '../src/devtools-dispatcher';
21-
import { inject } from '@angular/core';
21+
import { Mock, vi } from 'vitest';
2222

2323
function createOptions(
2424
name = 'NgRx Store DevTools',
@@ -84,21 +84,19 @@ function createState(
8484

8585
const testSetup = (options: { config: StoreDevtoolsConfig }) => {
8686
const reduxDevtoolsExtension = {
87-
send: jasmine.createSpy('send'),
88-
connect: jasmine.createSpy('connect'),
87+
send: vi.fn(),
88+
connect: vi.fn(),
8989
};
9090

9191
const extensionConnection = {
92-
init: jasmine.createSpy('init'),
93-
subscribe: jasmine.createSpy('subscribe'),
94-
unsubscribe: jasmine.createSpy('unsubscribe'),
95-
send: jasmine.createSpy('send'),
96-
error: jasmine.createSpy('error'),
92+
init: vi.fn(),
93+
subscribe: vi.fn(),
94+
unsubscribe: vi.fn(),
95+
send: vi.fn(),
96+
error: vi.fn(),
9797
};
9898

99-
(reduxDevtoolsExtension.connect as jasmine.Spy).and.returnValue(
100-
extensionConnection
101-
);
99+
reduxDevtoolsExtension.connect.mockReturnValue(extensionConnection);
102100

103101
TestBed.configureTestingModule({
104102
// Provide both the service-to-test and its (spy) dependency
@@ -179,7 +177,7 @@ describe('DevtoolsExtension', () => {
179177
// Subscription needed or else extension connection will not be established.
180178
devtoolsExtension.actions$.subscribe(() => null);
181179
expect(reduxDevtoolsExtension.connect).toHaveBeenCalledWith(
182-
jasmine.objectContaining({ serialize: customSerializer })
180+
expect.objectContaining({ serialize: customSerializer })
183181
);
184182
});
185183

@@ -202,7 +200,7 @@ describe('DevtoolsExtension', () => {
202200
return (unwrappedAction = action);
203201
});
204202

205-
const [callback] = extensionConnection.subscribe.calls.mostRecent().args;
203+
const [callback] = extensionConnection.subscribe.mock.lastCall;
206204
callback({ type: ExtensionActionTypes.START });
207205
callback({ type: ExtensionActionTypes.ACTION, payload });
208206
expect(unwrappedAction).toEqual({
@@ -541,14 +539,12 @@ describe('DevtoolsExtension', () => {
541539
const NORMAL_ACTION = 'NORMAL_ACTION';
542540
const RANDOM_ACTION = 'RANDOM_ACTION';
543541

544-
const predicate = jasmine
545-
.createSpy('predicate', (state: any, action: Action) => {
546-
if (action.type === RANDOM_ACTION) {
547-
return false;
548-
}
549-
return true;
550-
})
551-
.and.callThrough();
542+
const predicate = vi.fn((state: any, action: Action) => {
543+
if (action.type === RANDOM_ACTION) {
544+
return false;
545+
}
546+
return true;
547+
});
552548

553549
let devtoolsExtension: DevtoolsExtension;
554550
let extensionConnection: ReduxDevtoolsExtensionConnection;
@@ -669,7 +665,7 @@ describe('DevtoolsExtension', () => {
669665
});
670666

671667
describe('error handling', () => {
672-
let consoleSpy: jasmine.Spy;
668+
let consoleSpy: Mock;
673669

674670
let devtoolsExtension: DevtoolsExtension;
675671
let extensionConnection: ReduxDevtoolsExtensionConnection;
@@ -682,11 +678,11 @@ describe('DevtoolsExtension', () => {
682678
}));
683679
// Subscription needed or else extension connection will not be established.
684680
devtoolsExtension.actions$.subscribe();
685-
consoleSpy = spyOn(console, 'warn');
681+
consoleSpy = vi.spyOn(console, 'warn');
686682
});
687683

688684
it('for normal action', () => {
689-
(extensionConnection.send as jasmine.Spy).and.callFake(() => {
685+
(extensionConnection.send as Mock).mockImplementation(() => {
690686
throw new Error('uh-oh something went wrong');
691687
});
692688

@@ -698,7 +694,7 @@ describe('DevtoolsExtension', () => {
698694
});
699695

700696
it('for action that requires full state update', () => {
701-
(reduxDevtoolsExtension.send as jasmine.Spy).and.callFake(() => {
697+
(reduxDevtoolsExtension.send as Mock).mockImplementation(() => {
702698
throw new Error('uh-oh something went wrong');
703699
});
704700

0 commit comments

Comments
 (0)