Package Exports
- @testyourai/jest
- @testyourai/jest/src/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@testyourai/jest) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@testyourai/jest
This is a set of Jest matchers that use OpenAI embeddings to test for semantic similarities between strings. With these matchers, you can easily test if two strings are similar in meaning or compare the similarity of one string to multiple others.
Installation
You can install this package using your favourite npm client:
npm install --save-dev @testyourai/jestpnpm add -D @testyourai/jestyarn add -D @testyourai/jestUsage
To use these matchers, simply import the setupMatchers function and pass the output to expect.extend in your setup file or before your tests. Then simply use them with Jest's built-in expect function.
import {setupMatchers} from "@testyourai/jest";
import {expect} from "@jest/globals";
// Setup your matchers before your tests run
expect.extend(setupMatchers({
openAiApiKey: process.env.OPENAI_API_KEY, // required
model: "text-embedding-ada-002" // optional
}));describe('String similarity tests', () => {
test('Hello -> Hi', async () => {
await expect('Hello').toBeSimilarTo('Hi');
});
test('Hello -> Yellow', async () => {
await expect('Hello').toBeSimilarTo('Yellow', 0.8);
});
test('Hello is more similar to Hi than Whistle or Lion', async () => {
await expect('Hello').toBeMoreSimilarTo('Hi', ['Whistle', 'Lion']);
});
});Matchers
toBeSimilarTo(expected: string, threshold?: number)
Checks if the tested string is similar in meaning to the expected string. The optional threshold parameter sets the minimum similarity score required for the test to pass (default is 0.7).
Example:
test('Hello -> Hi', async () => {
await expect('Hello').toBeSimilarTo('Hi');
});toBeMoreSimilarTo(expected: string, others: string[] | string)
Checks if the tested string is more similar in meaning to the expected string than it is to any of the other strings provided. The others parameter can be a single string or an array of strings.
Example:
test('Hello is more similar to Hi than Whistle or Lion', async () => {
await expect('Hello').toBeMoreSimilarTo('Hi', ['Whistle', 'Lion']);
});
Contributing
Contributions are welcome! If you find a bug or have a feature request, please open an issue on the GitHub repository. If you'd like to contribute code, please fork the repository and submit a pull request.
- Clone this repository
- Install dependencies
pnpm install - Copy
.env.exampleto.envfile and supply yourOPENAI_API_KEY - Run tests by
pnpm test