🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

@jscutlery/swc-angular

Package Overview
Dependencies
Maintainers
2
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jscutlery/swc-angular

<!-- TOC -->

0.20.5
latest
Source
npm
Version published
Weekly downloads
2.3K
-34.22%
Maintainers
2
Weekly downloads
 
Created
Source

⚡️ Speed Up Angular Testing with SWC 🦀

👀 What is this?

This is a set of Angular presets that enable you to use SWC (Speedy Web Compiler) with Jest or Vitest.

Switching to SWC can speed up your tests by 2x to 12x.

🤔 Context

Surprisingly, in most cases, the bottleneck in Jest & Vitest tests is not the test execution time nor the Angular JIT (Just-In-Time) compiler but the TypeScript transformer.

This is where SWC (Speedy Web Compiler) comes in. SWC is a JavaScript/TypeScript compiler that aims to be extremely fast.

This package enables you to use SWC with Angular projects by setting the right configuration for SWC and using our Angular plugin for SWC @jscutlery/swc-angular-plugin

🥇 Benchmark Summary

Benchmark

This chart is based on the benchmark where all caches were disabled as in most cases we will still have to transform the code that we changed and which is affecting our tests. In addition to that, incremental build tools like Nx (or jest/vitest changedSince/changed options) will reduce the amount of transforms.

Cf. Detailed Benchmark Data

🎭 Setup with Jest

1. Install

Install this preset and its dependencies via npm:

npm install -D @jscutlery/swc-angular @jscutlery/swc-angular-plugin @swc/core @swc/jest
grep .swc .gitignore || echo .swc >> .gitignore

2. Configure

In your Jest configuration file (e.g., jest.config.ts), use the swcAngularJestTransformer function to transform .ts, .js, and .mjs files.

import { swcAngularJestTransformer } from '@jscutlery/swc-angular';

export default {
  // ...
  transform: {
    '^.+\\.(ts|mjs|js)$': swcAngularJestTransformer(),
    '^.+\\.(html)$': [
      'jest-preset-angular',
      {
        tsconfig: '<rootDir>/tsconfig.spec.json',
        stringifyContentPathRegex: '\\.(html|svg)$',
      },
    ],
  },
  transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
  // ...
};

3. Add reflect-metadata

In order for constructor injection to work, you need to import reflect-metadata in the setup file which is probably located at src/test-setup.ts.

import 'reflect-metadata';

⚡️Setup with Vitest

1. Install

Install this preset and its dependencies via npm:

npm install -D @jscutlery/swc-angular @jscutlery/swc-angular-plugin @swc/core unplugin-swc
grep .swc .gitignore || echo .swc >> .gitignore

2. Configure

In your vite configuration file (e.g., vite.config.ts), use the unplugin-swc plugin with our preset: swcAngularVitePreset.

import { swcAngularUnpluginOptions } from '@jscutlery/swc-angular';
import swc from 'unplugin-swc';
import { defineConfig } from 'vite';

export default defineConfig({
  // ...
  plugins: [swc.vite(swcAngularUnpluginOptions())],
  // ...
});

3. Add reflect-metadata

In order for constructor injection to work, you need to import reflect-metadata in the setup file which is probably located at src/test-setup.ts.

import 'reflect-metadata';

🙋‍♂️ F.A.Q.

TypeScript property inheritance issue

If you encounter an issue with TypeScript property inheritance (Cf. https://github.com/jscutlery/devkit/issues/376), then you can try to disable the useDefineForClassFields option:

swcAngularJestTransformer({ useDefineForClassFields: false });

or

swcAngularUnpluginOptions({ useDefineForClassFields: false });

🙏 Thanks to @santiagof4, @pumano, and @ryan-bendel.

🥇 Benchmark Data

🔥 No Cache

All caches are cleared before each benchmark run.

ConfigurationRelativeMean [s]Min [s]Max [s]
🥇vitest-swc-no-isolate1.003.137 ± 0.0613.0423.227
🥈jest-swc3.31 ± 0.1510.393 ± 0.4299.74510.994
🥉angular-cli-karma3.52 ± 0.5311.041 ± 1.64210.21815.690
angular-cli-web-test-runner5.46 ± 0.4517.118 ± 1.36216.47320.911
jest5.78 ± 0.2518.132 ± 0.71617.55319.998
angular-cli-jest8.61 ± 0.6327.009 ± 1.90224.93830.450
vitest-swc8.94 ± 0.2128.038 ± 0.40227.53928.617
vitest11.43 ± 0.3235.872 ± 0.72734.45237.340
vitest-no-isolate11.49 ± 0.2536.061 ± 0.36935.41436.502

⚡️ With Cache

All tests are ran once to warm up the cache before running the benchmark.

ConfigurationRelativeMean [s]Min [s]Max [s]
🥇vitest-swc-no-isolate1.003.092 ± 0.0903.0303.301
🥈angular-cli-karma1.91 ± 0.075.901 ± 0.1395.7186.131
🥉jest-swc2.93 ± 0.189.045 ± 0.4948.51310.065
jest4.07 ± 0.2512.581 ± 0.66311.57213.915
angular-cli-web-test-runner5.96 ± 0.2918.428 ± 0.71117.49719.262
angular-cli-jest8.46 ± 1.0426.170 ± 3.11723.35031.983
vitest-swc9.28 ± 0.3628.691 ± 0.72827.97730.488
vitest-no-isolate11.74 ± 0.4536.286 ± 0.88035.10738.018
vitest12.18 ± 0.6337.666 ± 1.62535.56341.503

🥱 Cold Start

Run a single test module just and try to measure the bootstrap time.

ConfigurationRelativeMean [s]Min [s]Max [s]
🥇vitest-swc-no-isolate1.001.260 ± 0.0241.2411.314
🥈vitest-swc1.00 ± 0.031.262 ± 0.0261.2251.305
🥉jest-swc1.44 ± 0.151.812 ± 0.1861.7142.310
vitest-no-isolate1.47 ± 0.051.846 ± 0.0561.7971.949
vitest1.61 ± 0.162.030 ± 0.1981.8142.423
jest1.69 ± 0.082.136 ± 0.0942.0172.356
angular-cli-jest1.73 ± 0.392.186 ± 0.4951.8693.119
angular-cli-web-test-runner2.06 ± 1.152.602 ± 1.4512.0806.730
angular-cli-karma5.08 ± 1.306.400 ± 1.6365.37710.177

Configurations

ConfigurationTest Isolation*
angular-cli-karmaAngular CLI's Karmafalse
angular-cli-jestAngular CLI's Jesttrue
angular-cli-web-test-runnerAngular CLI's @web/test-runnertrue
jestJesttrue
jest-swcJest + SWCtrue
vitestVitesttrue
vitest-no-isolateVitest + No Isolationfalse
vitest-swcVitest + SWCtrue
vitest-swc-no-isolateVitest + SWC + No Isolationfalse
  • Test Isolation: this tells whether each test module has its own testing environment or if each one is isolated. (i.e. globals like the DOM are shared, asynchronous code might bleed between tests, etc...)

Source

💻 try it yourself: https://github.com/yjaaidi/experiments/tree/angular-swc-benchmark or checkout the last benchmark results: https://github.com/yjaaidi/experiments/actions/workflows/benchmark.yml

Keywords

SWC

FAQs

Package last updated on 31 May 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts