Repository navigation
Expand file tree
/
Copy pathjson-reader.test.js
More file actions
58 lines (53 loc) · 1.85 KB
/
Copy pathjson-reader.test.js
File metadata and controls
58 lines (53 loc) · 1.85 KB
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
import { test } from 'node:test';
import assert from 'node:assert/strict';
import fs from 'fs/promises';
import path from 'path';
import os from 'os';
import readJsonFile from './json-reader.js';
import defaultOptions from './default.js';
async function inTempDir(fn) {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'semantic-react-'));
const cwd = process.cwd();
process.chdir(dir);
try {
await fn(dir);
} finally {
process.chdir(cwd);
await fs.rm(dir, { recursive: true, force: true });
}
}
test('returns the defaults when the settings file is missing', async () => {
await inTempDir(async () => {
const paths = await readJsonFile('./semantic-react.settings.json');
assert.deepEqual(paths, defaultOptions);
});
});
test('overrides only the keys present in the settings file', async () => {
await inTempDir(async () => {
await fs.writeFile(
'./semantic-react.settings.json',
JSON.stringify({ components: 'src/components' })
);
const paths = await readJsonFile('./semantic-react.settings.json');
assert.equal(paths.components, 'src/components');
assert.equal(paths.helpers, defaultOptions.helpers);
assert.equal(paths.hooks, defaultOptions.hooks);
});
});
test('ignores unknown keys in the settings file', async () => {
await inTempDir(async () => {
await fs.writeFile(
'./semantic-react.settings.json',
JSON.stringify({ hooks: 'src/hooks', somethingElse: 'nope' })
);
const paths = await readJsonFile('./semantic-react.settings.json');
assert.equal(paths.hooks, 'src/hooks');
assert.equal(paths.somethingElse, undefined);
});
});
test('throws on malformed JSON', async () => {
await inTempDir(async () => {
await fs.writeFile('./semantic-react.settings.json', '{ not valid json');
await assert.rejects(readJsonFile('./semantic-react.settings.json'));
});
});