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.
@exgen/extractor
Advanced tools
Extract examples of how to consume your api from your unit tests
Extract examples of how to consume your api from your unit tests
Currently this library only works if you write your tests using jest and javascript/typescript, but I do have plans to support more testing framework. Complex tests with mocking, spying and using jest's api are still not supported.
In order to use this library effective you need to follow certain conventions while writing your unit tests.
describe
function provided by jest
to write your testsdescribe
callback invoke the it
function to write individual testsexpect
function.extractExamples
from the packageextractExamples
by passing the path to your test directoryextractExamples
recursively loops through all the files inside your test directory.test.ts
or .test.js
it tries to extract example from those filestypescript
to parse the text content of those files and generate an astdescribe
function call it checks if the first argument (string) matches with one of the imported named functionit
function callit
can be any appropriate message (ideally it should describe what this test is doing).describe
block it goes through all the statements inside the 2nd argument of it
expect
function call, it would be stored separately along with the expected value of the assertion// tests/index.test.ts
import { makeDouble } from './libs/makeDouble';
function getArgument() {
return 1;
};
describe('makeDouble', () => {
it("Convert 2 to double", () => {
let argument = getArgument();
argument+=1;
const doubled = makeDouble(argument);
expect(
doubled
).toStrictEqual(4);
});
it("Convert 1 to double", () => {
const doubled = makeDouble(1)
expect(
doubled
).toStrictEqual(1);
});
});
// src/index.ts
import extractExamples from "@exgen/extractor";
import path from "path";
async function main() {
const extractedExamples = await extractExamples(path.resolve(__dirname, "../tests"));
console.log(extractedExamples);
}
main();
interface ExampleInfo {
// Statements (except for expect that are inside the `it` function)
statements: string[];
// An array of assertion value and expected value
logs: {
// Assertion value
arg: string;
// expected value
output: string;
}[];
// First argument of the `it` function, used as a message that describes the test
message: string;
};
// Each key of the object would be the name of the function
type FunctionExampleRecord = Record<string, ExampleInfo[]>;
const functionExampleRecord: FunctionExampleRecord = {
makeDouble: [
{
logs: [{
output: "4",
arg: "doubled"
}],
message: "Convert 2 to double",
statements: ["let argument = getArgument();", "argument += 1;", "const doubled = makeDouble(argument);"]
},
{
logs: [{
output: "1",
arg: "doubled"
}],
message: "Convert 1 to double",
statements: ["const doubled = makeDouble(1);"]
}]
})
}
The rest is up to you how to display or use this data. You can embed it inside your api documentation markdown file or any other files.
Please take a look at the @exgen/embedder
package to see how you can embed this data in a markdown file generated by typedoc
and typedoc-plugin-markdown
FAQs
Extract examples of how to consume your api from your unit tests
We found that @exgen/extractor 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.