Skip to content

Commit

Permalink
Fix runtime setup file
Browse files Browse the repository at this point in the history
  • Loading branch information
nazarhussain committed Nov 22, 2024
1 parent cacb608 commit 988312f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 56 deletions.
25 changes: 1 addition & 24 deletions vitest.base.unit.config.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,6 @@
import path from "node:path";
import {defineConfig} from "vitest/config";
import {BuiltinEnvironment} from "vitest/node";
import {Environment} from "vitest/environments";
const __dirname = new URL(".", import.meta.url).pathname;
import denoEnvironment from "./vitest/environments/deno";

function getTestEnvironment(
runtime?: string
): BuiltinEnvironment | Environment {
switch (runtime) {
case "deno":
return denoEnvironment;
case "node":
return "node";
default: {
if (runtime) {
throw new Error(`Unsupported JS Runtime '${runtime}'`);
} else {
throw new Error(
"Please define `JS_RUNTIME` env variable to either node | deno"
);
}
}
}
}

export default defineConfig({
resolve: {
Expand Down Expand Up @@ -51,6 +28,6 @@ export default defineConfig({
"hanging-process",
],
onConsoleLog: () => !process.env.TEST_QUIET_CONSOLE,
environment: getTestEnvironment(process.env.JS_RUNTIME),
globalSetup: [`${__dirname}/vitest/globalSetup/runtime.ts`],
},
});
32 changes: 0 additions & 32 deletions vitest/environments/deno.ts

This file was deleted.

33 changes: 33 additions & 0 deletions vitest/globalSetup/runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { GlobalSetupContext } from "vitest/node";

export default function setup({ provide }: GlobalSetupContext): void {
const runtime = process.env.JS_RUNTIME;

switch (runtime) {
case "deno": {
// @ts-expect-error - Ignore the global object
if (!global.Deno) {
throw new Error("Not valid deno runtime available");
}
break;
}
case "node": {
if (!global.process) {
throw new Error("Not valid js runtime available");
}
break;
}
default:
throw new Error(
`Not valid deno runtime available "${runtime}". Possible options are deno | node`
);
}

provide("runtime", runtime);
}

declare module "vitest" {
export interface ProvidedContext {
runtime: "deno" | "node";
}
}

0 comments on commit 988312f

Please sign in to comment.