Skip to content

Commit

Permalink
enhanced tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed May 7, 2022
1 parent 9bd0221 commit cc26fdf
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 40 deletions.
2 changes: 2 additions & 0 deletions src/loader/file-type/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export async function loadJsonFile(filePath: string) : Promise<unknown | undefin
const file = await fs.promises.readFile(filePath);
return JSON.parse(file.toString('utf-8'));
} catch (e) {
/* istanbul ignore next */
return undefined;
}
}
Expand All @@ -21,6 +22,7 @@ export function loadJsonFileSync(filePath: string) : unknown | undefined {
const file = fs.readFileSync(filePath);
return JSON.parse(file.toString('utf-8'));
} catch (e) {
/* istanbul ignore next */
return undefined;
}
}
4 changes: 3 additions & 1 deletion src/loader/file-type/script/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ export async function loadScriptFile(data: LocatorInfo |string) : Promise<unknow
try {
return await import(filePath);
} catch (e) {
/* istanbul ignore next */
return undefined;
}
}

export async function loadScriptFileSingleExport(
export async function loadScriptFileExport(
data: LocatorInfo | string,
filterFn?: LoaderFilterFn,
) : Promise<unknown | undefined> {
Expand All @@ -35,6 +36,7 @@ export async function loadScriptFileSingleExport(

return getRecordItem(data, filterFn);
} catch (e) {
/* istanbul ignore next */
return undefined
}
}
4 changes: 3 additions & 1 deletion src/loader/file-type/script/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ export function loadScriptFileSync(data: LocatorInfo | string) : unknown | undef
// eslint-disable-next-line @typescript-eslint/no-var-requires, global-require,import/no-dynamic-require
return require(filePath);
} catch (e) {
/* istanbul ignore next */
return undefined;
}
}

