![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
@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.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.