Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(signals): migrate signals testing suite from Jest to Vitest #4631

Merged
merged 9 commits into from
Dec 23, 2024
18 changes: 14 additions & 4 deletions modules/signals/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"projectType": "library",
"sourceRoot": "modules/signals/src",
"prefix": "ngrx",
"tags": [],
"generators": {},
"targets": {
"build-package": {
"executor": "@angular-devkit/build-angular:ng-packagr",
Expand Down Expand Up @@ -45,15 +47,23 @@
"outputs": ["{options.outputFile}"]
},
"test": {
"executor": "nx:run-commands",
"options": {
"commands": ["nx test-source signals", "nx test-schematics signals"]
}
},
"test-source": {
"executor": "@analogjs/vitest-angular:test"
},
"test-schematics": {
"executor": "@nx/jest:jest",
"options": {
"jestConfig": "modules/signals/jest.config.ts",
"runInBand": true,
"passWithNoTests": false
"passWithNoTests": false,
"testPathPattern": ["schematics", "migrations"]
},
"outputs": ["{workspaceRoot}/coverage/modules/signals"]
brandonroberts marked this conversation as resolved.
Show resolved Hide resolved
}
},
"generators": {},
"tags": []
}
}
14 changes: 9 additions & 5 deletions modules/signals/spec/signal-state.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as angular from '@angular/core';
import { computed } from '@angular/core';
import { effect, isSignal } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { patchState, signalState } from '../src';
import { STATE_SOURCE } from '../src/state-source';

vi.mock('@angular/core', { spy: true });

describe('signalState', () => {
const initialState = {
user: {
Expand All @@ -15,6 +17,10 @@ describe('signalState', () => {
ngrx: 'signals',
};

afterEach(() => {
vi.clearAllMocks();
});

it('has writable state source', () => {
const state = signalState({});
const stateSource = state[STATE_SOURCE];
Expand Down Expand Up @@ -46,19 +52,17 @@ describe('signalState', () => {
});

it('caches previously created signals', () => {
jest.spyOn(angular, 'computed');

const state = signalState(initialState);
const user1 = state.user;
const user2 = state.user;

expect(angular.computed).toHaveBeenCalledTimes(1);
expect(computed).toHaveBeenCalledTimes(1);

const _ = state.user.firstName;
const __ = user1.firstName;
const ___ = user2.firstName;

expect(angular.computed).toHaveBeenCalledTimes(2);
expect(computed).toHaveBeenCalledTimes(2);
});

it('does not modify props that are not state slices', () => {
Expand Down
6 changes: 3 additions & 3 deletions modules/signals/spec/signal-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,9 @@ describe('signalStore', () => {
}))
);
const warnings: string[][] = [];
jest
.spyOn(console, 'warn')
.mockImplementation((...args: string[]) => warnings.push(args));
vi.spyOn(console, 'warn').mockImplementation((...args: string[]) =>
warnings.push(args)
);

new Store();

Expand Down
2 changes: 1 addition & 1 deletion modules/signals/spec/with-computed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('withComputed', () => {
})),
].reduce((acc, feature) => feature(acc), getInitialInnerStore());
const s2 = signal(10).asReadonly();
jest.spyOn(console, 'warn').mockImplementation();
vi.spyOn(console, 'warn').mockImplementation();

withComputed(() => ({
p: signal(0).asReadonly(),
Expand Down
2 changes: 1 addition & 1 deletion modules/signals/spec/with-methods.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('withMethods', () => {
})),
].reduce((acc, feature) => feature(acc), getInitialInnerStore());
const m2 = () => 10;
jest.spyOn(console, 'warn').mockImplementation();
vi.spyOn(console, 'warn').mockImplementation();

withMethods(() => ({
p() {},
Expand Down
2 changes: 1 addition & 1 deletion modules/signals/spec/with-props.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('withProps', () => {
m2() {},
})),
].reduce((acc, feature) => feature(acc), getInitialInnerStore());
jest.spyOn(console, 'warn').mockImplementation();
vi.spyOn(console, 'warn').mockImplementation();

withProps(() => ({
s1: { foo: 'bar' },
Expand Down
2 changes: 1 addition & 1 deletion modules/signals/spec/with-state.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('withState', () => {
m2() {},
})),
].reduce((acc, feature) => feature(acc), getInitialInnerStore());
jest.spyOn(console, 'warn').mockImplementation();
vi.spyOn(console, 'warn').mockImplementation();

withState(() => ({
p2: 100,
Expand Down
14 changes: 12 additions & 2 deletions modules/signals/test-setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import 'zone.js/plugins/zone-legacy';
import 'jest-preset-angular/setup-jest';
import { TextEncoder, TextDecoder } from 'util';
import '@analogjs/vitest-angular/setup-zone';

import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting,
} from '@angular/platform-browser-dynamic/testing';
import { getTestBed } from '@angular/core/testing';

getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);

Object.assign(global, { TextDecoder, TextEncoder });
4 changes: 2 additions & 2 deletions modules/signals/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"],
"module": "es2022",
"types": ["jest", "node", "vitest", "vitest/globals"],
brandonroberts marked this conversation as resolved.
Show resolved Hide resolved
"target": "es2016"
},
"files": ["test-setup.ts"],
Expand Down
30 changes: 30 additions & 0 deletions modules/signals/vite.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/// <reference types="vitest" />

import angular from '@analogjs/vite-plugin-angular';

import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';

import { defineConfig } from 'vite';

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
return {
root: __dirname,
plugins: [
angular(),
nxViteTsPaths(),
],
test: {
globals: true,
pool: 'forks',
environment: 'jsdom',
setupFiles: ['test-setup.ts'],
include: ['**/*.spec.ts'],
exclude: ['migrations/**', 'schematics/**'],
brandonroberts marked this conversation as resolved.
Show resolved Hide resolved
reporters: ['default']
},
define: {
'import.meta.vitest': mode !== 'production',
},
};
});
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@
"zone.js": "0.15.0"
},
"devDependencies": {
"@analogjs/vite-plugin-angular": "^1.9.0",
"@analogjs/vitest-angular": "^1.9.0",
"@angular/build": "19.0.0",
"@angular-devkit/build-angular": "19.0.0",
"@angular-devkit/core": "19.0.0",
"@angular-devkit/schematics": "19.0.0",
Expand All @@ -105,6 +108,7 @@
"@nx/eslint-plugin": "20.1.2",
"@nx/jest": "20.1.2",
"@nx/node": "20.1.2",
"@nx/vite": "20.1.2",
"@nx/workspace": "20.1.2",
"@octokit/rest": "^15.17.0",
"@schematics/angular": "19.0.0",
Expand Down Expand Up @@ -151,6 +155,7 @@
"jest-environment-jsdom": "29.7.0",
"jest-jasmine2": "29.7.0",
"jest-preset-angular": "14.1.0",
"jsdom": "^22.0.0",
"karma": "6.4.0",
"karma-chrome-launcher": "3.1.0",
"karma-cli": "~1.0.1",
Expand Down Expand Up @@ -186,7 +191,8 @@
"tsutils": "2.27.2",
"typescript": "5.6.2",
"typescript-eslint": "8.15.0",
"uglify-js": "^3.1.9"
"uglify-js": "^3.1.9",
"vitest": "^2.0.0"
},
"collective": {
"type": "opencollective",
Expand Down
Loading
Loading