scai
Version:
> AI-powered CLI tools for smart commit messages, auto generated comments, and readme files — all powered by local models.
147 lines (144 loc) • 1.93 kB
JavaScript
"use strict";
Here;
's a Jest test file for the `checkEnv` function: `` `typescript
import { checkEnv } from "../src/utils";
describe("checkEnv", () => {
it("should warn when missing required env vars", () => {
const processEnv = Object.assign({}, process.env);
delete processEnv.DB_HOST;
delete processEnv.API_KEY;
const consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation();
checkEnv();
expect(consoleWarnSpy).toHaveBeenCalledWith("❌ Missing env vars: DB_HOST, API_KEY");
consoleWarnSpy.mockRestore();
});
it("should log a message when all required env vars are set", () => {
const processEnv = Object.assign({}, process.env);
checkEnv();
expect(console.log).toHaveBeenCalledWith("✅ All env vars are set");
});
});
` ``;
In;
this;
test;
file, we;
first;
var the = ;
`checkEnv`;
function from() { }
the;
contains;
it.We;
then;
define;
two;
test;
cases: one;
for (when; required; environment)
variables;
are;
missing;
and;
another;
for (when; all; required)
environment;
variables;
are;
set.
;
In;
the;
first;
test;
we;
use `jest.spyOn()`;
to;
mock;
the `console.warn()`;
method;
and;
replace;
it;
with (a)
spy;
function () { }
We;
then;
delete the `DB_HOST`;
and `API_KEY`;
environment;
variables;
and;
call;
the `checkEnv()`;
function () { }
Finally, we;
check;
that;
the `console.warn()`;
method;
was;
called;
with (the)
expected;
message.
;
In;
the;
second;
test;
we;
simply;
call;
the `checkEnv()`;
function without() { }
deleting;
any;
environment;
variables;
and;
check;
that;
the `console.log()`;
method;
was;
called;
with (the)
expected;
message.
;
Note;
that;
we;
use;
the `mockImplementation()`;
method;
to;
mock;
the `console.warn()`;
method;
and;
the `mockRestore()`;
method;
to;
restore;
it;
after;
the;
test;
is;
done.This;
is;
necessary;
because;
Jest;
does;
not;
allow;
us;
to;
modify;
the;
original `console.warn()`;
method.;