@liively/swc-jest-coverage-nestjs-plugin 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Liively Pty Ltd
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,145 @@
1
+ # @liively/swc-jest-coverage-nestjs-plugin
2
+
3
+ SWC plugin that eliminates phantom coverage targets generated by NestJS decorator transpilation, giving you accurate function and branch coverage when using `@swc/jest`.
4
+
5
+ ## Problem
6
+
7
+ When SWC transpiles TypeScript decorators it generates wrapper code — arrow functions for type parameters, `_ts_metadata` calls for reflection — that Istanbul/Jest counts as coverable targets. These targets can never be "called" by your tests, so your coverage reports show artificially low numbers (e.g. 64% function coverage on a fully-tested resolver).
8
+
9
+ This plugin transforms the generated code at compile time so that Istanbul sees only the targets that correspond to real application logic.
10
+
11
+ ## Installation
12
+
13
+ ```bash
14
+ npm install --save-dev @liively/swc-jest-coverage-nestjs-plugin
15
+ # or
16
+ pnpm add --save-dev @liively/swc-jest-coverage-nestjs-plugin
17
+ # or
18
+ yarn add --dev @liively/swc-jest-coverage-nestjs-plugin
19
+ ```
20
+
21
+ ## Configuration
22
+
23
+ Add the plugin to your Jest SWC config (usually in `jest.config.js` or `jest.config.ts`):
24
+
25
+ ```js
26
+ // jest.config.js
27
+ module.exports = {
28
+ transform: {
29
+ '^.+\\.tsx?$': [
30
+ '@swc/jest',
31
+ {
32
+ jsc: {
33
+ experimental: {
34
+ plugins: [
35
+ ['@liively/swc-jest-coverage-nestjs-plugin', {
36
+ // All options shown with their defaults:
37
+ // unwrapTypeArrows: true,
38
+ // unwrapDecoratorArrows: true,
39
+ // stripMetadata: false,
40
+ }],
41
+ ],
42
+ },
43
+ },
44
+ },
45
+ ],
46
+ },
47
+ };
48
+ ```
49
+
50
+ > **Important:** After installing or updating the plugin, clear the Jest cache:
51
+ > ```bash
52
+ > npx jest --clearCache
53
+ > ```
54
+
55
+ ### Options
56
+
57
+ | Option | Type | Default | Description |
58
+ |---|---|---|---|
59
+ | `unwrapTypeArrows` | `boolean` | `true` | Unwrap `type: () => String` → `type: String` in decorator option objects |
60
+ | `unwrapDecoratorArrows` | `boolean` | `true` | Unwrap `ResolveField(() => String)` → `ResolveField(String)` in decorator call arguments |
61
+ | `stripMetadata` | `boolean` | `false` | Remove `_ts_metadata("design:type", ...)` calls from `_ts_decorate` arrays |
62
+
63
+ > **Note:** `stripMetadata` defaults to `false` because `@nestjs/mongoose` depends on `design:type` metadata at runtime for schema type inference. Only enable it if your project does not use Mongoose (or any other library that reads `design:type` metadata).
64
+
65
+ ## How It Works
66
+
67
+ The plugin applies up to three transforms on `_ts_decorate([ ... ])` call sites:
68
+
69
+ ### 1. Unwrap decorator arrow arguments (`unwrapDecoratorArrows`)
70
+
71
+ ```js
72
+ // Before — Istanbul counts the arrow as a coverable function
73
+ (0, _graphql.Query)(() => Menu)
74
+
75
+ // After — no phantom function target
76
+ (0, _graphql.Query)(Menu)
77
+ ```
78
+
79
+ ### 2. Unwrap type property arrows (`unwrapTypeArrows`)
80
+
81
+ ```js
82
+ // Before
83
+ _ts_param(0, (0, _graphql.Args)('id', { type: () => String }))
84
+
85
+ // After
86
+ _ts_param(0, (0, _graphql.Args)('id', { type: String }))
87
+ ```
88
+
89
+ ### 3. Strip metadata calls (`stripMetadata`)
90
+
91
+ ```js
92
+ // Before
93
+ _ts_decorate([
94
+ (0, _graphql.Query)(),
95
+ _ts_metadata("design:type", Function),
96
+ _ts_metadata("design:paramtypes", [Object]),
97
+ _ts_metadata("design:returntype", Promise)
98
+ ], Resolver.prototype, "method", null);
99
+
100
+ // After
101
+ _ts_decorate([
102
+ (0, _graphql.Query)()
103
+ ], Resolver.prototype, "method", null);
104
+ ```
105
+
106
+ Only "simple" arrows are unwrapped — the body must be an identifier (`String`), member expression (`SomeModule.Type`), or array expression (`[String]`). Complex arrows with block bodies are left untouched to avoid changing runtime behavior.
107
+
108
+ ## Compatibility
109
+
110
+ | Dependency | Version |
111
+ |---|---|
112
+ | `@swc/core` | `>= 1.15.0` (swc_core v50 plugin ABI) |
113
+ | Node.js | `>= 18` |
114
+ | Jest | `>= 29` with `@swc/jest` |
115
+
116
+ ### `swc_core` version compatibility
117
+
118
+ | Plugin version | `swc_core` | `@swc/core` |
119
+ |---|---|---|
120
+ | `0.1.x` | `50` | `>= 1.15.0` |
121
+
122
+ ## Development
123
+
124
+ ```bash
125
+ # Run tests
126
+ cargo test
127
+
128
+ # Build WASM (debug)
129
+ cargo build --target wasm32-wasip1
130
+
131
+ # Build WASM (release)
132
+ bash build.sh
133
+ ```
134
+
135
+ ### Adding test fixtures
136
+
137
+ Each fixture is a directory under `tests/fixture/` containing:
138
+
139
+ - `input.js` — the SWC-transpiled code to transform
140
+ - `output.js` — the expected output after transformation
141
+ - `config.json` (optional) — plugin options to use instead of defaults
142
+
143
+ ## License
144
+
145
+ [MIT](LICENSE)
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@liively/swc-jest-coverage-nestjs-plugin",
3
+ "version": "0.1.0",
4
+ "description": "SWC plugin to transform NestJS decorator metadata for accurate Jest coverage",
5
+ "main": "swc_jest_coverage_nestjs_plugin.wasm",
6
+ "files": [
7
+ "swc_jest_coverage_nestjs_plugin.wasm"
8
+ ],
9
+ "scripts": {
10
+ "build": "bash build.sh",
11
+ "build:dev": "cargo build --target wasm32-wasip1 && cp target/wasm32-wasip1/debug/swc_jest_coverage_nestjs_plugin.wasm .",
12
+ "build:release": "bash build.sh",
13
+ "prepublishOnly": "bash build.sh"
14
+ },
15
+ "keywords": [
16
+ "swc",
17
+ "plugin",
18
+ "nestjs",
19
+ "jest",
20
+ "coverage",
21
+ "decorators"
22
+ ],
23
+ "license": "MIT",
24
+ "directories": {
25
+ "test": "tests"
26
+ },
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "git+https://github.com/LIIVE-LY/swc-jest-coverage-nestjs-plugin.git"
30
+ },
31
+ "author": "Liively",
32
+ "bugs": {
33
+ "url": "https://github.com/LIIVE-LY/swc-jest-coverage-nestjs-plugin/issues"
34
+ },
35
+ "homepage": "https://github.com/LIIVE-LY/swc-jest-coverage-nestjs-plugin#readme"
36
+ }