Skip to content

Commit

Permalink
Merge pull request #14 from Jhonatanmizu/test-ast-service
Browse files Browse the repository at this point in the history
🧪 test ast service
  • Loading branch information
Jhonatanmizu committed Jun 10, 2024
2 parents 5f64b60 + 0d6a812 commit da75df1
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 10 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"start": "pm2 start process.yml -i max -f --no-daemon",
"start:dev": "node --watch src/server.js",
"lint": "yarn run eslint src/*",
"test": "vitest test/",
"test:watch": "vitest --watch test/",
"test": "vitest run test/",
"test:watch": "vitest test/",
"test:coverage": "vitest run --coverage test/"
},
"type": "module",
Expand All @@ -31,11 +31,12 @@
"pm2": "^5.3.1",
"rimraf": "^5.0.5"
},
"repository": "https://github.com/Jhonatanmizu/mizu-js-test-smell-detector-api.git",
"repository": "https://github.com/Jhonatanmizu/snutsjs.git",
"devDependencies": {
"@eslint/js": "^9.3.0",
"@vitest/coverage-v8": "^1.6.0",
"eslint": "9.x",
"git-commit-msg-linter": "^5.0.8",
"globals": "^15.3.0",
"vitest": "^1.6.0"
}
Expand Down
5 changes: 3 additions & 2 deletions src/common/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { rimraf } from "rimraf";
import path from "node:path";
import parser from "@babel/parser";
import process from "node:process";
const isWin = process.platform === "win32";

const IS_WIN_SO = process.platform === "win32";

