Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
driver-pattern
Advanced tools
A project for generating react test drivers.
Features:
Resources: BDD feat. Driver & Builder Patterns
You can see usage examples in the test file, those are some cherry-picked exmaples:
Automatically created "givens" based on properties & declared getters:
const NameComponent: FC<ComponentProps> = ({ name }: { name: string }) => (
<span data-testid="test-name">{name}</span>
);
const driver = createDriver(NameComponent, {
getters: {
name: "test-name",
},
});
driver.given.name("Some Name").when.render();
expect(driver.get.name()!.innerHTML).toEqual("Some Name");
Actions are defined as a function which receives a get parameter, giving you access to the the get object infered from getters.
Simply put, you can use driver.get.someGetter()
when performing actions:
When defining actions, you can then use the .on
property to trigger them:
const driver = createDriver(Component, {
getters: {
button: "test-button",
},
actions: get => ({
click: () => fireEvent.click(driver.get.button()!),
}),
});
const onClickSpy = jest.fn();
driver.given.onClick(onClickSpy).when.render().on.click();
expect(onClickSpy).toHaveBeenCalledTimes(1);
const driver = createDriver(Component, {
getters: {
button: "test-button",
},
actions: get => ({
click: () => {
fireEvent.click(driver.get.button()!);
return waitFor(/* some condition */);
},
}),
});
await driver.given.onClick(onClickSpy).when.render().on.click();
expect(/* some expectation */);
const name = chance.word();
const driver = createDriver(Component, {
defaultProps: () => ({ name }),
getters: {
name: "test-name",
},
});
driver.when.render();
expect(driver.get.name()!.innerHTML).toEqual(name);
Receives a createStore method, which:
const createStore = ({ preloadedState }: { preloadedState: any }) => configureStore(...);
const driver = createDriver(ReduxComponent, {
getters: {
title: "test-redux-title",
},
createStore,
});
driver.givenState.title("Some Title").when.render();
expect(driver.get.title()!.innerHTML).toEqual("Some Title");
it("should get default state from defaultState", () => {
const title = chance.word();
const driver = createDriver(ReduxComponent, {
getters: {
title: "test-redux-title",
},
createStore,
defaultState: () => ({ title }),
});
driver.when.render();
expect(driver.get.title()!.innerHTML).toEqual(title);
});
You can call initialize to empty props & state between tests (or initialize then to defaultProps & defaultState)
describe("test suite", () => {
const driver = createDriver(Component, {
getters: {
propRender: "some-component-prop",
stateRender: "some-state-prop",
},
defaultProps: () => ({
someComponentProp: chance.string()
}),
createStore,
defaultProps: () => ({
someStateProp: chance.string()
}),
});
// The following will initialize both props & state using the generate methods provided
// in the creation method above.
// In the following tests, the values for driver.get.propRender() & driver.get.stateRender()
// will be populated, and regenerated in each test anew.
beforeEach(() => driver.initialize());
it("test 1", () => {
// ...
});
it("test 2", () => {
// ...
});
});
FAQs
Driver pattern for dynamically creating test drivers
We found that driver-pattern 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’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.