Description
prop-sets is a test tool to help generate every possible instance of a component in JavaScript + TypeScript.
With prop-sets, you don't need to outsmart your own code when writing tests. Instead of determining fragile points of failure from particular combinations of inputs, simply generate all possible combinations and assert everything.
Works with React, Vue, Jest, Mocha, etc. No dependencies.
prop-sets alternatives and similar libraries
Based on the "Testing Frameworks" category.
Alternatively, view prop-sets alternatives based on common mentions on social networks and blogs.
-
Playwright
Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API. -
nightwatch
Integrated end-to-end testing framework written in Node.js and using W3C Webdriver API. Developed at @browserstack -
istanbul
Yet another JS code coverage tool that computes statement, line, function and branch coverage with module loader hooks to transparently add coverage when running tests. Supports all JS coverage use cases including unit tests, server side functional tests and browser tests. Built for scale. -
chai
BDD / TDD assertion framework for node.js and the browser that can be paired with any testing framework. -
blanket
blanket.js is a simple code coverage library for javascript. Designed to be easy to install and use, for both browser and nodejs. -
autochecker
โป๏ธ Test your libraries in many different versions of NodeJS, Ruby, Java and many other languages -
Xeito
๐ค Xeito is a framework for building interactive web applications with Typescript and Tagged Template Literals. -
MonsterJS
DISCONTINUED. Simple but powerful progressive JavaScript framework based on web components. -
react testing library
Simple and complete React DOM testing utilities that encourage good testing practices.
SaaSHub - Software Alternatives and Reviews
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of prop-sets or a related project?
README
prop-sets
prop-sets
is a test tool to help generate every possible instance of a component in JavaScript + TypeScript.
With prop-sets
, you don't need to outsmart your own code when writing tests. Instead of determining fragile points of failure from particular combinations of inputs, simply generate all possible combinations and assert everything.
Works with React, Vue, Jest, Mocha, etc. No dependencies.
Benefits
Let's say you have a React component called Button with the props disabled
and color
, as well as a test that asserts the button is gray when disabled
is true and color
otherwise. Here's how to use prop-sets
to assert the component renders the correct color:
const Button = props => (
<button
disabled={props.disabled}
style={{
backgroundColor: props.disabled ? "gray" : props.color
}}
/>
);
it("is gray when disabled, props.color otherwise", () => {
propSets({
color: ["red", "blue"],
disabled: [true, false]
}).forEach(props => {
const component = <Button {...props} />;
const color = getColor(component);
expect(color).toBe(props.disabled ? "gray" : props.color);
});
});
prop-sets
helps you easily write tests for assertions that are based on multiple input values (in this case, disabled
and color
) without greatly increasing the amount of code you have to write.
Without prop-sets
, this test will need to be expanded to three assertions:
it("is gray when disabled", () => {
const component = <Button disabled color="red" />;
const color = getColor(component);
expect(color).toBe("gray");
});
it("is red when props.color is red", () => {
const component = <Button color="red" />;
const color = getColor(component);
expect(color).toBe("red");
});
it("is blue when props.color is blue", () => {
const component = <Button color="blue" />;
const color = getColor(component);
expect(color).toBe("blue");
});
Because backgroundColor
's value is determined by both the disabled
and color
prop, we need to have all three assertions to be sure the component behaves as expected. Here are some implementations of Button
that will only pass certain tests but fail all others.
// Passes 'is gray when disabled', fails all others
const Button = props => <button style={{ backgroundColor: "gray" }} />;
// Passes 'is red when color is red', fails all others
const Button = props => <button style={{ backgroundColor: "red" }} />;
// Passes 'is blue when color is blue', fails all others
const Button = props => <button style={{ backgroundColor: "blue" }} />;
// Passes 'is gray when disabled', 'is red when color is red', fails all others
const Button = props => (
<button style={{ backgroundColor: props.disabled ? "gray" : "red" }} />
);
The amount of combinations prop-sets
generates is the Cartesian product of all the values passed in (a.length * b.length * c.length * ...
), so as the amount of props grows, prop-sets
reduces your test's complexity at an exponential rate.
For example, if you have a component that only behaves a certain way if all 5 of its boolean props are true, the amount of tests you would need to write to formally assert that behavior is 32. With prop-sets
, just one!:
it("does something if all props are true, false otherwise", () => {
const tf = [true, false];
propSets({ a: tf, b: tf, c: tf, d: tf, e: tf }).forEach(props => {
expect(/* something */).toBe(
props.a && props.b && props.c && props.d && props.e
);
});
});
Use
Install
yarn add prop-sets
or npm install prop-sets
Import
import propSets from "prop-sets";
API
propSets(object)
Arguments
Name | Type | Description |
---|---|---|
object |
{ [prop]: Array<value> } |
An object of arrays containing possible values of the prop |
Return
Type | Description |
---|---|
Array<{ [prop]: value }> |
An array of props where every combination of prop values is unique |
TypeScript
prop-sets
comes typed but works perfectly fine without TypeScript.
declare const propSets: <
T extends Readonly<{
[key: string]: ReadonlyArray<any>;
}>
>(
obj: T
) => {
[key in keyof T]: T[key] extends (infer ElementType)[] ? ElementType : any
}[];
Further reading
Why Don't People Use Formal Methods? by Hillel Wayne
Combinatorial explosion (state explosion problem)
License
[MIT](./license)
*Note that all licence references and agreements mentioned in the prop-sets README section above
are relevant to that project's source code only.