Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
troubleshooting test environment
Browse files Browse the repository at this point in the history
I was running into issues where jest wasn't defined. Moving it to __test__ directory seemed to resolve the issue.

I also added jest-fetch-mock, which resolved some mocking issues. I might remove it.
  • Loading branch information
pBread committed Jan 16, 2024
1 parent c580a77 commit 69bcc3b
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 20 deletions.
27 changes: 27 additions & 0 deletions tests/client.test.js → __tests__/client.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import MistralClient from "../dist/client.module.js";
import fetchMock from "jest-fetch-mock";

beforeEach(() => {
fetch.resetMocks();
});

describe("MistralClient Constructor", () => {
it("should initialize with default values if no arguments are provided", () => {
Expand Down Expand Up @@ -39,3 +44,25 @@ describe("MistralClient Constructor", () => {
}).toThrow("MistralClient was not provided a valid API key");
});
});

describe("MistralClient", () => {
it("chat() sends a request and handles response correctly", async () => {
const mockApiKey = "test-api-key";
const mockEndpoint = "https://mock.api";
const mockModel = "mistral-tiny";
const mockMessages = [{ role: "user", content: "Hello, world!" }];

// Mocking the fetch response
fetchMock.mockResponseOnce(JSON.stringify({ success: true }));

const client = new MistralClient(mockApiKey, mockEndpoint);

await client.chat({
model: mockModel,
messages: mockMessages,
});

// Checking if the fetch was called correctly
expect(fetch).toHaveBeenCalledTimes(1);
});
});
64 changes: 45 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@
"build": "microbundle",
"dev": "microbundle watch",
"lint": "./node_modules/.bin/eslint .",
"test": "npm run build && node --experimental-vm-modules node_modules/.bin/jest"
"test": "node --experimental-vm-modules node_modules/.bin/jest"
},
"jest": {
"automock": false,
"setupFiles": [
"./scripts/setupJest.js"
],
"testPathIgnorePatterns": [
"examples"
]
Expand All @@ -38,6 +42,7 @@
"eslint": "^8.55.0",
"eslint-config-google": "^0.14.0",
"jest": "^29.7.0",
"jest-fetch-mock": "^3.0.3",
"microbundle": "^0.15.1",
"prettier": "2.8.8",
"typescript": "^5.3.3"
Expand Down
6 changes: 6 additions & 0 deletions scripts/setupJest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// adds the 'fetchMock' global variable and rewires 'fetch' global to call 'fetchMock' instead of the real implementation
import jestMock from "jest-fetch-mock";
jestMock.enableMocks();

// changes default behavior of fetchMock to use the real 'fetch' implementation and not mock responses
fetchMock.dontMock();

0 comments on commit 69bcc3b

Please sign in to comment.