Skip to content

Commit 594726b

Browse files
committed
chore(detector): migrated to vitest
1 parent 6bebf4f commit 594726b

File tree

5 files changed

+184
-118
lines changed

5 files changed

+184
-118
lines changed

detector/package.json

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,21 @@
3333
"@types/sinon": "^17.0.3",
3434
"@typescript-eslint/eslint-plugin": "^5.53.0",
3535
"@typescript-eslint/parser": "^5.53.0",
36+
"@vitest/coverage-v8": "^2.1.6",
3637
"eslint": "^8.34.0",
37-
"mocha": "^10.2.0",
3838
"nodemon": "^3.1.4",
39-
"nyc": "^15.1.0",
40-
"sinon": "^19.0.2",
4139
"ts-loader": "*",
42-
"ts-mocha": "^10.0.0",
4340
"ts-node": "^10.9.2"
4441
},
4542
"scripts": {
46-
"compile": "rm -rf build/ && tsc && cp -R ./src/reporters/layout ./build/src/reporters/",
43+
"compile": "rm -rf build/ types && tsc && cp -R ./src/reporters/layout ./build/src/reporters/",
4744
"watch": "webpack --watch",
4845
"clean": "rm -rf out/ dist/ types/",
4946
"compile-tests": "npm run clean && tsc -p . --outDir out",
5047
"watch-tests": "tsc -p . -w --outDir out",
5148
"lint": "eslint . --ext ts",
52-
"test": "rm -rf smelly-report.html && ts-mocha -p tsconfig.json ./**/**.test.ts",
53-
"test:watch": "nodemon --exec 'npm run test' --watch 'src/**' --ext 'ts' --delay 5",
54-
"test:coverage": "nyc --reporter=lcov ts-mocha -p tsconfig.json ts-node/register ./**/**.test.ts",
49+
"test": "vitest --watch=false ./**/**.test.ts",
50+
"test:coverage": "rm -rf smelly-report.html coverage/ && npm run test -- --coverage",
5551
"test:mutation": "stryker run",
5652
"coveralls": "npm run test:coverage && coveralls --verbose < coverage/lcov.info"
5753
},
Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,28 @@
1-
import sinon from 'sinon';
21
import { ExportOptions, SmellsAggreagtor, SmellsList } from '../src/reporters/Html';
32
import { HtmlOutput } from '../src/reporters/Output';
43
import { ReadHtml } from '../src/reporters/Input';
4+
import { vi,test, describe, expect } from 'vitest';
55

