Skip to content

Commit

Permalink
test(signals): migrate signals testing suite from Jest to Vitest (#4631)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts authored Dec 23, 2024
1 parent 57d0c52 commit 5e98142
Show file tree
Hide file tree
Showing 17 changed files with 471 additions and 59 deletions.
22 changes: 0 additions & 22 deletions modules/signals/jest.config.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import { createWorkspace } from '@ngrx/schematics-core/testing';
import { tags } from '@angular-devkit/core';

describe('migrate protectedState', () => {
const collectionPath = path.join(__dirname, '../migration.json');
const collectionPath = path.join(
process.cwd(),
'dist/modules/signals/migrations/migration.json'
);
const schematicRunner = new SchematicTestRunner('schematics', collectionPath);

let appTree: UnitTestTree;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import * as path from 'path';
import { LogEntry } from '@angular-devkit/core/src/logger';

describe('18_0_0-rc_3-writablestatesource', () => {
const collectionPath = path.join(__dirname, '../migration.json');
const collectionPath = path.join(
process.cwd(),
'dist/modules/signals/migrations/migration.json'
);
const schematicRunner = new SchematicTestRunner('schematics', collectionPath);

let appTree: UnitTestTree;
Expand Down
5 changes: 4 additions & 1 deletion modules/signals/migrations/19_0_0-rc_0-props/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import { createWorkspace } from '@ngrx/schematics-core/testing';
import { tags } from '@angular-devkit/core';

describe('migrate to props', () => {
const collectionPath = path.join(__dirname, '../migration.json');
const collectionPath = path.join(
process.cwd(),
'dist/modules/signals/migrations/migration.json'
);
const schematicRunner = new SchematicTestRunner('schematics', collectionPath);

let appTree: UnitTestTree;
Expand Down
14 changes: 5 additions & 9 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,9 @@
"outputs": ["{options.outputFile}"]
},
"test": {
"executor": "@nx/jest:jest",
"options": {
"jestConfig": "modules/signals/jest.config.ts",
"runInBand": true,
"passWithNoTests": false
},
"executor": "@analogjs/vitest-angular:test",
"dependsOn": ["build"],
"outputs": ["{workspaceRoot}/coverage/modules/signals"]
}
},
"generators": {},
"tags": []
}
}
2 changes: 1 addition & 1 deletion modules/signals/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { createWorkspace } from '@ngrx/schematics-core/testing';
describe('Signals ng-add Schematic', () => {
const schematicRunner = new SchematicTestRunner(
'@ngrx/signals',
path.join(__dirname, '../collection.json')
path.join(process.cwd(), 'dist/modules/signals/schematics/collection.json')
);
const defaultOptions: SchemaOptions = {
skipPackageJson: false,
Expand Down
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 });
6 changes: 3 additions & 3 deletions modules/signals/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"],
"module": "es2022",
"types": ["node", "vitest", "vitest/globals"],
"target": "es2016"
},
"files": ["test-setup.ts"],
"include": ["jest.config.ts", "**/*.spec.ts", "**/*.test.ts", "**/*.d.ts"]
"include": ["**/*.spec.ts", "**/*.test.ts", "**/*.d.ts"]
}
29 changes: 29 additions & 0 deletions modules/signals/vite.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// <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'],
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

0 comments on commit 5e98142

Please sign in to comment.