Skip to content

Commit 4a11e94

Browse files
authored
test(vitest): add a test for mocking a module with vitest (#5146)
1 parent 3354719 commit 4a11e94

File tree

15 files changed

+819
-147
lines changed

15 files changed

+819
-147
lines changed

package-lock.json

Lines changed: 483 additions & 146 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
],
88
"devDependencies": {
99
"@eslint/js": "9.13.0",
10+
"@testing-library/jest-dom": "^6.6.3",
11+
"@testing-library/react": "^16.1.0",
1012
"@types/chai-as-promised": "8.0.1",
1113
"@types/chai-jest-snapshot": "1.3.8",
1214
"@types/eslint__js": "8.42.3",
@@ -17,6 +19,7 @@
1719
"@types/sinon": "17.0.3",
1820
"@types/sinon-chai": "4.0.0",
1921
"@types/source-map-support": "0.5.10",
22+
"@vitejs/plugin-react": "^4.3.4",
2023
"c8": "10.1.2",
2124
"chai": "5.1.2",
2225
"chai-as-promised": "8.0.1",

packages/vitest-runner/test/integration/vitest-test-runner.it.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,4 +377,32 @@ describe('VitestRunner integration', () => {
377377
]);
378378
});
379379
});
380+
381+
describe('using vi.mock to mock a module', () => {
382+
beforeEach(async () => {
383+
sandbox = new TempTestDirectorySandbox('vi-mock');
384+
await sandbox.init();
385+
});
386+
387+
it('should be able to mock a module', async () => {
388+
await sut.init();
389+
const runResult = await sut.dryRun();
390+
assertions.expectCompleted(runResult);
391+
expect(runResult.tests).lengthOf(3);
392+
assertions.expectTestResults(runResult, [
393+
{
394+
id: 'components/Counter.test.tsx#Counter increments',
395+
status: TestStatus.Success,
396+
},
397+
{
398+
id: "components/Product.test.tsx#Product doesn't render the discount when 0",
399+
status: TestStatus.Success,
400+
},
401+
{
402+
id: 'components/Product.test.tsx#Product should render the discount',
403+
status: TestStatus.Success,
404+
},
405+
]);
406+
});
407+
});
380408
});

