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

test: add unit test and integration test setup #13

Merged
merged 1 commit into from
Dec 20, 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
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.

Loading