Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@nifty-lil-tricks/testing
Advanced tools
A selection of useful utilities (or nifty li'l tricks!) for all things testing
Note: this package and selected plugins are currently a work in progress
A selection of useful utilities (or nifty li'l tricks!) for all things testing.
Note: this package works with TypeScript v5 or later
import * as testing from "deps";
npm install @nifty-lil-tricks/testing
The TypeScript tsconfig.json
must contain the following recommended settings:
{
"compilerOptions": {
"target": "ES2022",
"strict": true
}
}
Note, check the plugin READMEs for any additional TypeScript settings.
The following features are supported
The following plugins are available to that make use of the setup tests plugin system.
Plugin | Description | Status | Npm | Docs |
---|---|---|---|---|
PostgreSQl | Setup the World's Most Advanced Open Source Relational Database for testing. It has the following features:
| ✅ | Docs. | |
NestJS Server | Setup a progressive Node.js framework for building efficient, reliable and scalable server-side applications for testing. | ✅ | Docs | |
Express Server | Setup a minimal and flexible Node.js web Express application for testing. | 🚧 | Docs |
import {
afterEach,
beforeEach,
describe,
it,
} from "testing/bdd.ts";
import { assertEquals } from "testing/asserts.ts";
import {
setupTestsFactory,
type SetupTestsTeardown,
} from "@nifty-lil-tricks/testing";
// Define or import a plugin as follows:
const helloWorldPlugin = {
setup: (config: { message: string }) => {
// Setup plugin according to config
return {
output: config,
teardown: () => {},
};
},
};
// In another file, load plugins as follows to generate a setupTests function:
export const { setupTests } = setupTestsFactory({
helloWorld: helloWorldPlugin,
});
// Then one can use this in any test file as follows:
describe("Service", () => {
let teardownTests: SetupTestsTeardown;
let message: string;
beforeEach(async () => {
// Setup tests with configured plugins
const result = await setupTests({
helloWorld: { message: "Hello, world!" },
});
message = result.outputs.helloWorld.output.message;
teardownTests = result.teardownTests;
});
afterEach(async () => {
// Teardown tests to restore environment after tests have run
await teardownTests();
});
describe("method", () => {
it("should test something that relies on the plugin being configured", () => {
// Some other testing
assertEquals(message, "Hello, world!");
});
});
});
setupTestsFactory
One can use the setupTestsFactory
to register or load defined plugins. It is
only needed to load these once so it is recommended to share the returned
setupTests
function across all test files.
Each plugin must be given a name that is not one of the reserved plugin names. This returns a function that when run, sets up the tests to use the loaded plugins according to the provided config.
Each plugin must contain the following functions in order to be correctly loaded:
setup
teardown
An example of a plugin is as follows:
import {
type Plugin,
} from "@nifty-lil-tricks/testing";
interface HelloWorldConfig {
message: string;
}
type HelloWorldResult = string;
const helloWorldPlugin: Plugin<HelloWorldConfig, HelloWorldResult> = {
setup(config: HelloWorldConfig) {
// Setup plugin according to config
return {
output: config.message,
teardown() {
// Teardown any setup resources
},
};
},
};
setupTests
One can use the returned setupTests
function use the loaded plugins.
The loaded plugins available to the setupTests function returned from the factory can be configured using config namespaced to the name of the plugin.
For example, if the plugin is named helloWorld
, then the config for that
plugin must be provided under the helloWorld
namespace.
When run, setupTests will return an object with the data returned from the plugin invocation. The data will be namespaced to the plugin name. For example:
import {
setupTestsFactory,
type SetupTestsTeardown,
} from "@nifty-lil-tricks/testing";
// Define or import a plugin as follows:
const helloWorldPlugin = {
setup: (config: { message: string }) => {
// Setup plugin according to config
return {
output: config.message,
teardown: () => {
// Teardown any setup resources
},
};
},
};
// In another file, load plugins as follows to generate a setupTests function:
export const { setupTests } = setupTestsFactory({
helloWorld: helloWorldPlugin,
});
const result = await setupTests({
helloWorld: { message: "Hello, world!" },
});
result.outputs.helloWorld.output; // "Hello, world!"
Only plugins that are configured will be run. If a plugin is not configured, then it will not be run. The order of the plugins in the config is defined the order in which they defined in the config object. This follows the rules as defined here.
The returned object will also contain a teardown
function that when run, will
teardown the plugins in the reverse order that they were setup.
teardownTests
One can use the returned teardownTests
function to restore the environment to
its original state after the tests have run.
For example:
import {
setupTestsFactory,
type SetupTestsTeardown,
} from "@nifty-lil-tricks/testing";
// Define or import a plugin as follows:
const helloWorldPlugin = {
setup: (config: { message: string }) => {
// Setup plugin according to config
return {
output: config.message,
teardown: () => {
// Teardown any setup resources
},
};
},
};
// In another file, load plugins as follows to generate a setupTests function:
export const { setupTests } = setupTestsFactory({
helloWorld: helloWorldPlugin,
});
const result = await setupTests({
helloWorld: { message: "Hello, world!" },
});
// Teardown tests to restore environment after tests have run
await result.teardownTests();
The API Docs can be found here.
Examples can be found here.
Platform Version | Supported | Notes |
---|---|---|
Deno v1 | :white_check_mark: | |
Node.JS v18 | :white_check_mark: | TypeScript v5+ for typings |
Node.JS v20 | :white_check_mark: | TypeScript v5+ for typings |
Web Browsers | :x: | Coming soon |
Nifty li'l tricks packages are 100% free and open-source, under the MIT license.
This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.
Contributions, issues and feature requests are very welcome. If you are using this package and fixed a bug for yourself, please consider submitting a PR!
FAQs
A selection of useful utilities (or nifty li'l tricks!) for all things testing
We found that @nifty-lil-tricks/testing demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.