Scope
Library (gpt-turbo)
Type
New feature
Description
I tried implementing some tests, but because GPT Turbo is ESM only with TypeScript, it's surprisingly very hard to even get started with Jest. Unfortunately, I have very small experience with testing with Jest and it's usually in CommonJS front-end environments, so this setup is totally new to me. I've tried for about a week a bunch of solutions but couldn't get it started...
I'd really appreciate it if someone could just get the tests started. I'm not even asking to write them, I just want the following test (or something like it) to work fine when I npm test
. If this works, then I don't mind picking up from there.
The only requirement I have is that tests are in a test
folder, separate from the src
folder.
// packages/library/test/classes/Conversation.test.ts
import { describe, expect, test } from "@jest/globals";
import { Conversation } from "../../src/classes/Conversation.js"; // notice ESM .js extension
import { DEFAULT_CONTEXT } from "../../src/config/constants.js"; // notice ESM .js extension
describe("Conversation", () => {
test("creates a Conversation with default parameters", () => {
const conversation = new Conversation();
expect(conversation.getMessages()).toHaveLength(0);
expect(conversation.getMessages(true)).toHaveLength(1);
const firstMessage = conversation.getMessages(true)[0];
expect(firstMessage.role).toBe("system");
expect(firstMessage.content).toBe(DEFAULT_CONTEXT);
});
});
Notice the use .js
extensions to import .ts
modules. This is the syntax used across the library, since it's the required import syntax when working with ES Modules. The library simply won't compile without it. Removing the .js
extension fixes the issue where Jest is unable to find the module (with .js
, since files are actually .ts
), but then crashes because these modules are actually importing other modules with .js
.
Benefit
Adding tests to the library is crucial as it helps to reinforce the robustness of GPT Turbo. It can also be seen as a good indicator for potential devs willing to work with the library and I believe that it conveys a certain degree of maturity for the project as well.
I rarely add tests to my projects as they are often left unfinished, so I don't want to spend too much time testing something that won't go far enough to be worth the time. In the case of GPT Turbo, I feel like it is in a stable enough state that it can be used in a variety of projects and fulfill its purpose, so it feels right that it gets tested properly.
Participation
Yes