Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Adjust jest for esm and monorepo #57

Merged
merged 11 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"scripts": {
"postinstall": "pnpm compile",
"compile": "pnpm -r -w=false run compile",
"test": "pnpm -r run test",
"test": "NODE_OPTIONS=--experimental-vm-modules pnpm -r run test",
"test:type": "tsd",
"lint": "pnpm -r run lint",
"lint:fix": "pnpm -r run lint:fix"
Expand All @@ -26,6 +26,7 @@
"@sap-cloud-sdk/http-client": "^3.18.0",
"@types/jest": "^29.5.12",
"@types/node": "^20.14.14",
"@jest/globals": "^29.5.12",
"eslint": "^9.8.0",
"jest": "^30.0.0-alpha.3",
"nock": "^13.5.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/ai-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
],
"scripts": {
"compile": "tsc",
"test": "jest",
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
"lint": "eslint . && prettier . --config ../../.prettierrc --ignore-path ../../.prettierignore -c",
"lint:fix": "eslint . --fix && prettier . --config ../../.prettierrc --ignore-path ../../.prettierignore -w --log-level error"
},
Expand Down
4 changes: 1 addition & 3 deletions packages/gen-ai-hub/jest.config.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import config from '../../jest.config.mjs';
const aiCoreConfig = {
export default {
...config,
displayName: 'gen-ai-hub',
};

export default aiCoreConfig;
2 changes: 1 addition & 1 deletion packages/gen-ai-hub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
],
"scripts": {
"compile": "tsc",
"test": "jest",
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
"lint": "eslint . && prettier . --config ../../.prettierrc --ignore-path ../../.prettierignore -c",
"lint:fix": "eslint . --fix && prettier . --config ../../.prettierrc --ignore-path ../../.prettierignore -w --log-level error",
"generate": "openapi-generator -i ./src/orchestration/spec/ -o ./src/orchestration/client"
Expand Down
19 changes: 13 additions & 6 deletions packages/gen-ai-hub/src/client/openai/openai-client.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import nock from 'nock';
import { jest } from '@jest/globals';
import { HttpDestination } from '@sap-cloud-sdk/connectivity';
import { mockGetAiCoreDestination } from '../../../test-util/mock-context.js';
import { mockGetAiCoreDestination } from '../../test-util/mock-context.js';
import {
BaseLlmParametersWithDeploymentId,
EndpointOptions
} from '../../core/http-client.js';
import {
mockInference,
parseMockResponse
} from '../../../test-util/mock-http.js';
import { OpenAiClient } from './openai-client.js';
import { mockInference, parseMockResponse } from '../../test-util/mock-http.js';
import {
OpenAiChatCompletionOutput,
OpenAiChatCompletionParameters,
OpenAiChatMessage,
OpenAiEmbeddingOutput,
OpenAiEmbeddingParameters
} from './openai-types.js';
jest.unstable_mockModule('../../core/context.js', () => ({
getAiCoreDestination: jest.fn(() =>
Promise.resolve(mockGetAiCoreDestination())
)
}));
const { OpenAiClient } = await import('./openai-client.js');

describe('openai client', () => {
let destination: HttpDestination;
Expand Down Expand Up @@ -44,6 +47,10 @@ describe('openai client', () => {
nock.cleanAll();
});

afterAll(() => {
jest.restoreAllMocks();
});

describe('chatCompletion', () => {
it('parses a successful response', async () => {
const prompt = {
Expand Down
2 changes: 1 addition & 1 deletion packages/gen-ai-hub/src/core/context.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
mockAiCoreEnvVariable,
mockClientCredentialsGrantCall
} from '../../test-util/mock-context.js';
} from '../test-util/mock-context.js';
import { getAiCoreDestination } from './context.js';

describe('context', () => {
Expand Down
17 changes: 14 additions & 3 deletions packages/gen-ai-hub/src/core/http-client.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { jest } from '@jest/globals';
import { HttpDestination } from '@sap-cloud-sdk/connectivity';
import { mockGetAiCoreDestination } from '../../test-util/mock-context.js';
import { mockInference } from '../../test-util/mock-http.js';
import { executeRequest } from './http-client.js';
import { mockGetAiCoreDestination } from '../test-util/mock-context.js';
import { mockInference } from '../test-util/mock-http.js';

jest.unstable_mockModule('./context.js', () => ({
getAiCoreDestination: jest.fn(() =>
Promise.resolve(mockGetAiCoreDestination())
)
}));
const { executeRequest } = await import('./http-client.js');

describe('http-client', () => {
let destination: HttpDestination;
Expand All @@ -10,6 +17,10 @@ describe('http-client', () => {
destination = mockGetAiCoreDestination();
});

afterAll(() => {
jest.restoreAllMocks();
});

it('should execute a request to the AI Core service', async () => {
const mockPrompt = { prompt: 'some test prompt' };
const mockPromptResponse = { completion: 'some test completion' };
Expand Down
24 changes: 16 additions & 8 deletions packages/gen-ai-hub/src/orchestration/orchestration-client.test.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
import nock from 'nock';
import { jest } from '@jest/globals';
import { HttpDestination } from '@sap-cloud-sdk/connectivity';
import { mockGetAiCoreDestination } from '../../test-util/mock-context.js';
import { mockInference, parseMockResponse } from '../../test-util/mock-http.js';
import { mockGetAiCoreDestination } from '../test-util/mock-context.js';
import { mockInference, parseMockResponse } from '../test-util/mock-http.js';
import { BaseLlmParametersWithDeploymentId } from '../core/index.js';
import {
GenAiHubClient,
constructCompletionPostRequest
} from './orchestration-client.js';
import { CompletionPostResponse } from './api/index.js';
import { GenAiHubCompletionParameters } from './orchestration-types.js';
jest.unstable_mockModule('../core/context.js', () => ({
getAiCoreDestination: jest.fn(() =>
Promise.resolve(mockGetAiCoreDestination())
)
}));

const { GenAiHubClient, constructCompletionPostRequest } = await import(
'./orchestration-client.js'
);
describe('GenAiHubClient', () => {
let destination: HttpDestination;
let client: GenAiHubClient;
const client = new GenAiHubClient();
const deploymentConfiguration: BaseLlmParametersWithDeploymentId = {
deploymentId: 'deployment-id'
};

beforeAll(() => {
destination = mockGetAiCoreDestination();
client = new GenAiHubClient();
});

afterEach(() => {
nock.cleanAll();
});

afterAll(() => {
jest.restoreAllMocks();
});

it('calls chatCompletion with minimum configuration and parses response', async () => {
const request: GenAiHubCompletionParameters = {
deploymentConfiguration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
ServiceCredentials
} from '@sap-cloud-sdk/connectivity';
import nock from 'nock';
import * as context from '../src/core/context.js';

export const aiCoreServiceBinding = {
label: 'aicore',
Expand Down Expand Up @@ -54,11 +53,6 @@ export function mockGetAiCoreDestination(
authentication: 'OAuth2ClientCredentials',
...createDestinationTokens()
};

jest
.spyOn(context, 'getAiCoreDestination')
.mockResolvedValue(mockDestination);

return mockDestination;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
BaseLlmParameters,
CustomRequestConfig,
EndpointOptions
} from '../src/core/http-client.js';
} from '../core/http-client.js';

const mockEndpoint: EndpointOptions = {
url: 'mock-endpoint',
Expand Down Expand Up @@ -44,7 +44,7 @@ export function mockInference<D extends BaseLlmParameters>(

export function parseMockResponse<T>(client: string, fileName: string): T {
const fileContent = fs.readFileSync(
path.join('test-util', 'mock-data', client, fileName),
path.join('src', 'test-util', 'mock-data', client, fileName),
'utf-8'
);

Expand Down
Loading
Loading