Skip to content

Commit

Permalink
refactor: enable strict type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs committed Mar 9, 2024
1 parent 0e1e412 commit da3b3b4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
31 changes: 16 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ const unrtfPathRegex = /(.+)unrtf/u;
// UnRTF version output is inconsistent between versions but always starts with the semantic version number
const unrtfVersionRegex = /^(\d{1,2}\.\d{1,2}\.\d{1,2})/u;

/** @typedef {{[key: string]: {arg: string, type: string, minVersion: string, maxVersion?: string}}} UnRTFAcceptedOptions */

/**
* @author Frazer Smith
* @description Checks each option provided is valid, of the correct type, and can be used by specified
* version of binary.
* @ignore
* @param {object} acceptedOptions - Object containing accepted options.
* @param {object} options - Object containing options to pass to binary.
* @param {string} [version] - Semantic version of binary.
* @param {UnRTFAcceptedOptions} acceptedOptions - Object containing accepted options.
* @param {{[key: string]: any}} options - Object containing options to pass to binary.
* @param {string} version - Semantic version of binary.
* @returns {string[]} Array of CLI arguments.
* @throws If invalid arguments provided.
*/
Expand All @@ -48,23 +50,14 @@ function parseOptions(acceptedOptions, options, version) {
);
}

if (
acceptedOptions[key].minVersion &&
version &&
lt(version, acceptedOptions[key].minVersion)
) {
if (lt(version, acceptedOptions[key].minVersion)) {
invalidArgs.push(
`Invalid option provided for the current version of the binary used. '${key}' was introduced in v${acceptedOptions[key].minVersion}, but received v${version}`
);
}

/* istanbul ignore next: requires incredibly old version of UnRTF to test */
if (
acceptedOptions[key].maxVersion &&
version &&
// @ts-ignore: type checking is done above
gt(version, acceptedOptions[key].maxVersion)
) {
if (gt(version, acceptedOptions[key].maxVersion || version)) {
invalidArgs.push(
`Invalid option provided for the current version of the binary used. '${key}' is only present up to v${acceptedOptions[key].maxVersion}, but received v${version}`
);
Expand Down Expand Up @@ -115,6 +108,7 @@ class UnRTF {
}
}

/* istanbul ignore next: unable to test due to https://github.com/jestjs/jest/pull/14297 */
if (!this.unrtfPath) {
throw new Error(
`Unable to find ${process.platform} UnRTF binaries, please pass the installation directory as a parameter to the UnRTF instance.`
Expand All @@ -132,7 +126,12 @@ class UnRTF {
/** @type {string|undefined} */
this.unrtfVersion = unrtfVersionRegex.exec(version)?.[1];

/** @type {object} */
/* istanbul ignore next: unable to test due to https://github.com/jestjs/jest/pull/14297 */
if (!this.unrtfVersion) {
throw new Error(`Unable to determine UnRTF version.`);
}

/** @type {UnRTFAcceptedOptions} */
this.unrtfAcceptedOptions = {
noPictures: {
arg: "--nopict",
Expand Down Expand Up @@ -222,6 +221,7 @@ class UnRTF {
const args = parseOptions(
this.unrtfAcceptedOptions,
options,
// @ts-ignore: unrtfVersion is set in constructor and will throw if not set
this.unrtfVersion
);
args.push(normalizeTrim(file));
Expand All @@ -247,6 +247,7 @@ class UnRTF {
} else if (stdErr === "") {
reject(
new Error(
// @ts-ignore: Second operand used if code is not in errorMessages
errorMessages[code] ||
`unrtf ${args.join(
" "
Expand Down
30 changes: 30 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,36 @@ describe("Constructor", () => {
);
}
});

/**
* @todo Fix this test, mocking of "node:" scheme not supported yet.
* @see {@link https://github.com/jestjs/jest/pull/14297 | Jest PR #14297}
*/
// eslint-disable-next-line jest/no-disabled-tests -- Blocked by Jest PR #14297
it.skip("Throws an Error if the version of the binary cannot be determined", () => {
// Ensure the mock is used by the UnRTF constructor
jest.resetModules();
jest.mock("node:child_process", () => ({
spawnSync: jest.fn(() => ({
stdout: {
toString: () => "/usr/bin/unrtf",
},
stderr: {
toString: () => "",
},
})),
}));
require("node:child_process");
const { UnRTF: UnRTFMock } = require("./index");

expect.assertions(1);
try {
// eslint-disable-next-line no-unused-vars -- This is intentional
const unRtf = new UnRTFMock();
} catch (err) {
expect(err.message).toBe("mock error");
}
});
});

describe("Convert function", () => {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"moduleResolution": "NodeNext",
"outDir": "types",
"resolveJsonModule": true,
"strict": true,
"target": "ES2022"
},
"include": ["src/index.js"]
Expand Down

0 comments on commit da3b3b4

Please sign in to comment.