Skip to content

Commit

Permalink
More jest fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MatKuhr committed Aug 5, 2024
1 parent c629e13 commit 37d4bd5
Show file tree
Hide file tree
Showing 18 changed files with 212 additions and 40 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@sap-cloud-sdk/connectivity": "^3.17.0",
"@sap-cloud-sdk/http-client": "^3.17.0",
"@types/jest": "^29.5.12",
"@jest/globals": "^29.5.12",
"@types/node": "^20.14.14",
"eslint": "^9.8.0",
"jest": "^30.0.0-alpha.3",
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
3 changes: 1 addition & 2 deletions packages/gen-ai-hub/jest.config.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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
4 changes: 2 additions & 2 deletions packages/gen-ai-hub/src/client/openai/openai-client.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import nock from 'nock';
import { HttpDestination } from '@sap-cloud-sdk/connectivity';
import { mockGetAiCoreDestination } from '../../../test-util/mock-context.js';
import { mockGetAiCoreDestination } from '../../test-util/mock-context.js';
import {
EndpointOptions
} from '../../core/http-client.js';
import {
mockInference,
parseMockResponse
} from '../../../test-util/mock-http.js';
} from '../../test-util/mock-http.js';
import { AiDeployment } from '../../core/aicore.js';
import { OpenAiClient } from './openai-client.js';
import {
Expand Down
6 changes: 3 additions & 3 deletions packages/gen-ai-hub/src/core/aicore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { resolveDeployment } from './aicore';

describe('AICore', () => {
const destination: HttpDestination = {
url: '/'
url: 'http://foo.com/'
};

it('should be defined', () => {
nock('/v2')
.post(
nock('http://foo.com')
.get(
'/lm/deployments'
)
.query({ 'scenarioId': 'foundation-models', 'status': 'RUNNING' })
Expand Down
10 changes: 9 additions & 1 deletion packages/gen-ai-hub/src/core/aicore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,19 @@ export async function resolveDeployment(opts: { scenarioId: string; executableId
destination = await getAiCoreDestination();
}

// TODO: is there a more elegant way to write this in TS?
let query: any;
if (opts.executableId) {
query = { scenarioId: opts.scenarioId, status: 'RUNNING', executableIds: [opts.executableId] }
} else {
query = { scenarioId: opts.scenarioId, status: 'RUNNING' }
}

// TODO: add a cache
let deploymentList: Deployment[];
try {
deploymentList = await DeploymentApi
.deploymentQuery({ scenarioId: opts.scenarioId, status: 'RUNNING', executableIds: opts.executableId ? [opts.executableId] : undefined }, { 'AI-Resource-Group': 'default' })
.deploymentQuery(query, { 'AI-Resource-Group': 'default' })
.execute(destination).then((res) => res.resources);
} catch (error) {
throw new Error('Failed to fetch the list of deployments: ' + error);
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
4 changes: 2 additions & 2 deletions packages/gen-ai-hub/src/core/http-client.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpDestination } from '@sap-cloud-sdk/connectivity';
import { mockGetAiCoreDestination } from '../../test-util/mock-context.js';
import { mockInference } from '../../test-util/mock-http.js';
import { mockGetAiCoreDestination } from '../test-util/mock-context.js';
import { mockInference } from '../test-util/mock-http.js';
import { executeRequest } from './http-client.js';

describe('http-client', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import nock from 'nock';
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 { AiDeployment } from '../core/aicore.js';
import { EndpointOptions } from '../core/http-client.js';
import {
Expand Down Expand Up @@ -47,7 +47,7 @@ describe('OrchestrationService', () => {

mockInference({
request:{
data: mockResponse,
data: request,
destination,
endpoint: mockEndpoint
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import {
ServiceCredentials
} from '@sap-cloud-sdk/connectivity';
import nock from 'nock';
import * as context from '../src/core/context.js';

import { jest } from '@jest/globals';

jest.mock('../core/context.js')
import {getAiCoreDestination} from '../core/context.js';


export const aiCoreServiceBinding = {
label: 'aicore',
Expand Down Expand Up @@ -55,9 +60,7 @@ export function mockGetAiCoreDestination(
...createDestinationTokens()
};

jest
.spyOn(context, 'getAiCoreDestination')
.mockResolvedValue(mockDestination);
(getAiCoreDestination as any).mockReturnValue(Promise.resolve(mockDestination));

return mockDestination;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import nock from 'nock';
import { removeLeadingSlashes } from '@sap-cloud-sdk/util';
import {
EndpointOptions
} from '../src/core/http-client.js';
} from '../core/http-client.js';

export function mockInference(stub: {
request: {
Expand Down
Loading

0 comments on commit 37d4bd5

Please sign in to comment.