Skip to content

Commit

Permalink
fix(tests): update stubs in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
atticusofsparta committed Feb 13, 2024
1 parent b944f4d commit e4bbc6e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 21 deletions.
6 changes: 5 additions & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ module.exports = {
collectCoverageFrom: ['src/**/*.ts', 'tests/**/*.ts'],
testEnvironment: 'node',
testTimeout: 5000,
extensionsToTreatAsEsm: ['.ts'],
transform: {
'^.+\\.(ts|js)$': 'ts-jest',
'^.+\\.(ts|js)$': ['ts-jest', { useESM: true }],
},
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
};
4 changes: 3 additions & 1 deletion src/common/ContractStateProviders/ArNSRemoteCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ export class ArNSRemoteCache implements ContractStateProvider {
async getContractState<ContractState>(
contractId: string,
): Promise<ContractState> {
validateArweaveId(contractId);
if (!validateArweaveId(contractId)) {
throw new BadRequest(`Invalid contract id: ${contractId}`);
}
const contractLogger = this.logger.logger.child({ contractId });
contractLogger.debug(`Fetching contract state`);

Expand Down
8 changes: 2 additions & 6 deletions src/utils/arweave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
*/
import { ARWEAVE_TX_REGEX } from '../constants.js';

export const validateArweaveId = (id: string): void => {
if (!ARWEAVE_TX_REGEX.test(id)) {
throw new Error(
`Invalid contract id: [${id}]. Must be a valid Arweave transaction id.`,
);
}
export const validateArweaveId = (id: string): boolean => {
return ARWEAVE_TX_REGEX.test(id);
};
4 changes: 2 additions & 2 deletions tests/ArIo.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ArIo } from '../src/common/ArIo';
import { ArNSRemoteCache } from '../src/common/ContractStateProviders/ArNSRemoteCache';
import { ArIo } from '../src/common/ArIo.js';
import { ArNSRemoteCache } from '../src/common/ContractStateProviders/ArNSRemoteCache.js';

describe('ArIO Client', () => {
const remoteCacheProvider = new ArNSRemoteCache({});
Expand Down
23 changes: 15 additions & 8 deletions tests/ArNSRemoteCache.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ArIo } from '../src/common/ArIo';
import { ArNSRemoteCache } from '../src/common/ContractStateProviders/ArNSRemoteCache';
import { ArIo } from '../src/common/ArIo.js';
import { ArNSRemoteCache } from '../src/common/ContractStateProviders/ArNSRemoteCache.js';
import { BadRequest } from '../src/common/error.js';

describe('ArIO Client', () => {
const remoteCacheProvider = new ArNSRemoteCache({});
Expand All @@ -8,18 +9,24 @@ describe('ArIO Client', () => {
});

it('should call remote state provider', async () => {
const remoteProvider = new ArNSRemoteCache({});
const client = new ArIo({
contractStateProvider: remoteProvider,
});

const stubGetContractState = jest.fn();
remoteCacheProvider.getContractState = stubGetContractState;
remoteProvider.getContractState = stubGetContractState;
const contractId = ''.padEnd(43, 'a');
await arioClient.getContractState(contractId);
await client.getContractState(contractId);
expect(stubGetContractState).toHaveBeenCalledWith(contractId);
});

it('should call remote state provider and throw on bad contract id', async () => {
const stubGetContractState = jest.fn();
remoteCacheProvider.getContractState = stubGetContractState;
const contractId = ''.padEnd(42, 'a');
await arioClient.getContractState(contractId);
expect(stubGetContractState).toThrow();
const result = await arioClient
.getContractState(contractId)
.catch((e) => e);

expect(result).toBeInstanceOf(BadRequest);
});
});
7 changes: 4 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
{
"compilerOptions": {
"target": "ES6",
"target": "esnext",
"allowSyntheticDefaultImports": true,
"lib": ["esnext", "dom"],
"outDir": "./lib/esm",
"listEmittedFiles": false,
"listFiles": false,
"moduleResolution": "nodenext",
"module": "nodenext",
"moduleResolution": "bundler",
"module": "esnext",
"alwaysStrict": true,
"types": ["node", "jest"],
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"noEmit": false,
/* Additional Checks */
"pretty": true,
"noUnusedLocals": true,
Expand Down

0 comments on commit e4bbc6e

Please sign in to comment.