6-
describe('report html', () => {
7-
let input: sinon.SinonStub;
8-
let output: sinon.SinonMock;
9-
10-
beforeEach(() => {
11-
input = sinon.stub(ReadHtml.prototype, 'readTeamplate');
12-
output = sinon.mock(HtmlOutput.prototype);
13-
});
6+
vi.mock('../src/reporters/Input');
7+
vi.mock('../src/reporters/Output');
148

15-
afterEach(() => {
16-
input.restore();
17-
output.restore();
18-
});
19-
20-
it('no smells for a single file', async () => {
9+
describe('report html', () => {
10+
test('no smells for a single file', async () => {
2111
const smellsFound: SmellsList[] = [];
2212
const exportsOptions: ExportOptions = { to: '.' };
2313

24-
input.resolves('fake data');
25-
output.expects('writeTo').once().withArgs('fake data', exportsOptions);
14+
ReadHtml.mockImplementation(() => {
15+
return {
16+
readTeamplate: () => Promise.resolve('fake data')
17+
};
18+
});
19+
20+
HtmlOutput.prototype.writeTo = vi.fn();
2621

2722
const reporter = new SmellsAggreagtor(smellsFound, exportsOptions);
2823

2924
await reporter.build();
3025

31-
sinon.verify();
26+
expect(HtmlOutput.prototype.writeTo).toHaveBeenCalledWith('fake data', exportsOptions);
3227
});
3328
});

detector/test/smells-detector.test.ts

Lines changed: 60 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import * as assert from 'assert';
2-
import { suite, test } from 'mocha';
1+
import { describe, expect, test } from 'vitest';
32
import { SmellDetector } from '../src/index';
43

54
const IF_STATEMENT = 'if-statement';
@@ -13,8 +12,8 @@ const MOCKERY = 'excessive-jest-mock';
1312
const JAVASCRIPT = 'javascript';
1413
const TYPESCRIPT = 'typescript';
1514

16-
suite('Smelly Test Smell Detection Suite', () => {
17-
[{
15+
describe('Smelly Test Smell Detection Suite', () => {
16+
test.each([[{
1817
code: `const a = 1;
1918
if (a === 1) {}`,
2019
language: JAVASCRIPT,
@@ -27,8 +26,8 @@ if (a === 1) {}`,
2726
total: 1,
2827
description: `Smelly: Avoid Conditional Test Logic in the test. Having conditional logic points to a test case that requires different context to run. Split the test case to fit one context per test case.`,
2928
diagnostic: `Smelly: Avoid Conditional Test Logic in the test. Having conditional logic points to a test case that requires different context to run. Split the test case to fit one context per test case.`,
30-
},
31-
{
29+
}],
30+
[{
3231
code: `const lists = [{}, {}];
3332
3433
for (const i of lists) {
@@ -44,8 +43,8 @@ for (const i of lists) {
4443
total: 1,
4544
description: `Smelly: Avoid Conditional Test Logic in the test. Having conditional logic points to a test case that requires different context to run. Split the test case to fit one context per test case.`,
4645
diagnostic: `Smelly: Avoid Conditional Test Logic in the test. Having conditional logic points to a test case that requires different context to run. Split the test case to fit one context per test case.`
47-
},
48-
{
46+
}],
47+
[{
4948
code: `const lists = [{}, {}];
5049
5150
for (const i in lists) {
@@ -61,8 +60,8 @@ for (const i in lists) {
6160
total: 1,
6261
description: `Smelly: Avoid Conditional Test Logic in the test. Having conditional logic points to a test case that requires different context to run. Split the test case to fit one context per test case.`,
6362
diagnostic: `Smelly: Avoid Conditional Test Logic in the test. Having conditional logic points to a test case that requires different context to run. Split the test case to fit one context per test case.`
64-
},
65-
{
63+
}],
64+
[{
6665
code: `const lists = [{}, {}];
6766
6867
for (let i = 0; i < 1; i++) {
@@ -78,8 +77,8 @@ for (let i = 0; i < 1; i++) {
7877
total: 1,
7978
description: `Smelly: Avoid Conditional Test Logic in the test. Having conditional logic points to a test case that requires different context to run. Split the test case to fit one context per test case.`,
8079
diagnostic: `Smelly: Avoid Conditional Test Logic in the test. Having conditional logic points to a test case that requires different context to run. Split the test case to fit one context per test case.`
81-
},
82-
{
80+
}],
81+
[{
8382
code: `setTimeout(() => {
8483
done();
8584
});`,
@@ -93,8 +92,8 @@ for (let i = 0; i < 1; i++) {
9392
total: 1,
9493
description: `Smelly: Avoid using setTimeouts for tests. It might lead to Sleepy test or undeterministic behaviour based on where the test is executed.`,
9594
diagnostic: `Smelly: Avoid using setTimeouts for tests. It might lead to Sleepy test or undeterministic behaviour based on where the test is executed`,
96-
},
97-
{
95+
}],
96+
[{
9897
code: `function done() {};
9998
setTimeout(() => {
10099
done();
@@ -109,8 +108,8 @@ setTimeout(() => {
109108
total: 1,
110109
description: `Smelly: Avoid using setTimeouts for tests. It might lead to Sleepy test or undeterministic behaviour based on where the test is executed.`,
111110
diagnostic: `Smelly: Avoid using setTimeouts for tests. It might lead to Sleepy test or undeterministic behaviour based on where the test is executed`,
112-
},
113-
{
111+
}],
112+
[{
114113
code: `const a: number = 1;
115114
if (a === 1) { }`,
116115
language: TYPESCRIPT,
@@ -123,8 +122,8 @@ if (a === 1) { }`,
123122
total: 1,
124123
description: `Smelly: Avoid Conditional Test Logic in the test. Having conditional logic points to a test case that requires different context to run. Split the test case to fit one context per test case.`,
125124
diagnostic: `Smelly: Avoid Conditional Test Logic in the test. Having conditional logic points to a test case that requires different context to run. Split the test case to fit one context per test case.`,
126-
},
127-
{
125+
}],
126+
[{
128127

129128
code: `const a: number = 1;
130129
if (a === 1) { }
@@ -141,8 +140,8 @@ if (a === 2) {
141140
total: 3,
142141
description: `Smelly: Avoid Conditional Test Logic in the test. Having conditional logic points to a test case that requires different context to run. Split the test case to fit one context per test case.`,
143142
diagnostic: `Smelly: Avoid Conditional Test Logic in the test. Having conditional logic points to a test case that requires different context to run. Split the test case to fit one context per test case.`,
144-
},
145-
{
143+
}],
144+
[{
146145
code: `const lists: any[] = [{}, {}];
147146
148147
for (const i of lists) {
@@ -158,8 +157,8 @@ for (const i of lists) {
158157
total: 1,
159158
description: `Smelly: Avoid Conditional Test Logic in the test. Having conditional logic points to a test case that requires different context to run. Split the test case to fit one context per test case.`,
160159
diagnostic: `Smelly: Avoid Conditional Test Logic in the test. Having conditional logic points to a test case that requires different context to run. Split the test case to fit one context per test case.`
161-
},
162-
{
160+
}],
161+
[{
163162
code: `const lists: any[] = [{}, {}];
164163
165164
for (const i in lists) {
@@ -175,8 +174,8 @@ for (const i in lists) {
175174
total: 1,
176175
description: `Smelly: Avoid Conditional Test Logic in the test. Having conditional logic points to a test case that requires different context to run. Split the test case to fit one context per test case.`,
177176
diagnostic: `Smelly: Avoid Conditional Test Logic in the test. Having conditional logic points to a test case that requires different context to run. Split the test case to fit one context per test case.`
178-
},
179-
{
177+
}],
178+
[{
180179
code: `const lists: any[] = [{}, {}];
181180
182181
for (let i =0; i < 2; i++) {
@@ -192,8 +191,8 @@ for (let i =0; i < 2; i++) {
192191
total: 1,
193192
description: `Smelly: Avoid Conditional Test Logic in the test. Having conditional logic points to a test case that requires different context to run. Split the test case to fit one context per test case.`,
194193
diagnostic: `Smelly: Avoid Conditional Test Logic in the test. Having conditional logic points to a test case that requires different context to run. Split the test case to fit one context per test case.`
195-
},
196-
{
194+
}],
195+
[{
197196
code: `setTimeout(() => {
198197
done();
199198
});`,
@@ -207,8 +206,8 @@ for (let i =0; i < 2; i++) {
207206
total: 1,
208207
description: `Smelly: Avoid using setTimeouts for tests. It might lead to Sleepy test or undeterministic behaviour based on where the test is executed.`,
209208
diagnostic: `Smelly: Avoid using setTimeouts for tests. It might lead to Sleepy test or undeterministic behaviour based on where the test is executed`,
210-
},
211-
{
209+
}],
210+
[{
212211
code: `function done() {};
213212
setTimeout(() => {
214213
done();
@@ -223,8 +222,8 @@ setTimeout(() => {
223222
total: 1,
224223
description: `Smelly: Avoid using setTimeouts for tests. It might lead to Sleepy test or undeterministic behaviour based on where the test is executed.`,
225224
diagnostic: `Smelly: Avoid using setTimeouts for tests. It might lead to Sleepy test or undeterministic behaviour based on where the test is executed`,
226-
},
227-
{
225+
}],
226+
[{
228227
code: `describe("my test", () => {
229228
it("a", () => {
230229
console.log(1);
@@ -240,8 +239,8 @@ console.log(1);
240239
total: 1,
241240
description: `Smelly: Avoid poluting the test output. It is known as the loudmouth`,
242241
diagnostic: `Smelly: Avoid poluting the test output. It is known as the loudmouth`,
243-
},
244-
{
242+
}],
243+
[{
245244
code: `describe("my test", () => {
246245
it("a", () => {
247246
console.log(1);
@@ -257,8 +256,8 @@ console.log(1);
257256
total: 1,
258257
description: `Smelly: Avoid poluting the test output. It is known as the loudmouth`,
259258
diagnostic: `Smelly: Avoid poluting the test output. It is known as the loudmouth`,
260-
},
261-
{
259+
}],
260+
[{
262261
code: `describe("my test", () => {
263262
it("a", () => {
264263
console.error(1);
@@ -274,8 +273,8 @@ console.error(1);
274273
total: 1,
275274
description: `Smelly: Avoid poluting the test output. It is known as the loudmouth`,
276275
diagnostic: `Smelly: Avoid poluting the test output. It is known as the loudmouth`,
277-
},
278-
{
276+
}],
277+
[{
279278
code: `describe("my test", () => {
280279
it("a", () => {
281280
console.info(1);
@@ -291,8 +290,8 @@ console.info(1);
291290
total: 1,
292291
description: `Smelly: Avoid poluting the test output. It is known as the loudmouth`,
293292
diagnostic: `Smelly: Avoid poluting the test output. It is known as the loudmouth`,
294-
},
295-
{
293+
}],
294+
[{
296295
code: `describe("my test", () => {
297296
it("a", () => {
298297
console.info(1);
@@ -308,8 +307,8 @@ console.info(1);
308307
total: 1,
309308
description: `Smelly: Avoid poluting the test output. It is known as the loudmouth`,
310309
diagnostic: `Smelly: Avoid poluting the test output. It is known as the loudmouth`,
311-
},
312-
{
310+
}],
311+
[{
313312
code: `describe("my test", () => {
314313
it("a", () => {
315314
console.error(1);
@@ -325,8 +324,8 @@ console.error(1);
325324
total: 1,
326325
description: `Smelly: Avoid poluting the test output. It is known as the loudmouth`,
327326
diagnostic: `Smelly: Avoid poluting the test output. It is known as the loudmouth`,
328-
},
329-
{
327+
}],
328+
[{
330329
code: `jest.mock("../");
331330
jest.mock("../");
332331
jest.mock("../");
@@ -347,8 +346,8 @@ jest.mock("../");`,
347346
total: 1,
348347
description: `Smelly: Avoid mocking too many dependencies in the test file. Split the test cases to distribute the mocking load.`,
349348
diagnostic: `Smelly: Avoid mocking too many dependencies in the test file. Split the test cases to distribute the mocking load.`,
350-
},
351-
{
349+
}],
350+
[{
352351
code: `jest.mock("../");
353352
jest.mock("../");
354353
jest.mock("../");
@@ -370,34 +369,30 @@ jest.mock("../");`,
370369
total: 1,
371370
description: `Smelly: Avoid mocking too many dependencies in the test file. Split the test cases to distribute the mocking load.`,
372371
diagnostic: `Smelly: Avoid mocking too many dependencies in the test file. Split the test cases to distribute the mocking load.`,
373-
}
374-
].forEach(({ code, language, index, type, lineStart, lineEnd, startAt, endsAt, total, description, diagnostic }) => {
375-
test(`detect test smell for ${language}: type ${type} at index ${index}`, () => {
376-
const smellDetector = new SmellDetector(code, language);
377-
const result = smellDetector.findAll();
372+
}]
373+
])(`detect test smell for %s %s: type %s %s at index %s`, ({ code, language, index, type, lineStart, lineEnd, startAt, endsAt, total, description, diagnostic }) => {
374+
const smellDetector = new SmellDetector(code, language);
375+
const result = smellDetector.findAll();
378376

379-
assert.equal(result.length, total, 'number of detected smells is not correct');
380-
assert.equal(result[index].type, type, 'type');
381-
assert.equal(result[index].lineStart, lineStart, 'lineStart');
382-
assert.equal(result[index].lineEnd, lineEnd, 'lineEnd');
383-
assert.equal(result[index].startAt, startAt, 'startAt');
384-
assert.equal(result[index].endsAt, endsAt, 'endsAt');
385-
assert.equal(result[index].description, description, `description for ${type} does not match`);
386-
assert.equal(result[index].diagnostic, diagnostic, `diagnostic for ${type} does not match`);
387-
});
377+
expect(result.length).toEqual(total);
378+
expect(result[index].type).toEqual(type);
379+
expect(result[index].lineStart).toEqual(lineStart);
380+
expect(result[index].lineEnd).toEqual(lineEnd);
381+
expect(result[index].startAt).toEqual(startAt);
382+
expect(result[index].endsAt).toEqual(endsAt);
383+
expect(result[index].description).toEqual(description);
384+
expect(result[index].diagnostic).toEqual(diagnostic);
388385
});
389386

390-
[{
387+
test.each([{
391388
code: `
392389
jest.mock("../");`,
393390
language: TYPESCRIPT,
394391
}
395-
].forEach(({ code, language }) => {
396-
test(`detect code without smells`, () => {
397-
const smellDetector = new SmellDetector(code, language);
398-
const result = smellDetector.findAll();
392+
])(`detect code without smells`, ({ code, language }) => {
393+
const smellDetector = new SmellDetector(code, language);
394+
const result = smellDetector.findAll();
399395

400-
assert.equal(result.length, 0);
401-
});
396+
expect(result.length).toEqual(0);
402397
});
403398
});

detector/tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@
1818
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
1919
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
2020
// "noUnusedParameters": true, /* Report errors on unused parameters. */
21-
}
21+
},
22+
"exclude": [
23+
"test",
24+
]
2225
}

0 commit comments

Comments
 (0)