Utils: Fix getServerVersionFromPackageJson

The cache is never null, because it defaults to undefined, and therefore this function always returns undefined.

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-04-15 18:15:25 +02:00 committed by David Mehren
parent 4377376821
commit fa724c2603
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
2 changed files with 28 additions and 1 deletions

View file

@ -0,0 +1,27 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { promises as fs } from 'fs';
import { getServerVersionFromPackageJson } from './serverVersion';
it('getServerVersionFromPackageJson works', async () => {
const major = 2;
const minor = 0;
const patch = 0;
const preRelease = 'dev';
/* eslint-disable @typescript-eslint/require-await*/
jest.spyOn(fs, 'readFile').mockImplementationOnce(async (_) => {
return `{
"version": "${major}.${minor}.${patch}"
}
`;
});
const serverVersion = await getServerVersionFromPackageJson();
expect(serverVersion.major).toEqual(major);
expect(serverVersion.minor).toEqual(minor);
expect(serverVersion.patch).toEqual(patch);
expect(serverVersion.preRelease).toEqual(preRelease);
});

View file

@ -11,7 +11,7 @@ import { join as joinPath } from 'path';
let versionCache: ServerVersion;
export async function getServerVersionFromPackageJson(): Promise<ServerVersion> {
if (versionCache === null) {
if (versionCache === undefined) {
const rawFileContent: string = await fs.readFile(
joinPath(__dirname, '../../package.json'),
{ encoding: 'utf8' },