A handful of helpful utility functions.
# with npm
npm install @noshot/utils react react-dom
# or with yarn
yarn add @noshot/utils react react-dom
A list of functions that are exported from this package.
This asynchronous function utilizes terser to compress an array of files with optional minify options.
Dependencies: terser
Arguments (2):
files: Array<string>,
opts?: MinifyOptions
Example:
import { compressFiles } from "@noshot/utils";
// import compressFiles from "@noshot/utils/compressFiles";
(async(): Promise<void> => {
const dirs = [
"folder1",
"folder2",
"folder3"
].map(file => `${file ? `${file}/` : ""}index.js`);
await compressFiles(dirs);
process.exit(0);
})();
This synchronous function utilizes node's fs API to check if a file exists.
Dependencies: (none if using node v12+)
Arguments (1):
file: string
Example:
import { getFilePath, fileExists } from "@noshot/utils";
// import getFilePath from "@noshot/utils/getFilePath";
// import fileExists from "@noshot/utils/fileExists";
((): void => {
const path = getFilePath("hello.js") // process.cwd() + hello.js
const result = fileExists(path);
console.log(typeof result, result) // boolean, true/false
process.exit(0);
})();
This synchronous function utilizes node's path API to join a directory path with a file name.
Dependencies: (none if using node v12+)
Arguments (2):
file: string,
dir?: string
Example:
import { getFilePath } from "@noshot/utils";
// import getFilePath from "@noshot/utils/getFilePath";
((): void => {
const path = getFilePath("hello.js") // process.cwd() + hello.js
console.log(typeof path, path) // string, "path/to/project/hello.js"
const customPath = getFilePath("hello.js", "src"); // src + hello.js (path starts from project directory)
console.log(typeof customPath, customPath) // string, "path/to/project/src/hello.js"
process.exit(0);
})();
This asynchronous function utilizes rimraf to remove an array of files with optional rimraf options.
Dependencies: rimraf
Arguments (2):
files: Array<string>,
opts?: rimraf.Options
Example:
import { removeFiles } from "@noshot/utils";
// import removeFiles from "@noshot/utils/removeFiles";
(async(): Promise<void> => {
const dirs = [
"folder1",
"folder2",
"folder3"
].map(file => `${file ? `${file}/` : ""}index.js`);
await removeFiles(dirs);
process.exit(0);
})();
This asynchronous function waits for promisified expectations to resolve. It works by trying to resolve the callback every 50ms within the specified timeout; otherwise, it rejects the promise by throwing an error. Useful for tests that may take an unknown amount of time to resolve.
Dependencies: (none if using node v12+)
Arguments (2):
callback: Function,
timeout?: string // default 1000ms
Example:
import { waitFor } from "@noshot/utils";
// import waitFor from "@noshot/utils/waitFor";
it("waits for an asynchronous expectation to succeed", async () => {
await waitFor(() => {
expect(result).toEqual("hi");
});
});
This asynchronous function utilizes ReactDOM to wait for promisified expectations wrapped in act
to resolve. It works by trying to resolve the callback every 50ms within the specified timeout; otherwise, it rejects the promise by throwing an error. Useful for React tests where expectations may take an unknown amount of time to resolve. This function should work with any testing suite.
Dependencies: React, ReactDOM
Arguments (2):
callback: Function,
timeout?: string // default 1000ms
Example:
import * as React from "react";
import { waitForAct } from "@noshot/utils";
// import waitForAct from "@noshot/utils/waitForAct";
import { mount } from "enzyme"
import Example from "../index";
const wrapper = mount(<Example />);
it("waits for an asynchronous expectation to succeed", async () => {
await waitForAct(() => {
wrapper.update();
expect(wrapper.find(".example").exists()).toBeTruthy();
});
});
As of Node v12.17.0+, node removed the experimental flag for ES modules. Unfortunately, most of development world has yet to adopt ESM as the standard. Therefore, until there's more widespread support, this documentation will caution against using ESM and instead opt for CJS. In addition, node doesn't support preloading ESM, since preloading utilizes Node's require
function. That said, this package offers experimental support for ESM. You can try it out by importing from the esm
directory of the package:
import utils from "@noshot/utils/esm";
// import { compressFiles, fileExists, getFilePath, ...etc } from "@noshot/utils/esm";
// import compressFiles from "@noshot/utils/esm/compressFiles";
See CONTRIBUTING.md