export function loadScriptFileSingleExportSync(
export function loadScriptFileExportSync(
data: LocatorInfo | string,
filterFn?: LoaderFilterFn,
) : unknown | undefined {
Expand All @@ -36,6 +37,7 @@ export function loadScriptFileSingleExportSync(

return getRecordItem(data, filterFn);
} catch (e) {
/* istanbul ignore next */
return undefined
}
}
29 changes: 11 additions & 18 deletions src/loader/file-type/script/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,19 @@ export function getRecordItem(
data: Record<string, any>,
filterFn: LoaderFilterFn
) : unknown | undefined {
if (
!filterFn &&
hasOwnProperty(data, 'default')
) {
return data.default;
}

const keys = Object.keys(data);
for (let i = 0; i < keys.length; i++) {
if (
filterFn &&
filterFn(keys[i], data[keys[i]])
) {
return data[keys[i]];
if (filterFn) {
const keys = Object.keys(data);
for (let i = 0; i < keys.length; i++) {
if (filterFn(keys[i], data[keys[i]])) {
return data[keys[i]];
}
}
} else {
return hasOwnProperty(data, 'default') ?
data.default :
data;
}

if (keys.length > 0) {
return data[keys[0]];
}

/* istanbul ignore next */
return undefined;
}
2 changes: 1 addition & 1 deletion src/locator/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function buildLocatorOptions(options?: Partial<LocatorOptions>) : Locator
return options as LocatorOptions;
}


/* istanbul ignore next */
export function isLocatorInfo(data: unknown) : data is LocatorInfo {
if(typeof data !== 'object') {
return false;
Expand Down
2 changes: 1 addition & 1 deletion test/data/file-default.ts → test/data/file-default-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
*/

export default {
foo: 'bar'
bar: 'baz'
}
9 changes: 9 additions & 0 deletions test/data/file-many-ts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright (c) 2022.
* Author Peter Placzek (tada5hi)
* For the full copyright and license information,
* view the LICENSE file that was distributed with this source code.
*/

export const foo = 'bar';
export const bar = 'baz';
2 changes: 1 addition & 1 deletion test/data/file.ts → test/data/file-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
*/

module.exports = {
foo: 'bar'
bar: 'baz'
}
2 changes: 1 addition & 1 deletion test/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = {
],
coverageThreshold: {
global: {
branches: 80,
branches: 70,
functions: 80,
lines: 80,
statements: 80
Expand Down
42 changes: 30 additions & 12 deletions test/unit/loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


import path from "path";
import {loadScriptFileSingleExport, loadScriptFileSingleExportSync, locateFile, locateFileSync} from "../../src";
import {loadScriptFileExport, loadScriptFileExportSync, locateFile, locateFileSync} from "../../src";
import {loadFile, loadFileSync} from "../../src";

const basePath = path.join(__dirname, '..', 'data');
Expand All @@ -20,7 +20,7 @@ describe('src/loader/**', () => {
expect(loaderContent.default).toBeDefined();
expect(loaderContent.foo).toEqual('bar');

loaderContent = await loadScriptFileSingleExport(locatorInfo);
loaderContent = await loadScriptFileExport(locatorInfo);
expect(loaderContent).toBeDefined();
expect(loaderContent.foo).toEqual('bar');

Expand All @@ -29,36 +29,54 @@ describe('src/loader/**', () => {
locatorInfo = locateFileSync( 'file', {paths: [basePath], extensions: ['.js']});
loaderContent = loadFileSync(locatorInfo);
expect(loaderContent).toBeDefined();
expect(loaderContent.default).toBeUndefined();
expect(loaderContent.foo).toEqual('bar');

loaderContent = loadScriptFileSingleExportSync(locatorInfo);
loaderContent = loadScriptFileExportSync(locatorInfo);
expect(loaderContent).toBeDefined();
expect(loaderContent).toEqual('bar');
expect(loaderContent.default).toBeUndefined();
expect(loaderContent.foo).toEqual('bar');
});

it('should load .ts file', async () => {
let locatorInfo = await locateFile( 'file', {paths: [basePath], extensions: ['.ts']});
let locatorInfo = await locateFile( 'file-ts', {paths: [basePath], extensions: ['.ts']});
let loaderContent : Record<string, any> = await loadFile(locatorInfo);
expect(loaderContent).toBeDefined();
expect(loaderContent.default).toBeDefined();
expect(loaderContent.foo).toEqual('bar');
expect(loaderContent.bar).toEqual('baz');

loaderContent = await loadScriptFileSingleExport(locatorInfo);
loaderContent = await loadScriptFileExport(locatorInfo);
expect(loaderContent).toBeDefined();
expect(loaderContent.foo).toEqual('bar');
expect(loaderContent.bar).toEqual('baz');

// --------------------------------------------------------------------

locatorInfo = locateFileSync( 'file', {paths: [basePath], extensions: ['.ts']});
locatorInfo = locateFileSync( 'file-ts', {paths: [basePath], extensions: ['.ts']});
loaderContent = loadFileSync(locatorInfo);
expect(loaderContent).toBeDefined();
expect(loaderContent.foo).toEqual('bar');
expect(loaderContent.bar).toEqual('baz');

loaderContent = loadScriptFileSingleExportSync(locatorInfo);
loaderContent = loadScriptFileExportSync(locatorInfo);
expect(loaderContent).toBeDefined();
expect(loaderContent).toEqual('bar');
expect(loaderContent.bar).toEqual('baz');
});

it('should filter .ts file', async () => {
let locatorInfo = await locateFile( 'file-many-ts', {paths: [basePath], extensions: ['.ts']});
let loaderContent : Record<string, any> = await loadScriptFileExport(locatorInfo, (key, value) => {
return key === 'bar';
});
expect(loaderContent).toBeDefined();
expect(loaderContent).toEqual('baz');

locatorInfo = locateFileSync( 'file-many-ts', {paths: [basePath], extensions: ['.ts']});
loaderContent = loadScriptFileExportSync(locatorInfo, (key, value) => {
return key === 'bar';
});
expect(loaderContent).toBeDefined();
expect(loaderContent).toEqual('baz');
})

it('should load .json file', async () => {
let locatorInfo = await locateFile( 'file', {paths: [basePath], extensions: ['.json']});
let loaderContent : Record<string, any> = await loadFile(locatorInfo);
Expand Down
8 changes: 4 additions & 4 deletions test/unit/locator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ describe('src/locator.ts', () => {
});

it('should locate .ts file', async () => {
let locatorInfo = await locateFile( 'file', {paths: [basePath], extensions: ['.ts']});
let locatorInfo = await locateFile( 'file-ts', {paths: [basePath], extensions: ['.ts']});
expect(locatorInfo).toBeDefined();
expect(locatorInfo).toEqual({
path: basePath,
fileName: 'file',
fileName: 'file-ts',
fileExtension: '.ts'
} as LocatorInfo);

locatorInfo = locateFileSync( 'file', {paths: [basePath], extensions: ['.ts']});
locatorInfo = locateFileSync( 'file-ts', {paths: [basePath], extensions: ['.ts']});
expect(locatorInfo).toBeDefined();
expect(locatorInfo).toEqual({
path: basePath,
fileName: 'file',
fileName: 'file-ts',
fileExtension: '.ts'
} as LocatorInfo);
});
Expand Down

0 comments on commit cc26fdf

Please sign in to comment.