# A function that opens and returns the contents of a file and some tests for it

I have the files "locale/{languageCode}.json" saved as part of the project, the reason that I made the read synchronous is because I think it is better that the internationalisation library I am using is initialised with the correct locale (rather than an async read, then dealing with promise resolution and updating the i18n settings with the new data when it arrives).

I plan to move the contents of the file into redis later, to reduce disc I/O, but right now I would like to focus on the usefulness of my tests, and ways this could be improved.

I am stubbing the readFileSync method because I don't think my tests should actually be doing disc I/O, as well as the logging function because I don't think the tests should actually write anything to the log file.

getLocale.ts

import fs from 'fs';
import {logger} from './logging/logger';

export const getLocale = (locale: string) : string => {
const path = ${process.cwd()}/locales/${locale}.json;
try {
}
catch(err) {
logger.error(err);
}
};


getLocale_spec.ts

import {expect} from 'chai';
import {getLocale} from '../../src/server/getLocale';
import sinonTest from 'sinon-test';
import fs from 'fs';
import sinon from 'sinon';
import {logger} from '../../src/server/logging/logger';

const test = sinonTest(sinon);

describe('getLocale', () => {
it('opens the correct locale file', test(function () {
const fsStub = this.stub(fs, 'readFileSync').returns('fake locale text');
const result = getLocale('testLocale');
expect(fsStub.calledWith('testLocale'));
expect(result).to.equal('fake locale text');
}));

it('logs an error when locale file throws during open', test(function () {
const fsStub = this.stub(fs, 'readFileSync').throws();
const loggerStub = this.stub(logger, 'error');
getLocale('testLocale');
expect(fsStub.calledWith('testLocale'));
expect(loggerStub.called).to.be.true;
}));
});