Skip to content

Commit

Permalink
refactor to builder
Browse files Browse the repository at this point in the history
  • Loading branch information
timbrinded committed Jul 5, 2024
1 parent a08f101 commit c6af8b2
Showing 1 changed file with 84 additions and 64 deletions.
148 changes: 84 additions & 64 deletions packages/cli/src/cmds/runTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,27 +91,6 @@ export async function executeTests(env: Environment, testRunArgs?: testRunArgs)
}
}

const baseOptions = {
watch: false,
globals: true,
reporters: env.reporters ? env.reporters : ["default"],
outputFile: env.reportFile,
testTimeout: env.timeout || globalConfig.defaultTestTimeout,
hookTimeout: env.timeout || globalConfig.defaultTestTimeout,
passWithNoTests: false,
deps: {
optimizer: { ssr: { enabled: false }, web: { enabled: false } },
},
include: env.include ? env.include : ["**/*{test,spec,test_,test-}*{ts,mts,cts}"],
onConsoleLog(log) {
if (filterList.includes(log.trim())) return false;
// if (log.trim() === "stdout | unknown test" || log.trim() === "<empty line>") return false;
if (log.includes("has multiple versions, ensure that there is only one installed.")) {
return false;
}
},
} satisfies UserConfig;

const additionalArgs: testRunArgs = testRunArgs || {};

// transform in regexp pattern
Expand All @@ -120,8 +99,13 @@ export async function executeTests(env: Environment, testRunArgs?: testRunArgs)
additionalArgs.testNamePattern = `^((?!${env.skipTests?.map((test) => `${test.name}`).join("|")}).)*$`;
}

// TODO: Create options builder class
const options = addThreadConfig(baseOptions, env.multiThreads);
const options = new VitestOptionsBuilder()
.setReporters(env.reporters || ["default"])
.setOutputFile(env.reportFile)
.setTimeout(env.timeout || globalConfig.defaultTestTimeout)
.setInclude(env.include || ["**/*{test,spec,test_,test-}*{ts,mts,cts}"])
.addThreadConfig(env.multiThreads)
.build();

if (
globalConfig.environments.find((env) => env.name === process.env.MOON_TEST_ENV)?.foundation
Expand All @@ -134,8 +118,10 @@ export async function executeTests(env: Environment, testRunArgs?: testRunArgs)
try {
const testFileDir =
additionalArgs?.subDirectory !== undefined
? // @ts-expect-error - bug in tsc
env.testFileDir.map((folder) => path.join(folder, additionalArgs.subDirectory))
? env.testFileDir.map((folder) =>
// @ts-expect-error - bug in tsc
path.join(folder, additionalArgs.subDirectory)
)
: env.testFileDir;

const folders = testFileDir.map((folder) => path.join(".", folder, "/"));
Expand All @@ -154,59 +140,93 @@ export async function executeTests(env: Environment, testRunArgs?: testRunArgs)

const filterList = ["<empty line>", "", "stdout | unknown test"];

function addThreadConfig(
config: UserConfig,
threads: number | boolean | object = false
): UserConfig {
const configWithThreads: UserConfig = {
...config,
fileParallelism: false,
pool: "forks",
poolOptions: {
class VitestOptionsBuilder {
private options: UserConfig = {
watch: false,
globals: true,
reporters: ["default"],
passWithNoTests: false,
deps: {
optimizer: { ssr: { enabled: false }, web: { enabled: false } },
},
include: ["**/*{test,spec,test_,test-}*{ts,mts,cts}"],
onConsoleLog(log) {
if (filterList.includes(log.trim())) return false;
if (log.includes("has multiple versions, ensure that there is only one installed.")) {
return false;
}
},
};

setReporters(reporters: string[]): this {
this.options.reporters = reporters;
return this;
}

setOutputFile(
file?:
| string
| {
[reporterName: string]: string;
}
): this {
if (!file) {
console.log("No output file specified, skipping");
return this;
}
this.options.outputFile = file;
return this;
}

setTimeout(timeout: number): this {
this.options.testTimeout = timeout;
this.options.hookTimeout = timeout;
return this;
}

setInclude(include: string[]): this {
this.options.include = include;
return this;
}

addThreadConfig(threads: number | boolean | object = false): this {
this.options.fileParallelism = false;
this.options.pool = "forks";
this.options.poolOptions = {
forks: {
isolate: true,
minForks: 1,
maxForks: 3,
singleFork: false,
},
},
};
};

if (threads === true && process.env.MOON_RECYCLE !== "true") {
if (!configWithThreads.poolOptions) {
throw new Error("poolOptions not defined in config, this is an error please raise.");
if (threads === true && process.env.MOON_RECYCLE !== "true") {
this.options.fileParallelism = true;
}
configWithThreads.fileParallelism = true;
configWithThreads.poolOptions.forks = {
isolate: true,
minForks: 1,
maxForks: 3,
singleFork: false,
};
}

if (typeof threads === "number" && process.env.MOON_RECYCLE !== "true") {
if (!configWithThreads.poolOptions) {
throw new Error("poolOptions not defined in config, this is an error please raise.");
if (typeof threads === "number" && process.env.MOON_RECYCLE !== "true") {
this.options.fileParallelism = true;
if (this.options.poolOptions?.forks) {
this.options.poolOptions.forks.maxForks = threads;
this.options.poolOptions.forks.singleFork = false;
}
}

if (!configWithThreads.poolOptions.forks) {
throw new Error("poolOptions.forks not defined in config, this is an error please raise.");
if (typeof threads === "object" && process.env.MOON_RECYCLE !== "true") {
const key = Object.keys(threads)[0];
if (["threads", "forks", "vmThreads", "typescript"].includes(key)) {
this.options.pool = key as "threads" | "forks" | "vmThreads" | "typescript";
this.options.poolOptions = Object.values(threads)[0];
} else {
throw new Error(`Invalid pool type: ${key}`);
}
}

configWithThreads.fileParallelism = true;
configWithThreads.poolOptions.forks.maxForks = threads;
configWithThreads.poolOptions.forks.singleFork = false;
return this;
}

if (typeof threads === "object" && process.env.MOON_RECYCLE !== "true") {
const key = Object.keys(threads)[0];
if (["threads", "forks", "vmThreads", "typescript"].includes(key)) {
configWithThreads.pool = key as "threads" | "forks" | "vmThreads" | "typescript";
configWithThreads.poolOptions = Object.values(threads)[0];
} else {
throw new Error(`Invalid pool type: ${key}`);
}
build(): UserConfig {
return this.options;
}
return configWithThreads;
}

0 comments on commit c6af8b2

Please sign in to comment.