refactor: Use declarations style for functions.

This commit is contained in:
Ruben Verborgh
2021-01-06 11:24:43 +01:00
parent e70e060225
commit f9a20799eb
23 changed files with 208 additions and 162 deletions

View File

@@ -8,8 +8,8 @@ export const BASE = 'http://test.com';
/**
* Returns a component instantiated from a Components.js configuration.
*/
export const instantiateFromConfig = async(componentUrl: string, configFile: string,
variables?: Record<string, any>): Promise<any> => {
export async function instantiateFromConfig(componentUrl: string, configFile: string,
variables?: Record<string, any>): Promise<any> {
// Initialize the Components.js loader
const mainModulePath = joinFilePath(__dirname, '../../');
const loader = new Loader({ mainModulePath });
@@ -18,15 +18,16 @@ export const instantiateFromConfig = async(componentUrl: string, configFile: str
// Instantiate the component from the config
const configPath = toSystemFilePath(joinFilePath(__dirname, 'config', configFile));
return loader.instantiateFromUrl(componentUrl, configPath, undefined, { variables });
};
}
export const getTestFolder = (name: string): string =>
joinFilePath(__dirname, '../tmp', name);
export function getTestFolder(name: string): string {
return joinFilePath(__dirname, '../tmp', name);
}
export const createFolder = (folder: string): void => {
export function createFolder(folder: string): void {
mkdirSync(folder, { recursive: true });
};
}
export const removeFolder = (folder: string): void => {
export function removeFolder(folder: string): void {
rimraf.sync(folder, { glob: false });
};
}

View File

@@ -29,13 +29,13 @@ class DummyFactory implements FileIdentifierMapperFactory {
}
}
const genToArray = async<T>(iterable: AsyncIterable<T>): Promise<T[]> => {
async function genToArray<T>(iterable: AsyncIterable<T>): Promise<T[]> {
const arr: T[] = [];
for await (const result of iterable) {
arr.push(result);
}
return arr;
};
}
describe('A TemplatedResourcesGenerator', (): void => {
const rootFilePath = 'templates';

View File

@@ -21,13 +21,13 @@ describe('A LockingResourceStore', (): void => {
jest.clearAllMocks();
order = [];
const delayedResolve = (resolve: (resolveParams: any) => void, name: string, resolveParams?: any): void => {
function delayedResolve(resolve: (value: any) => void, name: string, resolveValue?: any): void {
// `setImmediate` is introduced to make sure the promise doesn't execute immediately
setImmediate((): void => {
order.push(name);
resolve(resolveParams);
resolve(resolveValue);
});
};
}
const readable = streamifyArray([ 1, 2, 3 ]);
source = {
@@ -70,14 +70,14 @@ describe('A LockingResourceStore', (): void => {
store = new LockingResourceStore(source, locker);
});
const registerEventOrder = async(eventSource: EventEmitter, event: string): Promise<void> => {
async function registerEventOrder(eventSource: EventEmitter, event: string): Promise<void> {
await new Promise((resolve): any => {
eventSource.prependListener(event, (): any => {
order.push(event);
resolve();
});
});
};
}
it('acquires a lock on the container when adding a representation.', async(): Promise<void> => {
await store.addResource({ path: 'path' }, {} as Representation);

View File

@@ -20,12 +20,12 @@ const { literal, namedNode, quad } = DataFactory;
jest.mock('fetch-sparql-endpoint');
const simplifyQuery = (query: string | string[]): string => {
function simplifyQuery(query: string | string[]): string {
if (Array.isArray(query)) {
query = query.join(' ');
}
return query.replace(/\n/gu, ' ').trim();
};
}
describe('A SparqlDataAccessor', (): void => {
const endpoint = 'http://test.com/sparql';

View File

@@ -65,7 +65,7 @@ describe('A SparqlUpdatePatchHandler', (): void => {
handler = new SparqlUpdatePatchHandler(source, locker);
});
const basicChecks = async(quads: Quad[]): Promise<boolean> => {
async function basicChecks(quads: Quad[]): Promise<boolean> {
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
expect(source.getRepresentation).toHaveBeenLastCalledWith(
{ path: 'path' }, { type: { [INTERNAL_QUADS]: 1 }},
@@ -81,7 +81,7 @@ describe('A SparqlUpdatePatchHandler', (): void => {
expect(setParams[1].metadata.contentType).toEqual(INTERNAL_QUADS);
await expect(arrayifyStream(setParams[1].data)).resolves.toBeRdfIsomorphic(quads);
return true;
};
}
it('only accepts SPARQL updates.', async(): Promise<void> => {
const input = { identifier: { path: 'path' },

View File

@@ -9,14 +9,14 @@ describe('A WrappedExpiringResourceLocker', (): void => {
order = [];
});
const registerEventOrder = async(eventSource: EventEmitter, event: string): Promise<void> => {
async function registerEventOrder(eventSource: EventEmitter, event: string): Promise<void> {
await new Promise((resolve): any => {
eventSource.prependListener(event, (): any => {
order.push(event);
resolve();
});
});
};
}
it('emits an error event when releasing the lock errors.', async(): Promise<void> => {
jest.useFakeTimers();

View File

@@ -199,9 +199,9 @@ export class ResourceHelper {
}
}
export const describeIf = (envFlag: string, name: string, fn: () => void): void => {
export function describeIf(envFlag: string, name: string, fn: () => void): void {
const flag = `TEST_${envFlag.toUpperCase()}`;
const enabled = !/^(|0|false)$/iu.test(process.env[flag] ?? '');
// eslint-disable-next-line jest/valid-describe, jest/valid-title, jest/no-disabled-tests
return enabled ? describe(name, fn) : describe.skip(name, fn);
};
}

View File

@@ -9,13 +9,13 @@ import type { HttpHandler } from '../../src/server/HttpHandler';
import type { HttpRequest } from '../../src/server/HttpRequest';
import type { SystemError } from '../../src/util/errors/SystemError';
export const performRequest = async(
export async function performRequest(
handler: HttpHandler,
requestUrl: URL,
method: string,
headers: IncomingHttpHeaders,
data: string[],
): Promise<MockResponse<any>> => {
): Promise<MockResponse<any>> {
const request = streamifyArray(data) as HttpRequest;
request.url = requestUrl.pathname;
request.method = method;
@@ -36,7 +36,7 @@ export const performRequest = async(
await endPromise;
return response;
};
}
/**
* Mocks (some) functions of the fs system library.
@@ -56,21 +56,21 @@ export const performRequest = async(
* @param rootFilepath - The name of the root folder in which fs will start.
* @param time - The date object to use for time functions (currently only mtime from lstats)
*/
export const mockFs = (rootFilepath?: string, time?: Date): { data: any } => {
export function mockFs(rootFilepath?: string, time?: Date): { data: any } {
const cache: { data: any } = { data: {}};
rootFilepath = rootFilepath ?? 'folder';
time = time ?? new Date();
// eslint-disable-next-line unicorn/consistent-function-scoping
const throwSystemError = (code: string): void => {
function throwSystemError(code: string): void {
const error = new Error('error') as SystemError;
error.code = code;
error.syscall = 'this exists for isSystemError';
throw error;
};
}
const getFolder = (path: string): { folder: any; name: string } => {
function getFolder(path: string): { folder: any; name: string } {
let parts = path.slice(rootFilepath!.length).split('/').filter((part): boolean => part.length > 0);
if (parts.length === 0) {
@@ -91,7 +91,7 @@ export const mockFs = (rootFilepath?: string, time?: Date): { data: any } => {
});
return { folder, name };
};
}
const mock = {
createReadStream(path: string): any {
@@ -171,4 +171,4 @@ export const mockFs = (rootFilepath?: string, time?: Date): { data: any } => {
Object.assign(fs, mock);
return cache;
};
}