-
Notifications
You must be signed in to change notification settings - Fork 2
/
load.test.ts
84 lines (69 loc) · 3.05 KB
/
load.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* eslint-disable @typescript-eslint/no-explicit-any */
import { fileExists, load, loadSync } from '../src/load';
import toml from '../src/parsers/toml';
import json5 from '../src/parsers/json5';
import yaml from '../src/parsers/yaml';
describe('the fileExists function', () => {
it('should asynchronously check if a file exists', async () => {
expect(await fileExists('./test/load.test.ts')).toBeTruthy();
expect(await fileExists('./test/load.testa.ts')).toBeFalsy();
});
});
describe('asynchronous file loading', () => {
it('should load files', async () => {
const loaded = await load('example', ['./test'], [toml], false);
expect(loaded).toHaveLength(1);
expect(loaded[0]).toHaveProperty('hello');
expect(loaded[0].hello).toHaveLength(1);
expect(loaded[0].hello).toContain('world');
});
it('should return the first file found if the many parameter is false', async () => {
const loaded = await load('example', ['./test'], [toml, yaml], false);
expect(loaded).toHaveLength(1);
expect(loaded[0]).toHaveProperty('hello');
expect(loaded[0].hello).toHaveLength(1);
expect(loaded[0].hello).toContain('world');
});
it('should return multiple files if the many parameter is true', async () => {
const loaded = await load('example', ['./test'], [toml, yaml], true);
expect(loaded).toHaveLength(2);
expect(loaded[0]).toHaveProperty('hello');
expect(loaded[0].hello).toHaveLength(1);
expect(loaded[0].hello).toContain('world');
expect(loaded[1]).toHaveProperty('hello');
expect(loaded[1].hello).toHaveLength(1);
expect(loaded[1].hello).toContain('hi there');
});
it('should return an empty array if no files are found', async () => {
expect(await load('example', ['./test'], [json5], false)).toHaveLength(0);
});
});
describe('synchronous file loading', () => {
it('should load files', () => {
const loaded = loadSync('example', ['./test'], [toml], false);
expect(loaded).toHaveLength(1);
expect(loaded[0]).toHaveProperty('hello');
expect(loaded[0].hello).toHaveLength(1);
expect(loaded[0].hello).toContain('world');
});
it('should return the first file found if the many parameter is false', () => {
const loaded = loadSync('example', ['./test'], [toml, yaml], false);
expect(loaded).toHaveLength(1);
expect(loaded[0]).toHaveProperty('hello');
expect(loaded[0].hello).toHaveLength(1);
expect(loaded[0].hello).toContain('world');
});
it('should return multiple files if the many parameter is true', () => {
const loaded = loadSync('example', ['./test'], [toml, yaml], true);
expect(loaded).toHaveLength(2);
expect(loaded[0]).toHaveProperty('hello');
expect(loaded[0].hello).toHaveLength(1);
expect(loaded[0].hello).toContain('world');
expect(loaded[1]).toHaveProperty('hello');
expect(loaded[1].hello).toHaveLength(1);
expect(loaded[1].hello).toContain('hi there');
});
it('should return an empty array if no files are found', () => {
expect(loadSync('example', ['./test'], [json5], false)).toHaveLength(0);
});
});