Code Monkey home page Code Monkey logo

js-test-gen's Introduction

js-test-gen

js-test-gen is an opinionated unit test template generator using babel and prettier.

It uses babel to identify what modules are being exported. It then generates a test template and formats the template using prettier.

CircleCI

Installation

npm install js-tes-gen

or

yarn add js-test-gen

Input

//name of this file is myfuncs.js

export const someModule1 = () => {};
export const someModule2 = () => {};

const someModule = () => {};

export default someModule;

Output

import someModule, { someModule1, someModule2 } from "./myfuncs";

describe("someModule", () => {
  it("should fail auto generated test", () => {
    expect(someModule()).toBe(false);
  });
});
describe("someModule1", () => {
  it("should fail auto generated test", () => {
    expect(someModule1()).toBe(false);
  });
});
describe("someModule2", () => {
  it("should fail auto generated test", () => {
    expect(someModule2()).toBe(false);
  });
});

API

generateTestTemplate({contents, srcfileName, importFromPath, typeSystem})

generateTestTemplate is used to generate a complete js unit test template.

property type description
contents string The JS contents to generate a test from.
srcFileName string The name of the JS module the test is being created for.
importFromPath string The path of where the module is located in relation to the generated test file E.G . or ../
typeSystem FLOW or TYPESCRIPT If the js module is using a type system this needs to be specified.

generateTest(contents, typeSystem)

generateTest is used to generate test cases from given js contents.

arguments type description
contents string The JS contents to generate a test from.
typeSystem FLOW or TYPESCRIPT If the js contents is using a type system this needs to be specified.

Usage

import { generateTestTemplate } from "js-test-gen";

const contentToParse = `export const addOne = (x) => x +1`;

const testTemplate = generateTestTemplate({
  contents: contentsToParse, // contents of the mod
  srcFileName: "addOne", // the name of the mod we want to test
  importFromPath: ".." // where the test should import the mod from
});

result

import { addOne } from "../addOne";

describe("addOne", () => {
  it("should fail auto generated test", () => {
    expect(addOne()).toBe(false);
  });
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.