Skip to content

Commit

Permalink
Merge pull request #2 from rluvaton/add-support-for-each
Browse files Browse the repository at this point in the history
feat: add support for `.each`
  • Loading branch information
rluvaton committed Aug 6, 2023
2 parents b23d71a + 711edde commit 332756a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 13 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vscode-vitest",
"displayName": "Vitest Runner for VSCode that actually work",
"version": "0.1.0",
"version": "0.2.0",
"main": "dist/index.js",
"icon": "logo.png",
"license": "MIT",
Expand Down Expand Up @@ -32,7 +32,7 @@
"esbuild": "^0.14.27",
"prettier": "^2.6.0",
"typescript": "^4.6.2",
"vitest": "^0.7.7",
"vitest": "^0.34.1",
"vsce": "^2.7.0"
},
"scripts": {
Expand Down
57 changes: 47 additions & 10 deletions src/codelens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@ import { TextCase } from './types';
import { flatMap } from './utils';
import { RunVitestCommand, DebugVitestCommand } from './vscode';

const caseText = new Set(['it', 'describe','test']);
const caseText = new Set(['it', 'describe', 'test']);

function tryGetVitestTestCase(
typescript: typeof ts,
callExpression: ts.CallExpression,
file: ts.SourceFile
): TextCase | undefined {
if (!typescript.isIdentifier(callExpression.expression)) {
return undefined;
}

if (!caseText.has(callExpression.expression.text)) {
const each = isEach(typescript, callExpression);
if (!each && !(typescript.isIdentifier(callExpression.expression) && caseText.has((callExpression.expression as ts.Identifier).text))) {
return undefined;
}

Expand All @@ -32,15 +29,55 @@ function tryGetVitestTestCase(
return undefined;
}

let testNameText = testName.text;

const start = callExpression.getStart(file);
if (each) {
//
testNameText = testNameText
// From https://github.com/jestjs/jest/blob/0fd5b1c37555f485c56a6ad2d6b010a72204f9f6/packages/jest-each/src/table/array.ts#L15C32-L15C47
// (Did not find inside vitest source code)
.replace(/%[sdifjoOp#]/g, '.*')
// When using template string
.replace(/\$[a-zA-Z_0-9]+/g, '.*');
}

return {
start: testName.getStart(file),
end: testName.getEnd(),
text: testName.text
start,
end: callExpression.getEnd(),
text: testNameText
};
}

function isEach(typescript: typeof ts, callExpression: ts.CallExpression) {
return isEachWithArray(typescript, callExpression) || isEachWithTemplate(typescript, callExpression);
}

function isEachWithArray(typescript: typeof ts, callExpression: ts.CallExpression) {
return (
typescript.isCallExpression(callExpression.expression) &&
typescript.isPropertyAccessExpression(callExpression.expression.expression) &&
typescript.isIdentifier(callExpression.expression.expression.expression) &&
typescript.isIdentifier(callExpression.expression.expression.name) &&
callExpression.expression.expression.name.text === 'each' &&
caseText.has(callExpression.expression.expression.expression.text)
);
}

function isEachWithTemplate(typescript: typeof ts, callExpression: ts.CallExpression) {
return (
typescript.isTaggedTemplateExpression(callExpression.expression) &&
typescript.isPropertyAccessExpression(callExpression.expression.tag) &&
typescript.isIdentifier(callExpression.expression.tag.expression) &&
typescript.isIdentifier(callExpression.expression.tag.name) &&
callExpression.expression.tag.name.text === 'each' &&
caseText.has(callExpression.expression.tag.expression.text)
);
}

export class CodeLensProvider implements vscode.CodeLensProvider {
constructor(private typescript: typeof ts) {}
constructor(private typescript: typeof ts) {
}

provideCodeLenses(
document: vscode.TextDocument,
Expand Down
13 changes: 12 additions & 1 deletion tests/cases/test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import { describe, it, test, expect } from 'vitest';

describe('Test', () => {
it('Should work', () => {
it('Should work', () => {
expect(1 + 41).toBe(42);
});
});

test("Test should work", () => {
expect(42).toBe(42)
})

it.each([1])('test this %s', (s) => {
console.log(s);
});

it.each`
value
${1}
`('test this $va ccasacs', ({value}) => {
console.log(value);
});

0 comments on commit 332756a

Please sign in to comment.