npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@exgen/extractor

v0.0.2

Published

Extract examples of how to consume your api from your unit tests

Downloads

11

Readme

@exgen/extractor

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.

Conventions

In order to use this library effective you need to follow certain conventions while writing your unit tests.

  1. Importing the function that you want to test in your test files as a named import.
  2. Using the describe function provided by jest to write your tests
  3. The first argument of describe should match the name of the imported function
  4. Inside the describe callback invoke the it function to write individual tests
  5. Each individual assertions would be written using expect function.

Usage

  1. import the default function extractExamples from the package
  2. Invoke extractExamples by passing the path to your test directory

How it works

  1. extractExamples recursively loops through all the files inside your test directory
  2. If it sees a file that ends with .test.ts or .test.js it tries to extract example from those files
  3. Underneath the hood it uses typescript to parse the text content of those files and generate an ast
  4. It loops through all the statements of the file
  5. If it sees an import statement it stores all the named functions imported internally.
  6. If it sees a describe function call it checks if the first argument (string) matches with one of the imported named function
  7. If it does it checks all the statements of the 2nd argument (function/callback) otherwise it skips it completely
  8. Inside the describe block, it again goes through all the statements and looks for the it function call
  9. The first argument of it can be any appropriate message (ideally it should describe what this test is doing).
  10. Just like describe block it goes through all the statements inside the 2nd argument of it
  11. All the statements are stored (as they are necessary in order to setup assertions)
  12. If we encounter an expect function call, it would be stored separately along with the expected value of the assertion

Example

// 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

Todo

  1. Support for default imports
  2. Support for nested describe block
  3. Support for classes rather than only functions