packages/vitest-runner/testResources/browser-project/src/math.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export class MathComponent extends HTMLElement {
151151
{}
152152
} else {
153153
stryCov_9fa48("43");
154-
this.#operator = (value as Operator);
154+
this.#operator = value as Operator;
155155
}
156156
} else {
157157
if (stryMutAct_9fa48("44")) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// src/components/Counter.tsx
2+
import { useState } from 'react';
3+
4+
export const Counter = () => {
5+
const [count, setCount] = useState(0);
6+
7+
return (
8+
<div>
9+
<p>
10+
Count:
11+
<span data-testId="counter">{count}</span>
12+
</p>
13+
<button onClick={() => setCount(count + 1)}>Increment</button>
14+
</div>
15+
);
16+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import '@testing-library/jest-dom';
2+
import { render, screen, fireEvent } from '@testing-library/react';
3+
import { Counter } from './Counter';
4+
5+
describe('Counter', () => {
6+
it('increments', () => {
7+
render(<Counter />);
8+
const incrementButton = screen.getByText('Increment');
9+
const counter = screen.getByTestId('counter');
10+
11+
fireEvent.click(incrementButton);
12+
expect(counter).toHaveTextContent('1');
13+
});
14+
});
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// This file is generated with tasks/instrument-test-resources.js
2+
// src/components/Counter.tsx
3+
function stryNS_9fa48() {
4+
var g = typeof globalThis === 'object' && globalThis && globalThis.Math === Math && globalThis || new Function("return this")();
5+
var ns = g.__stryker2__ || (g.__stryker2__ = {});
6+
if (ns.activeMutant === undefined && g.process && g.process.env && g.process.env.__STRYKER_ACTIVE_MUTANT__) {
7+
ns.activeMutant = g.process.env.__STRYKER_ACTIVE_MUTANT__;
8+
}
9+
function retrieveNS() {
10+
return ns;
11+
}
12+
stryNS_9fa48 = retrieveNS;
13+
return retrieveNS();
14+
}
15+
stryNS_9fa48();
16+
function stryCov_9fa48() {
17+
var ns = stryNS_9fa48();
18+
var cov = ns.mutantCoverage || (ns.mutantCoverage = {
19+
static: {},
20+
perTest: {}
21+
});
22+
function cover() {
23+
var c = cov.static;
24+
if (ns.currentTestId) {
25+
c = cov.perTest[ns.currentTestId] = cov.perTest[ns.currentTestId] || {};
26+
}
27+
var a = arguments;
28+
for (var i = 0; i < a.length; i++) {
29+
c[a[i]] = (c[a[i]] || 0) + 1;
30+
}
31+
}
32+
stryCov_9fa48 = cover;
33+
cover.apply(null, arguments);
34+
}
35+
function stryMutAct_9fa48(id) {
36+
var ns = stryNS_9fa48();
37+
function isActive(id) {
38+
if (ns.activeMutant === id) {
39+
if (ns.hitCount !== void 0 && ++ns.hitCount > ns.hitLimit) {
40+
throw new Error('Stryker: Hit count limit reached (' + ns.hitCount + ')');
41+
}
42+
return true;
43+
}
44+
return false;
45+
}
46+
stryMutAct_9fa48 = isActive;
47+
return isActive(id);
48+
}
49+
import { useState } from 'react';
50+
export const Counter = () => {
51+
if (stryMutAct_9fa48("0")) {
52+
{}
53+
} else {
54+
stryCov_9fa48("0");
55+
const [count, setCount] = useState(0);
56+
return <div>
57+
<p>
58+
Count:
59+
<span data-testId="counter">{count}</span>
60+
</p>
61+
<button onClick={stryMutAct_9fa48("1") ? () => undefined : (stryCov_9fa48("1"), () => setCount(stryMutAct_9fa48("2") ? count - 1 : (stryCov_9fa48("2"), count + 1)))}>Increment</button>
62+
</div>;
63+
}
64+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function Discount ({ discount }: { discount: number }) {
2+
return (
3+
<p>{discount}</p>
4+
);
5+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// This file is generated with tasks/instrument-test-resources.js
2+
function stryNS_9fa48() {
3+
var g = typeof globalThis === 'object' && globalThis && globalThis.Math === Math && globalThis || new Function("return this")();
4+
var ns = g.__stryker2__ || (g.__stryker2__ = {});
5+
if (ns.activeMutant === undefined && g.process && g.process.env && g.process.env.__STRYKER_ACTIVE_MUTANT__) {
6+
ns.activeMutant = g.process.env.__STRYKER_ACTIVE_MUTANT__;
7+
}
8+
function retrieveNS() {
9+
return ns;
10+
}
11+
stryNS_9fa48 = retrieveNS;
12+
return retrieveNS();
13+
}
14+
stryNS_9fa48();
15+
function stryCov_9fa48() {
16+
var ns = stryNS_9fa48();
17+
var cov = ns.mutantCoverage || (ns.mutantCoverage = {
18+
static: {},
19+
perTest: {}
20+
});
21+
function cover() {
22+
var c = cov.static;
23+
if (ns.currentTestId) {
24+
c = cov.perTest[ns.currentTestId] = cov.perTest[ns.currentTestId] || {};
25+
}
26+
var a = arguments;
27+
for (var i = 0; i < a.length; i++) {
28+
c[a[i]] = (c[a[i]] || 0) + 1;
29+
}
30+
}
31+
stryCov_9fa48 = cover;
32+
cover.apply(null, arguments);
33+
}
34+
function stryMutAct_9fa48(id) {
35+
var ns = stryNS_9fa48();
36+
function isActive(id) {
37+
if (ns.activeMutant === id) {
38+
if (ns.hitCount !== void 0 && ++ns.hitCount > ns.hitLimit) {
39+
throw new Error('Stryker: Hit count limit reached (' + ns.hitCount + ')');
40+
}
41+
return true;
42+
}
43+
return false;
44+
}
45+
stryMutAct_9fa48 = isActive;
46+
return isActive(id);
47+
}
48+
export function Discount({
49+
discount
50+
}: {
51+
discount: number;
52+
}) {
53+
if (stryMutAct_9fa48("7")) {
54+
{}
55+
} else {
56+
stryCov_9fa48("7");
57+
return <p>{discount}</p>;
58+
}
59+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Discount } from './Discount';
2+
3+
export function Product({
4+
name,
5+
price,
6+
discount,
7+
}: {
8+
name: string;
9+
price: number;
10+
discount: number;
11+
}) {
12+
return (
13+
<div>
14+
<h2>{name}</h2>
15+
<p>{price}</p>
16+
{discount && <Discount discount={discount} />}
17+
</div>
18+
);
19+
}

0 commit comments

Comments
 (0)