@muirjs/proteus-msw 0.0.0-stage → 0.0.1
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 +21 -0
- package/README.md +177 -2
- package/dist/index.d.ts +7 -0
- package/dist/index.js +10 -0
- package/dist/install.d.ts +5 -0
- package/dist/install.js +27 -0
- package/dist/msw-response.d.ts +4 -0
- package/dist/msw-response.js +138 -0
- package/dist/mutator.d.ts +4 -0
- package/dist/mutator.js +12 -0
- package/dist/mutators.d.ts +12 -0
- package/dist/mutators.js +11 -0
- package/dist/package-name.d.ts +1 -0
- package/dist/package-name.js +1 -0
- package/package.json +89 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Lars Gyrup Brink Nielsen
|
|
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
CHANGED
|
@@ -1,3 +1,178 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/muirjs/muir/main/docs/images/muirjs-badge-small.png" alt="Muir logo" width="200">
|
|
3
|
+
</p>
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
# @muirjs/proteus-msw
|
|
6
|
+
|
|
7
|
+
StrykerJS mutators for the responses that msw request handlers send. A handler is test code: it plays the server
|
|
8
|
+
for the code under test. `MuirProteusMswResponse` empties the body of a response that a handler builds, or turns the
|
|
9
|
+
response into a network error. When the tests still pass, no test outcome depends on that response. The mutators
|
|
10
|
+
work with any test runner and are not tied to Storybook.
|
|
11
|
+
|
|
12
|
+
The package keeps Stryker's 17 built-in mutators on and adds 1 of its own: 1 runs by default, 0 are opt-in and 0 are
|
|
13
|
+
experimental.
|
|
14
|
+
|
|
15
|
+
## Requirements
|
|
16
|
+
|
|
17
|
+
- `@stryker-mutator/core` 10.1, tested on 10.1.1. The install patches internals of that version's instrumenter, and
|
|
18
|
+
warns when `@stryker-mutator/instrumenter` is a version other than 10.1.1.
|
|
19
|
+
- `@babel/core` 8.0, the Babel of Stryker 10, as a peer dependency (`~8.0.0`): the package's types name Babel's types.
|
|
20
|
+
- Handlers written for msw 2 or msw 3 with its `http`, `graphql` and `HttpResponse` exports, imported from `msw`,
|
|
21
|
+
`msw/http` or `msw/graphql`. The mutator reads the handler files as source and imports nothing from msw. It states the range as an optional
|
|
22
|
+
`msw` peer dependency (`^2.0.0 || ^3.0.0`), so an install with another msw version fails.
|
|
23
|
+
- Node.js 22.18 or a later 22.x, 24.11 or a later 24.x, or 26.13 or a later 26.x.
|
|
24
|
+
|
|
25
|
+
## Turn the mutators on
|
|
26
|
+
|
|
27
|
+
List the files that hold your handlers in the `mutate` of a Stryker configuration for test code. Call
|
|
28
|
+
`muirProteusMswMutators` at the top of the Stryker configuration file and await it:
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
// stryker.test-code.config.mjs
|
|
32
|
+
import { muirProteusMswMutators } from '@muirjs/proteus-msw';
|
|
33
|
+
|
|
34
|
+
await muirProteusMswMutators();
|
|
35
|
+
|
|
36
|
+
export default {
|
|
37
|
+
testRunner: 'vitest',
|
|
38
|
+
mutate: ['src/mocks/**/*.ts'],
|
|
39
|
+
reporters: ['clear-text', 'html'],
|
|
40
|
+
};
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Await the call, so that an error in the install stops the run. The configuration file must be a JavaScript or
|
|
44
|
+
TypeScript ES module, because a JSON configuration cannot import a module.
|
|
45
|
+
|
|
46
|
+
The call prints one line:
|
|
47
|
+
|
|
48
|
+
```text
|
|
49
|
+
@muirjs/proteus-msw: 1 MuirProteusMsw mutators on, beside Stryker's built-in mutators.
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
The call checks that each mutator it installs makes a mutant of a sample, and stops the run when one does not. It
|
|
53
|
+
names the instrumenter's folder and version when an internal it patches is missing. A second call replaces the
|
|
54
|
+
mutators of the first.
|
|
55
|
+
|
|
56
|
+
The package has no opt-in or experimental mutators, so the call takes no options. It throws on any argument other
|
|
57
|
+
than `undefined` or an object whose every value is `undefined`, such as `{}`:
|
|
58
|
+
|
|
59
|
+
```text
|
|
60
|
+
@muirjs/proteus-msw: the package has no opt-in or experimental mutators yet, so muirProteusMswMutators() takes no options. Turn a mutator off with mutator.excludedMutations.
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Handlers next to Storybook or Playwright test code
|
|
64
|
+
|
|
65
|
+
`muirProteusStorybookTestMutators()` and `muirProteusPlaywrightMutators()` turn Stryker's built-in mutators off for
|
|
66
|
+
every file in the run. In a configuration that installs either of them, the handlers get the `MuirProteusMswResponse`
|
|
67
|
+
mutants and no built-in mutants. Mutate the handlers in a configuration of their own to keep the built-ins.
|
|
68
|
+
|
|
69
|
+
Handlers in a story's `parameters.msw` are story test code. With `@muirjs/shadowcat-storybook`, the CSF ignorer
|
|
70
|
+
reports their mutants as Ignored unless its play mode is `mutate`, so mutate them in the Storybook test-code
|
|
71
|
+
configuration.
|
|
72
|
+
The ignorer hides only the value of `parameters.msw`. A handler that the story file declares elsewhere and
|
|
73
|
+
`parameters.msw` names still gets mutants in the components configuration.
|
|
74
|
+
|
|
75
|
+
## The mutators
|
|
76
|
+
|
|
77
|
+
Each mutator's name is the mutator name in Stryker's reports.
|
|
78
|
+
|
|
79
|
+
### Default mutators
|
|
80
|
+
|
|
81
|
+
These run on `await muirProteusMswMutators()`.
|
|
82
|
+
|
|
83
|
+
| Mutator | What it changes | Example |
|
|
84
|
+
| ------------------------ | -------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------ |
|
|
85
|
+
| `MuirProteusMswResponse` | In a handler callback, empties the body of a response and, in a second mutant, replaces the response with a network error. | `HttpResponse.json({ items: [1] })` becomes `HttpResponse.json({})` in one mutant and `HttpResponse.error()` in another. |
|
|
86
|
+
|
|
87
|
+
### Opt-in mutators
|
|
88
|
+
|
|
89
|
+
None.
|
|
90
|
+
|
|
91
|
+
### Experimental mutators
|
|
92
|
+
|
|
93
|
+
None.
|
|
94
|
+
|
|
95
|
+
## What MuirProteusMswResponse mutates
|
|
96
|
+
|
|
97
|
+
A handler callback is a function written as an argument of `http.<method>(...)` (`all`, `get`, `post`, `put`,
|
|
98
|
+
`patch`, `delete`, `head` or `options`) or `graphql.<operation>(...)` (`query`, `mutation` or `operation`), where
|
|
99
|
+
`http` or `graphql` is imported from `msw`. A response inside it is one of these:
|
|
100
|
+
|
|
101
|
+
- `HttpResponse.json(...)`, `text`, `xml`, `html`, `arrayBuffer` or `formData`, with `HttpResponse` imported from
|
|
102
|
+
`msw`.
|
|
103
|
+
- One of those six factories through a name destructured from `HttpResponse`, as in
|
|
104
|
+
`const { json } = HttpResponse`.
|
|
105
|
+
- `new HttpResponse(...)`.
|
|
106
|
+
- `new Response(...)` of the global `Response`.
|
|
107
|
+
|
|
108
|
+
Each response gets two mutants:
|
|
109
|
+
|
|
110
|
+
- The body mutant empties the first argument by its kind: `{}`, `[]`, `''`, or else `null`. It keeps the second
|
|
111
|
+
argument, such as `{ status: 201 }`. A response with no body, or an empty one, gets no body mutant.
|
|
112
|
+
- The outcome mutant replaces the response with `HttpResponse.error()`, or with `Response.error()` for a
|
|
113
|
+
`new Response(...)`, so the request fails as on a network error.
|
|
114
|
+
|
|
115
|
+
`passthrough()` and `bypass()` build no response and get no mutant.
|
|
116
|
+
|
|
117
|
+
## Stryker's built-in mutators stay on
|
|
118
|
+
|
|
119
|
+
A handler behaves like app code for the code under test: it decides what the server answers. An analysis of 130
|
|
120
|
+
built-in mutants sampled from one open-source repository's msw handlers and other test doubles turned no built-in
|
|
121
|
+
off. It found real test gaps with nine of them; the other eight had too few outcomes for a verdict or placed no
|
|
122
|
+
mutant, and stay on, because no outcome showed them adding noise that a disable comment cannot remove
|
|
123
|
+
([ADR 0001](docs/adr/0001-built-in-mutators-stay-on-in-handler-code.md)). `MuirProteusMswResponse` adds two changes
|
|
124
|
+
that no built-in makes: a network error, and a body emptied by its kind instead of every object literal emptied.
|
|
125
|
+
|
|
126
|
+
## Turn a mutator off
|
|
127
|
+
|
|
128
|
+
For the whole run, list the mutator in `mutator.excludedMutations`. The names are case sensitive, and Stryker does
|
|
129
|
+
not check them, so a misspelt name excludes nothing and logs nothing. Stryker still reports an excluded mutant, as
|
|
130
|
+
Ignored.
|
|
131
|
+
|
|
132
|
+
For one mutant, put a Stryker disable comment with a reason on the line before the response:
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
http.get('https://api.pwnedpasswords.com/range/:prefix', () =>
|
|
136
|
+
// Stryker disable next-line MuirProteusMswResponse: the app treats a failed breach check as no breach
|
|
137
|
+
HttpResponse.text(''),
|
|
138
|
+
);
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
For a block, use `// Stryker disable <name>: <reason>` and `// Stryker restore <name>`.
|
|
142
|
+
|
|
143
|
+
## Survivors to expect and limits
|
|
144
|
+
|
|
145
|
+
On two open-source repositories, 7 of the 10 `MuirProteusMswResponse` survivors pointed at a test that does not check
|
|
146
|
+
what the response changes, such as a test that also passes before any response arrives, or one that cannot tell an error
|
|
147
|
+
status from a network error. Read a survivor as a missing check first. These survive without one:
|
|
148
|
+
|
|
149
|
+
- An outcome mutant survives when the app catches the failed request and the page looks the same. Such a mutant is
|
|
150
|
+
equivalent, not a test gap. Disable it with a comment that says so.
|
|
151
|
+
- A body mutant survives on a response whose body the code does not read, such as an error response the code reads
|
|
152
|
+
only the status of.
|
|
153
|
+
- Both mutants survive in a handler that exists only to keep an unhandled request from failing the test, when
|
|
154
|
+
nothing reads its response.
|
|
155
|
+
|
|
156
|
+
Built-in mutants in a handler's module that survive without a missing check sit mostly in three kinds of code:
|
|
157
|
+
|
|
158
|
+
- A condition that reads `process.env`, directly or through a module-level constant set from it, such as a switch
|
|
159
|
+
that lets requests pass through outside tests. Disable the mutant with a comment that says so.
|
|
160
|
+
- Logging: `console.*` calls and msw's `print.*` in `onUnhandledRequest`. Disable these mutants too.
|
|
161
|
+
- Functions in the handler's module that the test process or a seed script also imports, such as one that writes a
|
|
162
|
+
user to a fixture file. Their mutants can also be killed or not depending on the order the mutants run in. Keep
|
|
163
|
+
these functions out of `mutate`, with a line range or a module of their own.
|
|
164
|
+
|
|
165
|
+
What the mutator does not see:
|
|
166
|
+
|
|
167
|
+
- A handler callback declared elsewhere and passed by name, such as `http.get('/a', getUser)`, gets no mutant.
|
|
168
|
+
- A response built outside the handler callback, such as in a function the callback calls, gets no mutant.
|
|
169
|
+
- An `http` or `graphql` that is not imported from `msw` makes no handler.
|
|
170
|
+
|
|
171
|
+
A Storybook project with msw can fail Stryker's dry run in a way that has nothing to do with this package. See the
|
|
172
|
+
`import.meta` crash under
|
|
173
|
+
[Survivors to expect and limits](../shadowcat-storybook/README.md#survivors-to-expect-and-limits) in the
|
|
174
|
+
`@muirjs/shadowcat-storybook` README.
|
|
175
|
+
|
|
176
|
+
## License
|
|
177
|
+
|
|
178
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type MuirProteusMswMutatorOptions } from './mutators.ts';
|
|
2
|
+
export type { MuirProteusMswMutatorOptions } from './mutators.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Installs this package's mutators for the responses of msw request handlers beside Stryker's built-in mutators. A
|
|
5
|
+
* Stryker configuration file awaits it, so that an error in the install stops the run.
|
|
6
|
+
*/
|
|
7
|
+
export declare function muirProteusMswMutators(options?: MuirProteusMswMutatorOptions): Promise<void>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { muirMoiraMutatorsResolveEachCore } from '@muirjs/moira-mutators';
|
|
2
|
+
import { muirProteusMswInstall, muirProteusMswResolveFromCore, } from "./install.js";
|
|
3
|
+
import { muirProteusMswMutatorSets, } from "./mutators.js";
|
|
4
|
+
/**
|
|
5
|
+
* Installs this package's mutators for the responses of msw request handlers beside Stryker's built-in mutators. A
|
|
6
|
+
* Stryker configuration file awaits it, so that an error in the install stops the run.
|
|
7
|
+
*/
|
|
8
|
+
export async function muirProteusMswMutators(options) {
|
|
9
|
+
await muirProteusMswInstall(muirProteusMswMutatorSets, options, () => muirMoiraMutatorsResolveEachCore(muirProteusMswResolveFromCore));
|
|
10
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type MuirMoiraMutatorsBabelInstrumenter, type MuirMoiraMutatorsMutatorSets } from '@muirjs/moira-mutators';
|
|
2
|
+
import { type MuirProteusMswBabelTypes, type MuirProteusMswMutator } from './mutator.ts';
|
|
3
|
+
export type MuirProteusMswInstrumenter = MuirMoiraMutatorsBabelInstrumenter<MuirProteusMswBabelTypes>;
|
|
4
|
+
export declare function muirProteusMswResolveFromCore(corePackage?: string): Promise<MuirProteusMswInstrumenter>;
|
|
5
|
+
export declare function muirProteusMswInstall(sets: MuirMoiraMutatorsMutatorSets<MuirProteusMswMutator>, options: unknown, resolve: () => Promise<readonly MuirProteusMswInstrumenter[]>, warn?: (message: string) => void, info?: (message: string) => void): Promise<void>;
|
package/dist/install.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { muirMoiraMutatorsBabelModel, } from '@muirjs/moira-mutators';
|
|
2
|
+
import { muirProteusMswBabelTypeMembers, } from "./mutator.js";
|
|
3
|
+
import { muirProteusMswPackageName } from "./package-name.js";
|
|
4
|
+
const babel = muirMoiraMutatorsBabelModel(muirProteusMswBabelTypeMembers);
|
|
5
|
+
export function muirProteusMswResolveFromCore(corePackage) {
|
|
6
|
+
return babel.resolveInstrumenter(muirProteusMswPackageName, corePackage);
|
|
7
|
+
}
|
|
8
|
+
export async function muirProteusMswInstall(sets, options, resolve, warn = console.warn, info = console.info) {
|
|
9
|
+
const mutators = await babel.install({
|
|
10
|
+
packageName: muirProteusMswPackageName,
|
|
11
|
+
installName: 'muirProteusMswMutators',
|
|
12
|
+
sets,
|
|
13
|
+
options,
|
|
14
|
+
}, resolve, warn);
|
|
15
|
+
info(startLine(sets, mutators));
|
|
16
|
+
}
|
|
17
|
+
function startLine(sets, mutators) {
|
|
18
|
+
const namesOf = (listed) => listed.map(({ name }) => name).join(', ');
|
|
19
|
+
const optional = mutators.filter((mutator) => !sets.defaults.includes(mutator));
|
|
20
|
+
const line = `${muirProteusMswPackageName}: ${mutators.length} MuirProteusMsw mutators on, beside Stryker's built-in mutators.`;
|
|
21
|
+
if (mutators.length === 0) {
|
|
22
|
+
return `${line} Off as experimental: ${namesOf(sets.experimental)}; muirProteusMswMutators({ experimental: true }) turns them on.`;
|
|
23
|
+
}
|
|
24
|
+
return optional.length > 0
|
|
25
|
+
? `${line} Experimental: ${namesOf(optional)}.`
|
|
26
|
+
: line;
|
|
27
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/** The static methods of msw's `HttpResponse` that build a response with a body. */
|
|
2
|
+
const factories = [
|
|
3
|
+
'json',
|
|
4
|
+
'text',
|
|
5
|
+
'xml',
|
|
6
|
+
'html',
|
|
7
|
+
'arrayBuffer',
|
|
8
|
+
'formData',
|
|
9
|
+
];
|
|
10
|
+
/** msw's namespaces of request handlers, each with the methods that take a handler callback. */
|
|
11
|
+
const handlerMethods = [
|
|
12
|
+
['http', ['all', 'get', 'post', 'put', 'patch', 'delete', 'head', 'options']],
|
|
13
|
+
['graphql', ['query', 'mutation', 'operation']],
|
|
14
|
+
];
|
|
15
|
+
/**
|
|
16
|
+
* The msw entry points that export `http`, `graphql` or `HttpResponse`. msw 3 added `msw/http` and `msw/graphql`,
|
|
17
|
+
* and exports `graphql` from `msw/graphql` only.
|
|
18
|
+
*/
|
|
19
|
+
const mswEntries = ['msw', 'msw/http', 'msw/graphql'];
|
|
20
|
+
export function muirProteusMswResponseMutator() {
|
|
21
|
+
return {
|
|
22
|
+
name: 'MuirProteusMswResponse',
|
|
23
|
+
sample: "import { http, HttpResponse } from 'msw';\n" +
|
|
24
|
+
"export const handlers = [http.get('/a', () => HttpResponse.json({ a: 1 }))];\n",
|
|
25
|
+
*mutate(path, t) {
|
|
26
|
+
if (!path.isCallExpression() && !path.isNewExpression()) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const owner = errorOwnerOf(path);
|
|
30
|
+
if (owner === undefined || !isInHandler(path)) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const [body] = path.node.arguments;
|
|
34
|
+
const empty = body === undefined ? undefined : emptyOf(body, t);
|
|
35
|
+
if (empty !== undefined) {
|
|
36
|
+
const emptied = t.cloneNode(path.node);
|
|
37
|
+
emptied.arguments = [empty, ...emptied.arguments.slice(1)];
|
|
38
|
+
yield emptied;
|
|
39
|
+
}
|
|
40
|
+
yield t.callExpression(t.memberExpression(t.identifier(owner), t.identifier('error')), []);
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/** The local name of the class whose `error()` replaces the response `call` builds, when it builds one. */
|
|
45
|
+
function errorOwnerOf(call) {
|
|
46
|
+
const callee = call.get('callee');
|
|
47
|
+
if (call.isNewExpression()) {
|
|
48
|
+
return callee.isIdentifier() &&
|
|
49
|
+
(isMswImport(callee, 'HttpResponse') || isGlobalResponse(callee))
|
|
50
|
+
? callee.node.name
|
|
51
|
+
: undefined;
|
|
52
|
+
}
|
|
53
|
+
if (callee.isIdentifier()) {
|
|
54
|
+
return destructuredOwnerOf(callee);
|
|
55
|
+
}
|
|
56
|
+
if (!callee.isMemberExpression({ computed: false })) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
const object = callee.get('object');
|
|
60
|
+
return object.isIdentifier() &&
|
|
61
|
+
isMswImport(object, 'HttpResponse') &&
|
|
62
|
+
factories.some((name) => callee.get('property').isIdentifier({ name }))
|
|
63
|
+
? object.node.name
|
|
64
|
+
: undefined;
|
|
65
|
+
}
|
|
66
|
+
/** `Response` with no binding in the file, so the Fetch API's. */
|
|
67
|
+
function isGlobalResponse(name) {
|
|
68
|
+
return (name.node.name === 'Response' &&
|
|
69
|
+
name.scope.getBinding('Response') === undefined);
|
|
70
|
+
}
|
|
71
|
+
/** The local name of msw's `HttpResponse` when `factory` is bound by `const { <factory> } = HttpResponse`. */
|
|
72
|
+
function destructuredOwnerOf(factory) {
|
|
73
|
+
const declarator = factory.scope.getBinding(factory.node.name)?.path;
|
|
74
|
+
if (declarator?.isVariableDeclarator() !== true) {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
const pattern = declarator.get('id');
|
|
78
|
+
const init = declarator.get('init');
|
|
79
|
+
return pattern.isObjectPattern() &&
|
|
80
|
+
init.isIdentifier() &&
|
|
81
|
+
isMswImport(init, 'HttpResponse') &&
|
|
82
|
+
pattern.node.properties.some((property) => property.type === 'ObjectProperty' &&
|
|
83
|
+
!property.computed &&
|
|
84
|
+
property.key.type === 'Identifier' &&
|
|
85
|
+
factories.includes(property.key.name) &&
|
|
86
|
+
property.value.type === 'Identifier' &&
|
|
87
|
+
property.value.name === factory.node.name)
|
|
88
|
+
? init.node.name
|
|
89
|
+
: undefined;
|
|
90
|
+
}
|
|
91
|
+
/** `path` sits in a function, at any depth, that is an argument of `http.<method>(...)` or `graphql.<operation>(...)`. */
|
|
92
|
+
function isInHandler(path) {
|
|
93
|
+
return (path.findParent((parent) => parent.isFunction() && isHandlerCallback(parent)) !== null);
|
|
94
|
+
}
|
|
95
|
+
function isHandlerCallback(fn) {
|
|
96
|
+
const call = fn.parentPath;
|
|
97
|
+
if (!call.isCallExpression()) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
const callee = call.get('callee');
|
|
101
|
+
return (callee.isMemberExpression({ computed: false }) &&
|
|
102
|
+
handlerMethods.some(([namespace, methods]) => isMswImport(callee.get('object'), namespace) &&
|
|
103
|
+
methods.some((name) => callee.get('property').isIdentifier({ name }))));
|
|
104
|
+
}
|
|
105
|
+
/** `path` is an identifier bound by `import { <exported> }` from one of `mswEntries`, under any local name. */
|
|
106
|
+
function isMswImport(path, exported) {
|
|
107
|
+
const binding = path.isIdentifier()
|
|
108
|
+
? path.scope.getBinding(path.node.name)
|
|
109
|
+
: undefined;
|
|
110
|
+
const specifier = binding?.path;
|
|
111
|
+
const declaration = specifier?.parentPath;
|
|
112
|
+
return (specifier?.isImportSpecifier() === true &&
|
|
113
|
+
importedName(specifier.node.imported) === exported &&
|
|
114
|
+
// Stryker disable next-line OptionalChaining: declaration is read only once specifier is an ImportSpecifier, whose parent path is always its ImportDeclaration, so it is never null here
|
|
115
|
+
declaration?.isImportDeclaration() === true &&
|
|
116
|
+
mswEntries.includes(declaration.node.source.value));
|
|
117
|
+
}
|
|
118
|
+
function importedName(imported) {
|
|
119
|
+
return imported.type === 'Identifier' ? imported.name : imported.value;
|
|
120
|
+
}
|
|
121
|
+
/** The empty body of the same kind as `body`: `{}`, `[]` or `''`, else `null`; undefined when `body` is empty already. */
|
|
122
|
+
function emptyOf(body, t) {
|
|
123
|
+
if (body.type === 'ObjectExpression') {
|
|
124
|
+
return body.properties.length === 0 ? undefined : t.objectExpression([]);
|
|
125
|
+
}
|
|
126
|
+
if (body.type === 'ArrayExpression') {
|
|
127
|
+
return body.elements.length === 0 ? undefined : t.arrayExpression([]);
|
|
128
|
+
}
|
|
129
|
+
if (body.type === 'StringLiteral') {
|
|
130
|
+
return body.value === '' ? undefined : t.stringLiteral('');
|
|
131
|
+
}
|
|
132
|
+
if (body.type === 'TemplateLiteral') {
|
|
133
|
+
return body.expressions.length === 0 && body.quasis[0]?.value.cooked === ''
|
|
134
|
+
? undefined
|
|
135
|
+
: t.stringLiteral('');
|
|
136
|
+
}
|
|
137
|
+
return body.type === 'NullLiteral' ? undefined : t.nullLiteral();
|
|
138
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { MuirMoiraMutatorsBabelMutator, MuirMoiraMutatorsBabelTypesOf } from '@muirjs/moira-mutators';
|
|
2
|
+
export declare const muirProteusMswBabelTypeMembers: readonly ("arrayExpression" | "callExpression" | "cloneNode" | "identifier" | "memberExpression" | "nullLiteral" | "objectExpression" | "stringLiteral")[];
|
|
3
|
+
export type MuirProteusMswBabelTypes = MuirMoiraMutatorsBabelTypesOf<typeof muirProteusMswBabelTypeMembers>;
|
|
4
|
+
export type MuirProteusMswMutator = MuirMoiraMutatorsBabelMutator<string, MuirProteusMswBabelTypes>;
|
package/dist/mutator.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Each key names its own member, so the compiler rejects a call to a member the table lacks.
|
|
2
|
+
const babelTypeMemberTable = Object.freeze({
|
|
3
|
+
arrayExpression: 'arrayExpression',
|
|
4
|
+
callExpression: 'callExpression',
|
|
5
|
+
cloneNode: 'cloneNode',
|
|
6
|
+
identifier: 'identifier',
|
|
7
|
+
memberExpression: 'memberExpression',
|
|
8
|
+
nullLiteral: 'nullLiteral',
|
|
9
|
+
objectExpression: 'objectExpression',
|
|
10
|
+
stringLiteral: 'stringLiteral',
|
|
11
|
+
});
|
|
12
|
+
export const muirProteusMswBabelTypeMembers = Object.freeze(Object.values(babelTypeMemberTable));
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { MuirMoiraMutatorsMutatorOptions, MuirMoiraMutatorsSampledMutator } from '@muirjs/moira-mutators';
|
|
2
|
+
import type { MuirProteusMswMutator } from './mutator.ts';
|
|
3
|
+
export declare const muirProteusMswMutatorSets: {
|
|
4
|
+
readonly defaults: readonly [MuirProteusMswMutator & {
|
|
5
|
+
readonly name: "MuirProteusMswResponse";
|
|
6
|
+
}];
|
|
7
|
+
readonly optIn: readonly [];
|
|
8
|
+
readonly experimental: readonly [];
|
|
9
|
+
};
|
|
10
|
+
type NamesIn<TTier extends readonly MuirMoiraMutatorsSampledMutator[]> = TTier[number]['name'];
|
|
11
|
+
export type MuirProteusMswMutatorOptions = MuirMoiraMutatorsMutatorOptions<NamesIn<(typeof muirProteusMswMutatorSets)['optIn']>, NamesIn<(typeof muirProteusMswMutatorSets)['experimental']>>;
|
|
12
|
+
export {};
|
package/dist/mutators.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { muirProteusMswResponseMutator } from "./msw-response.js";
|
|
2
|
+
// In place of `as const`: Stryker mutates nothing inside an `as` expression, which would leave the roster out of a
|
|
3
|
+
// mutation run. The literal names are what the option type reads.
|
|
4
|
+
function roster(sets) {
|
|
5
|
+
return sets;
|
|
6
|
+
}
|
|
7
|
+
export const muirProteusMswMutatorSets = roster({
|
|
8
|
+
defaults: [muirProteusMswResponseMutator()],
|
|
9
|
+
optIn: [],
|
|
10
|
+
experimental: [],
|
|
11
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const muirProteusMswPackageName = "@muirjs/proteus-msw";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const muirProteusMswPackageName = '@muirjs/proteus-msw';
|
package/package.json
CHANGED
|
@@ -1,6 +1,91 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@muirjs/proteus-msw",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "StrykerJS mutators for the responses of msw request handlers",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"stryker",
|
|
7
|
+
"msw",
|
|
8
|
+
"mutation testing",
|
|
9
|
+
"mutators"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/muirjs/muir/tree/main/packages/proteus-msw#readme",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/muirjs/muir.git",
|
|
15
|
+
"directory": "packages/proteus-msw"
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"type": "module",
|
|
22
|
+
"main": "./dist/index.js",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"@muirjs/source": "./src/index.ts",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"default": "./dist/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./package.json": "./package.json"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"!dist/**/*.tsbuildinfo"
|
|
35
|
+
],
|
|
36
|
+
"sideEffects": false,
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": "^22.18.0 || ^24.11.0 || ^26.13.0"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"@babel/core": "~8.0.0",
|
|
42
|
+
"@stryker-mutator/core": "~10.1.0",
|
|
43
|
+
"msw": "^2.0.0 || ^3.0.0"
|
|
44
|
+
},
|
|
45
|
+
"peerDependenciesMeta": {
|
|
46
|
+
"msw": {
|
|
47
|
+
"optional": true
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"@muirjs/moira-mutators": "0.0.1"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@babel/core": "8.0.9",
|
|
55
|
+
"@muirjs/madrox-node-test": "0.0.1",
|
|
56
|
+
"@muirjs/test-util-mutator-packages": "0.0.0"
|
|
57
|
+
},
|
|
58
|
+
"nx": {
|
|
59
|
+
"tags": [
|
|
60
|
+
"type:lib"
|
|
61
|
+
],
|
|
62
|
+
"targets": {
|
|
63
|
+
"build": {
|
|
64
|
+
"dependsOn": [
|
|
65
|
+
"^build"
|
|
66
|
+
]
|
|
67
|
+
},
|
|
68
|
+
"typecheck": {
|
|
69
|
+
"dependsOn": [
|
|
70
|
+
"build",
|
|
71
|
+
"^typecheck"
|
|
72
|
+
]
|
|
73
|
+
},
|
|
74
|
+
"test": {
|
|
75
|
+
"dependsOn": [
|
|
76
|
+
"^build"
|
|
77
|
+
]
|
|
78
|
+
},
|
|
79
|
+
"lint": {
|
|
80
|
+
"inputs": [
|
|
81
|
+
"...",
|
|
82
|
+
"{workspaceRoot}/tsconfig.base.json"
|
|
83
|
+
]
|
|
84
|
+
},
|
|
85
|
+
"mutate-src": {}
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
"scripts": {
|
|
89
|
+
"test": "node --conditions=@muirjs/source --test test/*.test.ts"
|
|
90
|
+
}
|
|
91
|
+
}
|