Skip to content

Commit

Permalink
chore: Adjust jest for esm and monorepo (#57)
Browse files Browse the repository at this point in the history
* add jest node optino

* move test-util to src

* fix: Changes from lint

* fix

* fix

* fix path

* tests fix

* fix

* lock file

---------

Co-authored-by: cloud-sdk-js <[email protected]>
  • Loading branch information
deekshas8 and cloud-sdk-js authored Aug 8, 2024
1 parent 58894ef commit 63096c3
Show file tree
Hide file tree
Showing 17 changed files with 246 additions and 54 deletions.
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

0 comments on commit 63096c3

Please sign in to comment.