const TEST_FILE_PATTERNS = [
"**/*.test.js",
Expand Down Expand Up @@ -70,7 +71,7 @@ class Helpers {
const options = {
ignore: "node_modules",
cwd: directory,
windowsPathsNoEscape: isWin,
windowsPathsNoEscape: IS_WIN_SO,
absolute: true,
nodir: true,
};
Expand Down
25 changes: 20 additions & 5 deletions src/services/ast.service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import traverse from "@babel/traverse";
const traverseDefault = traverse.default;
import * as t from "@babel/types";
import parser from "@babel/parser";
import fs from "node:fs";
Expand Down Expand Up @@ -34,6 +33,9 @@ const jestSuiteAliases = ["describe"];
const jestTestAliases = ["it", "test"];

class AstService {
traverseDefault =
typeof traverse === "function" ? traverse : traverse.default;

getTestInfo(ast) {
return {
itCount: this.getItCount(ast),
Expand All @@ -43,7 +45,7 @@ class AstService {

getDescribeCount(ast) {
let describeCount = 0;
traverseDefault(ast, {
this.traverseDefault(ast, {
CallExpression: ({ node }) => {
if (node.callee.name === "describe") {
describeCount++;
Expand All @@ -55,7 +57,7 @@ class AstService {

getItCount(ast) {
let itCount = 0;
traverseDefault(ast, {
this.traverseDefault(ast, {
CallExpression: ({ node }) => {
if (this.isTestCase(node) && /it|test/g.test(node.callee.name)) {
itCount++;
Expand All @@ -66,9 +68,9 @@ class AstService {
}

getTestNodeAst(code) {
const ast = this.parseToAst(code);
const ast = this.parseCodeToAst(code);
let testNode;
traverseDefault(ast, {
this.traverseDefault(ast, {
CallExpression(path) {
if (jestSuiteAliases.includes(path.node.callee.name)) {
path.traverse({
Expand Down Expand Up @@ -141,6 +143,19 @@ class AstService {
throw error;
}
}
hasAssertion(ast) {
let isValid = false;
const checkIsAssert = this.isAssert;
traverse(ast, {
ExpressionStatement(path) {
if (checkIsAssert(path.node)) {
isValid = true;
}
},
});
return isValid;
}

hasManyComments(node, maxComments) {
let commentCount = 0;
// Traverse the function node to count comments
Expand Down
62 changes: 62 additions & 0 deletions test/services/ast.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,42 @@ import { describe, expect, it } from "vitest";
import astService from "../../src/services/ast.service";

describe("Ast Service", () => {
it("should correctly count describe block", () => {
const code = `
describe('My Test Suite', () => {
it('should do something', () => {
expect(true).toBe(true);
});
it('should do another thing', () => {
expect(false).toBe(false);
});
});
`;

const result = astService.parseCodeToAst(code);
const describeCount = astService.getDescribeCount(result);
expect(describeCount).toBe(1);
});

it("should correctly count it/test block", () => {
const code = `
describe('My Test Suite', () => {
it('should do something', () => {
expect(true).toBe(true);
});
test('should do another thing', () => {
expect(false).toBe(false);
});
});
`;

const result = astService.parseCodeToAst(code);
const describeCount = astService.getItCount(result);
expect(describeCount).toBe(2);
});

it("should parse code in ast", () => {
const code = `
test("some test", () =>{
Expand Down Expand Up @@ -36,4 +72,30 @@ describe("Ast Service", () => {
const ast = astService.parseCodeToAst(code);
expect(ast).toBeDefined();
});

it("should parse code to AST and return the correct test node", () => {
const code = `
describe('some test', () => {
it('should do something', () => {
expect(true).toBe(true);
});
});
`;
const testNode = astService.getTestNodeAst(code);
expect(testNode).toBeDefined();
expect(testNode.node.callee.name).toBe("it");
});

it("should detect a assertion with expect/assert", () => {
const setupCode = `
describe("some test", ()=>{
it("should check numbers",() =>{
expect(13).toBe(13)
})
})
`;
const ast = astService.parseCodeToAst(setupCode);
const result = astService.hasAssertion(ast);
expect(result).toBeTruthy();
});
});
16 changes: 16 additions & 0 deletions vitest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// <reference types="vitest" />
import { defineConfig } from "vite";

export default defineConfig({
test: {
// ... Specify options here.
exclude: [
"**/node_modules/**",
"**/dist/**",
"**/cypress/**",
"**/.{idea,git,cache,output,temp}/**",
"**/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build}.config.*",
"**/public/**",
],
},
});
41 changes: 41 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,14 @@ [email protected]:
resolved "https://registry.yarnpkg.com/commander/-/commander-2.15.1.tgz#df46e867d0fc2aec66a34662b406a9ccafff5b0f"
integrity sha512-VlfT9F3V0v+jr4yxPc5gg9s62/fIVWsd2Bk2iD435um1NlGMYdVCq+MjcXnhYq2icNOizHr1kK+5TI6H0Hy0ag==

commit-msg-linter@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/commit-msg-linter/-/commit-msg-linter-1.0.0.tgz#45a29e9c38d8bb71e98d4da67f230abf01b46463"
integrity sha512-Di9oAjfjIwAcASQ8abW1936twemUdDQq4D1dBL42Y+S6rNXykauIBPztj8Ej4/MjI6zIziLVzfhmdkg0z4I4rg==
dependencies:
did-you-mean "^0.0.1"
supports-color "^8.1.1"

[email protected]:
version "0.0.1"
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
Expand Down Expand Up @@ -1217,6 +1225,14 @@ [email protected]:
resolved "https://registry.yarnpkg.com/depd/-/depd-2.0.0.tgz#b696163cc757560d09cf22cc8fad1571b79e76df"
integrity sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==

did-you-mean@^0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/did-you-mean/-/did-you-mean-0.0.1.tgz#8851ce82407903cb62c12cb6ad4f676921ccdec3"
integrity sha512-rKxPpbrHr4/u8VMPde6Z3DhbGoXw7kepHZIU2FGs+IU+LKnhEIlw1cx80FZ7fLruJXDyu0slKMai2FhPRZjwxQ==
dependencies:
levenshtein "*"
underscore "*"

diff-sequences@^29.6.3:
version "29.6.3"
resolved "https://registry.yarnpkg.com/diff-sequences/-/diff-sequences-29.6.3.tgz#4deaf894d11407c51efc8418012f9e70b84ea921"
Expand Down Expand Up @@ -1683,6 +1699,14 @@ get-uri@^6.0.1:
debug "^4.3.4"
fs-extra "^11.2.0"

git-commit-msg-linter@^5.0.8:
version "5.0.8"
resolved "https://registry.yarnpkg.com/git-commit-msg-linter/-/git-commit-msg-linter-5.0.8.tgz#733b8853306c7fee46a6c01833dcae729e830431"
integrity sha512-buif1Hfo3barMYIOz9kKbyD6upC4/bQSDVPuGMIXho3bAgaTCG0fPhOVq9JAFf9XAfcpRWqgIK1Q+BB7exNdFA==
dependencies:
chalk "^2.4.2"
commit-msg-linter "^1.0.0"

git-node-fs@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/git-node-fs/-/git-node-fs-1.0.0.tgz#49b215e242ebe43aa4c7561bbba499521752080f"
Expand Down Expand Up @@ -2070,6 +2094,11 @@ lazy@~1.0.11:
resolved "https://registry.yarnpkg.com/lazy/-/lazy-1.0.11.tgz#daa068206282542c088288e975c297c1ae77b690"
integrity sha512-Y+CjUfLmIpoUCCRl0ub4smrYtGGr5AOa2AKOaWelGHOGz33X/Y/KizefGqbkwfz44+cnq/+9habclf8vOmu2LA==

levenshtein@*:
version "1.0.5"
resolved "https://registry.yarnpkg.com/levenshtein/-/levenshtein-1.0.5.tgz#3911737a9cb56da345d008f55782c6f138979ba3"
integrity sha512-UQf1nnmxjl7O0+snDXj2YF2r74Gkya8ZpnegrUBYN9tikh2dtxV/ey8e07BO5wwo0i76yjOvbDhFHdcPEiH9aA==

levn@^0.4.1:
version "0.4.1"
resolved "https://registry.yarnpkg.com/levn/-/levn-0.4.1.tgz#ae4562c007473b932a6200d403268dd2fffc6ade"
Expand Down Expand Up @@ -3074,6 +3103,13 @@ supports-color@^7.1.0:
dependencies:
has-flag "^4.0.0"

supports-color@^8.1.1:
version "8.1.1"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-8.1.1.tgz#cd6fc17e28500cff56c1b86c0a7fd4a54a73005c"
integrity sha512-MpUEN2OodtUzxvKQl72cUF7RQ5EiHsGvSsVG0ia9c5RbWGL2CI4C7EpPS8UTBIplnlzZiNuV56w+FuNxy3ty2Q==
dependencies:
has-flag "^4.0.0"

supports-preserve-symlinks-flag@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz#6eda4bd344a3c94aea376d4cc31bc77311039e09"
Expand Down Expand Up @@ -3181,6 +3217,11 @@ ufo@^1.5.3:
resolved "https://registry.yarnpkg.com/ufo/-/ufo-1.5.3.tgz#3325bd3c977b6c6cd3160bf4ff52989adc9d3344"
integrity sha512-Y7HYmWaFwPUmkoQCUIAYpKqkOf+SbVj/2fJJZ4RJMCfZp0rTGwRbzQD+HghfnhKOjL9E01okqz+ncJskGYfBNw==

underscore@*:
version "1.13.6"
resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.13.6.tgz#04786a1f589dc6c09f761fc5f45b89e935136441"
integrity sha512-+A5Sja4HP1M08MaXya7p5LvjuM7K6q/2EaC0+iovj/wOcMsTzMvDFbasi/oSapiwOlt252IqsKqPjCl7huKS0A==

universalify@^2.0.0:
version "2.0.1"
resolved "https://registry.yarnpkg.com/universalify/-/universalify-2.0.1.tgz#168efc2180964e6386d061e094df61afe239b18d"
Expand Down

0 comments on commit da75df1

Please sign in to comment.