Skip to content

Commit

Permalink
test: detect general fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
Jhonatanmizu committed Jun 15, 2024
1 parent 91eb359 commit 3c615b0
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions test/common/detectors/generalFixture.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { describe, expect, it } from "vitest";
import astService from "../../../src/services/ast.service";
import detectGeneralFixture from "../../../src/common/detectors/generalFixture";

describe("GeneralFixture", () => {
it("should detect general fixture when some variable are unused", () => {
const code = `
describe("test", () => {
let user;
let admin;
let guest;
beforeEach(() => {
user = new User("Alice", 30);
admin = new Admin("Bob", 40);
guest = new Guest("Charlie", 25);
});
test("user should have a name", () => {
expect(user.name).toBe("Alice");
});
test("admin should have an age", () => {
expect(admin.age).toBe(40);
});
})
`;

const ast = astService.parseCodeToAst(code);
const result = detectGeneralFixture(ast);
expect(result).toBeDefined();
expect(result).toBeInstanceOf(Array);
expect(result).toHaveLength(1);
});
it("should not detect general fixture when some setup variable are block scoped", () => {
const code = `
describe("test", () => {
beforeEach(() => {
const user = new User("Alice", 30);
const admin = new Admin("Bob", 40);
const guest = new Guest("Charlie", 25);
});
test("user should have a name", () => {
expect(user.name).toBe("Alice");
});
test("admin should have an age", () => {
expect(admin.age).toBe(40);
});
})
`;

const ast = astService.parseCodeToAst(code);
const result = detectGeneralFixture(ast);
expect(result).toBeDefined();
expect(result).toBeInstanceOf(Array);
expect(result).toHaveLength(0);
});
});

0 comments on commit 3c615b0

Please sign in to comment.