Skip to content

Commit

Permalink
refactor(config): update config to detect test.ts files instead of fi…
Browse files Browse the repository at this point in the history
…les under test dir (#194)
  • Loading branch information
khalatevarun authored Jan 2, 2025
1 parent 79c0b12 commit d5c61f9
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 61 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ yarn shortest
export default {
headless: false,
baseUrl: 'http://localhost:3000',
testDir: 'app/__tests__',
testPattern: '**/*.test.ts',
anthropicKey: process.env.ANTHROPIC_API_KEY
} satisfies ShortestConfig;
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { shortest } from "@antiwork/shortest";

shortest("Verify that buttons are rounded on the landing page");
shortest(
"Login to the App using magic link. Use this email: '[email protected]'",
);
Expand Down
2 changes: 1 addition & 1 deletion packages/shortest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import type { ShortestConfig } from '@antiwork/shortest';
export default {
headless: false,
baseUrl: 'http://localhost:3000',
testDir: 'app/__tests__',
testPattern: "**/*.test.ts",
anthropicKey: process.env.ANTHROPIC_API_KEY
} satisfies ShortestConfig;
```
Expand Down
14 changes: 6 additions & 8 deletions packages/shortest/src/cli/bin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node

import pc from "picocolors";
import { getConfig } from "..";
import { GitHubTool } from "../browser/integrations/github";
import { TestRunner } from "../core/runner";

Expand Down Expand Up @@ -132,7 +133,7 @@ async function main() {
const targetUrl = args
.find((arg) => arg.startsWith("--target="))
?.split("=")[1];
const testPattern = args.find((arg) => !arg.startsWith("--"));
const cliTestPattern = args.find((arg) => !arg.startsWith("--"));
const debugAI = args.includes("--debug-ai");
const noCache = args.includes("--no-cache");

Expand All @@ -146,12 +147,9 @@ async function main() {
noCache,
);
await runner.initialize();

if (testPattern) {
await runner.runFile(testPattern);
} else {
await runner.runAll();
}
const config = getConfig();
const testPattern = cliTestPattern || config.testPattern;
await runner.runTests(testPattern);
} catch (error) {
if (error instanceof Error) {
if (error.message.includes("Config")) {
Expand All @@ -164,7 +162,7 @@ async function main() {
);
console.error(pc.dim(" - headless: boolean"));
console.error(pc.dim(" - baseUrl: string"));
console.error(pc.dim(" - testDir: string | string[]"));
console.error(pc.dim(" - testPattern: string"));
console.error(pc.dim(" - anthropicKey: string"));
console.error();
} else {
Expand Down
56 changes: 9 additions & 47 deletions packages/shortest/src/core/runner/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { resolve } from "path";
import Anthropic from "@anthropic-ai/sdk";
import { glob } from "glob";
import pc from "picocolors";
Expand Down Expand Up @@ -83,37 +82,17 @@ export class TestRunner {
}

private async findTestFiles(pattern?: string): Promise<string[]> {
const testDirs = Array.isArray(this.config.testDir)
? this.config.testDir
: [this.config.testDir || "__tests__"];

const files = [];
for (const dir of testDirs) {
if (pattern) {
const cleanPattern = pattern
.replace(/\.ts$/, "")
.replace(/\.test$/, "")
.split("/")
.pop();

const globPattern = `${dir}/**/${cleanPattern}.test.ts`;
const matches = await glob(globPattern, {
cwd: this.cwd,
absolute: true,
});

files.push(...matches);
} else {
const globPattern = `${dir}/**/*.test.ts`;
const matches = await glob(globPattern, { cwd: this.cwd });
files.push(...matches.map((f) => resolve(this.cwd, f)));
}
}
const testPattern = pattern || this.config.testPattern || "**/*.test.ts";

const files = await glob(testPattern, {
cwd: this.cwd,
absolute: true,
});

if (files.length === 0) {
this.logger.error(
"Test Discovery",
`No test files found in directories: ${testDirs.join(", ")}`,
`No test files found matching: ${testPattern}`,
);
process.exit(1);
}
Expand Down Expand Up @@ -386,14 +365,14 @@ export class TestRunner {
}
}

async runFile(pattern: string) {
async runTests(pattern?: string) {
await this.initialize();
const files = await this.findTestFiles(pattern);

if (files.length === 0) {
this.logger.error(
"Test Discovery",
`No test files found matching: ${pattern}`,
`No test files found matching the pattern: ${pattern || this.config.testPattern}`,
);
process.exit(1);
}
Expand All @@ -411,23 +390,6 @@ export class TestRunner {
}
}

async runAll() {
await this.initialize();
const files = await this.findTestFiles();

for (const file of files) {
await this.executeTestFile(file);
}

this.logger.summary();

if (this.exitOnSuccess && this.logger.allTestsPassed()) {
process.exit(0);
} else {
process.exit(1);
}
}

private async runCachedTest(
test: TestFunction,
browserTool: BrowserTool,
Expand Down
4 changes: 2 additions & 2 deletions packages/shortest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function validateConfig(config: Partial<ShortestConfig>) {

if (config.headless === undefined) missingFields.push("headless");
if (!config.baseUrl) missingFields.push("baseUrl");
if (!config.testDir) missingFields.push("testDir");
if (!config.testPattern) missingFields.push("testPattern");
if (!config.anthropicKey && !process.env.ANTHROPIC_API_KEY)
missingFields.push("anthropicKey");

Expand Down Expand Up @@ -97,7 +97,7 @@ export async function initialize() {
"Required fields:\n" +
" - headless: boolean\n" +
" - baseUrl: string\n" +
" - testDir: string | string[]\n" +
" - testPattern: string\n" +
" - anthropicKey: string",
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/shortest/src/types/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export interface ShortestConfig {
headless: boolean;
baseUrl: string;
testDir: string | string[];
testPattern: string;
anthropicKey: string;
mailosaur?: {
apiKey: string;
Expand Down
2 changes: 1 addition & 1 deletion shortest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { ShortestConfig } from "@antiwork/shortest";
export default {
headless: false,
baseUrl: "http://localhost:3000",
testDir: ["app/__tests__", "examples"],
testPattern: "**/*.test.ts",
anthropicKey: process.env.ANTHROPIC_API_KEY,
mailosaur: {
apiKey: process.env.MAILOSAUR_API_KEY,
Expand Down

0 comments on commit d5c61f9

Please sign in to comment.