Skip to content

Commit

Permalink
test: add unit test and integration test setup (#13)
Browse files Browse the repository at this point in the history
Adds integration test setup and a unit test over the client.
  • Loading branch information
malandis authored Dec 20, 2024
1 parent 23dbfe6 commit 72ff47d
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ jobs:
node: [16, 18, 20]
name: Test on Node ${{ matrix.node }}
runs-on: ubuntu-latest
env:
MOMENTO_API_KEY_REGION_1: ${{ secrets.ALPHA_TEST_AUTH_TOKEN }}
MOMENTO_API_KEY_REGION_2: ${{ secrets.ALPHA_TEST_AUTH_TOKEN }}

steps:
- name: Setup repo
Expand All @@ -46,3 +49,5 @@ jobs:
- name: Lint
run: npm run lint

- name: Test
run: npm run test
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
"scripts": {
"prebuild": "eslint . --ext .ts",
"test": "jest --maxWorkers 1",
"integration-test": "jest integration --maxWorkers 1",
"unit-test": "jest unit",
"lint": "eslint . --ext .ts",
"format": "eslint . --ext .ts --fix",
"watch": "tsc -w",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export {
DefaultMomentoLoggerFactory,
DefaultMomentoLogger,
DefaultMomentoLoggerLevel,
InvalidArgumentError,
MomentoLogger,
MomentoLoggerFactory,
NoopMomentoLogger,
Expand Down
17 changes: 17 additions & 0 deletions test/client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {
Configurations,
InvalidArgumentError,
MultiRegionCacheWriterClient,
} from '../src';

describe('client-setup', () => {
it('should throw an exception when no regions are provided', () => {
expect(() => {
new MultiRegionCacheWriterClient({
credentialProviders: {},
configuration: Configurations.Laptop.latest(),
defaultTtlSeconds: 60,
});
}).toThrow(InvalidArgumentError);
});
});
82 changes: 82 additions & 0 deletions test/integration-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import {v4} from 'uuid';
import {CacheClientProps} from '@gomomento/sdk/dist/src/cache-client-props';
import {
CacheClient,
CacheConfiguration,
Configurations,
CreateCache,
CredentialProvider,
DeleteCache,
MomentoErrorCode,
} from '@gomomento/sdk';
import {
IMultiRegionCacheWriterClient,
MultiRegionCacheWriterClient,
} from '../src';

export function testCacheName(): string {
const name = process.env.CACHE_NAME || 'js-integration-test-default';
return name + v4();
}

function regionalMomentoClientForTesting(regionEnvVarName: string) {
const configuration: CacheConfiguration = Configurations.Laptop.latest();
const IntegrationTestCacheClientProps: CacheClientProps = {
configuration,
credentialProvider: CredentialProvider.fromEnvironmentVariable({
environmentVariableName: regionEnvVarName,
}),
defaultTtlSeconds: 60,
};
return new CacheClient(IntegrationTestCacheClientProps);
}

export function SetupIntegrationTest(): {
client: IMultiRegionCacheWriterClient;
cacheName: string;
} {
const cacheName = testCacheName();
const regionEnvVarNames = [
'MOMENTO_API_KEY_REGION_1',
'MOMENTO_API_KEY_REGION_2',
];

beforeAll(async () => {
for (const regionEnvVarName of regionEnvVarNames) {
const momento = regionalMomentoClientForTesting(regionEnvVarName);
const createResponse = await momento.createCache(cacheName);
if (createResponse instanceof CreateCache.Error) {
if (
createResponse.errorCode() !==
MomentoErrorCode.CACHE_ALREADY_EXISTS_ERROR
) {
throw createResponse.innerException();
}
}
}
});

afterAll(async () => {
for (const regionEnvVarName of regionEnvVarNames) {
const momento = regionalMomentoClientForTesting(regionEnvVarName);
const deleteResponse = await momento.deleteCache(cacheName);
if (deleteResponse instanceof DeleteCache.Error) {
throw deleteResponse.innerException();
}
}
});

const client = new MultiRegionCacheWriterClient({
credentialProviders: {
'region-1': CredentialProvider.fromEnvVar('MOMENTO_API_KEY_REGION_1'),
'region-2': CredentialProvider.fromEnvVar('MOMENTO_API_KEY_REGION_2'),
},
configuration: Configurations.Laptop.latest(),
defaultTtlSeconds: 60,
});

return {
client,
cacheName,
};
}
1 change: 0 additions & 1 deletion test/test.ts

This file was deleted.

0 comments on commit 72ff47d

Please sign in to comment.