Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

setupTest.ts 1.0 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  1. import { vi, afterAll } from "vitest";
  2. import dotenv from "dotenv";
  3. import { resolve } from "path";
  4. import fs from "fs";
  5. import { MongoMemoryServer } from "mongodb-memory-server";
  6. let mongoServer: MongoMemoryServer;
  7. // Load the .env file
  8. const envPath = resolve(__dirname, "../.env");
  9. dotenv.config({ path: envPath });
  10. // Read the .env file content
  11. const envContent = fs.readFileSync(envPath, "utf-8");
  12. // Parse the .env content
  13. const envVars = dotenv.parse(envContent);
  14. // Separate public and private variables
  15. const publicEnv = {};
  16. const privateEnv = {};
  17. for (const [key, value] of Object.entries(envVars)) {
  18. if (key.startsWith("PUBLIC_")) {
  19. publicEnv[key] = value;
  20. } else {
  21. privateEnv[key] = value;
  22. }
  23. }
  24. vi.mock("$env/dynamic/public", () => ({
  25. env: publicEnv,
  26. }));
  27. vi.mock("$env/dynamic/private", async () => {
  28. mongoServer = await MongoMemoryServer.create();
  29. return {
  30. env: {
  31. ...privateEnv,
  32. MONGODB_URL: mongoServer.getUri(),
  33. },
  34. };
  35. });
  36. afterAll(async () => {
  37. if (mongoServer) {
  38. await mongoServer.stop();
  39. }
  40. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...