Skip to content

Commit 332756a

Browse files
authored
Merge pull request #2 from rluvaton/add-support-for-each
feat: add support for `.each`
2 parents b23d71a + 711edde commit 332756a

3 files changed

Lines changed: 61 additions & 13 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vscode-vitest",
33
"displayName": "Vitest Runner for VSCode that actually work",
4-
"version": "0.1.0",
4+
"version": "0.2.0",
55
"main": "dist/index.js",
66
"icon": "logo.png",
77
"license": "MIT",
@@ -32,7 +32,7 @@
3232
"esbuild": "^0.14.27",
3333
"prettier": "^2.6.0",
3434
"typescript": "^4.6.2",
35-
"vitest": "^0.7.7",
35+
"vitest": "^0.34.1",
3636
"vsce": "^2.7.0"
3737
},
3838
"scripts": {

src/codelens.ts

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@ import { TextCase } from './types';
44
import { flatMap } from './utils';
55
import { RunVitestCommand, DebugVitestCommand } from './vscode';
66

7-
const caseText = new Set(['it', 'describe','test']);
7+
const caseText = new Set(['it', 'describe', 'test']);
88

99
function tryGetVitestTestCase(
1010
typescript: typeof ts,
1111
callExpression: ts.CallExpression,
1212
file: ts.SourceFile
1313
): TextCase | undefined {
14-
if (!typescript.isIdentifier(callExpression.expression)) {
15-
return undefined;
16-
}
17-
18-
if (!caseText.has(callExpression.expression.text)) {
14+
const each = isEach(typescript, callExpression);
15+
if (!each && !(typescript.isIdentifier(callExpression.expression) && caseText.has((callExpression.expression as ts.Identifier).text))) {
1916
return undefined;
2017
}
2118

@@ -32,15 +29,55 @@ function tryGetVitestTestCase(
3229
return undefined;
3330
}
3431

32+
let testNameText = testName.text;
33+
34+
const start = callExpression.getStart(file);
35+
if (each) {
36+
//
37+
testNameText = testNameText
38+
// From https://github.com/jestjs/jest/blob/0fd5b1c37555f485c56a6ad2d6b010a72204f9f6/packages/jest-each/src/table/array.ts#L15C32-L15C47
39+
// (Did not find inside vitest source code)
40+
.replace(/%[sdifjoOp#]/g, '.*')
41+
// When using template string
42+
.replace(/\$[a-zA-Z_0-9]+/g, '.*');
43+
}
44+
3545
return {
36-
start: testName.getStart(file),
37-
end: testName.getEnd(),
38-
text: testName.text
46+
start,
47+
end: callExpression.getEnd(),
48+
text: testNameText
3949
};
4050
}
4151

52+
function isEach(typescript: typeof ts, callExpression: ts.CallExpression) {
53+
return isEachWithArray(typescript, callExpression) || isEachWithTemplate(typescript, callExpression);
54+
}
55+
56+
function isEachWithArray(typescript: typeof ts, callExpression: ts.CallExpression) {
57+
return (
58+
typescript.isCallExpression(callExpression.expression) &&
59+
typescript.isPropertyAccessExpression(callExpression.expression.expression) &&
60+
typescript.isIdentifier(callExpression.expression.expression.expression) &&
61+
typescript.isIdentifier(callExpression.expression.expression.name) &&
62+
callExpression.expression.expression.name.text === 'each' &&
63+
caseText.has(callExpression.expression.expression.expression.text)
64+
);
65+
}
66+
67+
function isEachWithTemplate(typescript: typeof ts, callExpression: ts.CallExpression) {
68+
return (
69+
typescript.isTaggedTemplateExpression(callExpression.expression) &&
70+
typescript.isPropertyAccessExpression(callExpression.expression.tag) &&
71+
typescript.isIdentifier(callExpression.expression.tag.expression) &&
72+
typescript.isIdentifier(callExpression.expression.tag.name) &&
73+
callExpression.expression.tag.name.text === 'each' &&
74+
caseText.has(callExpression.expression.tag.expression.text)
75+
);
76+
}
77+
4278
export class CodeLensProvider implements vscode.CodeLensProvider {
43-
constructor(private typescript: typeof ts) {}
79+
constructor(private typescript: typeof ts) {
80+
}
4481

4582
provideCodeLenses(
4683
document: vscode.TextDocument,

tests/cases/test.spec.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
import { describe, it, test, expect } from 'vitest';
22

33
describe('Test', () => {
4-
it('Should work', () => {
4+
it('Should work', () => {
55
expect(1 + 41).toBe(42);
66
});
77
});
88

99
test("Test should work", () => {
1010
expect(42).toBe(42)
1111
})
12+
13+
it.each([1])('test this %s', (s) => {
14+
console.log(s);
15+
});
16+
17+
it.each`
18+
value
19+
${1}
20+
`('test this $va ccasacs', ({value}) => {
21+
console.log(value);
22+
});

0 commit comments

Comments
 (0)