All files jsonc_test.ts

100% Statements 30/30
100% Branches 0/0
100% Functions 6/6
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 361x 1x 1x 1x 1x 1x 3x   1x             1x 1x 2x       2x 1x 1x 1x   1x 1x 2x 1x 1x 1x 1x      
import * as fs from 'fs-extra';
import * as path from 'path';
import * as mock from 'mock-fs';
import {JSONC} from './jsonc';
import {deepStrictEqual, strictEqual} from 'assert';
const cwd = process.cwd();
const here = (filename: string) => path.join(cwd, filename);
 
const content = `
// example
{
  "name": "thomas"
}
`;
 
describe('JSONC', () => {
  beforeEach(() =>
    mock({
      [here('hello.json')]: content,
    })
  );
  afterEach(() => mock.restore());
  it('should get parsedContent', async () => {
    const jsonFile = new JSONC({basename: 'hello.json'});
    deepStrictEqual(await jsonFile.parsedContent, {name: 'thomas'});
  });
  it('should set property and retain comment', async () => {
    const jsonFile = new JSONC({basename: 'hello.json'});
    await jsonFile.set('love', true);
    const result = {name: 'thomas', love: true};
    const commentResult = `// example\n${JSON.stringify(result, null, 2)}`;
    strictEqual(fs.readFileSync(here('hello.json'), 'utf8'), commentResult);
    deepStrictEqual(await jsonFile.parsedContent, result);
  });
});