0.8.0 • Published 11 months ago

@boostest/cli v0.8.0

Weekly downloads
-
License
MIT
Repository
-
Last release
11 months ago

TL;DR

Japanese(日本語)→README-ja.md

  • Instantly create test data from TypeScript type, interface, or class 📝
  • Partially overwrite test data in type or interface 🏗️
  • Test data is output as actual code, becoming a user asset 💸

Installation

# global
npm install -g @boostest/cli

# repo
npm install --save-dev @boostest/cli
pnpm add -D @boostest/cli
yarn add -D @boostest/cli

Init

Go to the root directory of the project where tsconfig.json and other files are located.

npx boostest init

The following file boostest.setting.json will be created

{
  "target_pattern": ["src/**/*.ts"],
  "name": "boostest",
  "tsconfig": "./tsconfig.json",
  "output": {
    "single": true
  },
  "initial_value": {
    "string": "init string value",
    "number": 10000,
    "bigint": "556455199254740991n",
    "any": "any value"
  }
}

Add BoostestFuntion

Define a function that includes the contents described in name in boostest.setting.json as shown below, and specify the Type, etc. in Generic. You can leave the function undefined.

type User = {
  name: string;
  age: number;
  // ...more complex types
};
interface Job {
  name: string
  // ...more complex types
}
class Test {
  constructor(public name: string) {
  }
}

const user = boostestUser<User>(); // Cannot find name 'boostestUser'
const job = boostestJob<Job>(); // Cannot find name 'boostestJob'.
const test = boostestTest<Test>(); // Cannot find name 'boostestTest'.

Generate

The following commands will automatically generate functions that were undefined.

npx boostest

Import boostestUser, boostestJob and boostestTest from the automatically generated directories.

import { boostestJob } from "../boostest_output/boostestJob";
import { boostestTest } from "../boostest_output/boostestTest";
import { boostestUser } from "../boostest_output/boostestUser";

When the file is executed, the contents of type are available as values.

console.log("user", user);
// user { name: "init string value", age: 10000 }

console.log("job", job);
// job { name: "init string value" }

console.log("test class prop name", test.name);
// test class prop name init string value

overwrite

type User = {
  name: string;
  age: number;
  // ...more complex types
};
const user = boostestUser<User>({ age: 30 }); // overwrites the default value age

console.log("user", user);
// user { name: "init string value", age: 30 }

Introduction 🚀

boostest.setting.json

target_pattern

Specify the target files in glob format.

"target_pattern": ["src/**/*.{ts,tsx}"],

Internally https://github.com/Gilnaa/globwalk is used.

name

Normally, functions containing boostest like the following are targeted, but you can freely change it using name.

// boostest[FreeName]<[TypeName or InterfaceName or ClassName]>();
const testData = boostestTest<TypeName>();

If you set name as follows, functions containing hoge will be targeted.

// "name": "hoge",
const testData = hogeTest<TypeName>();

output

If single is set to true, the test data will be output to a single directory.

project root
├── boostest_output
│   ├── boostestGeneric.ts
│   ├── boostestGenericsAliasType.ts
│   ├── boostestGenericsInterfaceType.ts
│   └── boostestGenericsTypeClass.ts

If single is set to false, the test data will be output to a separate file.

something
├── dir1
│   ├── boostest_output
│   │   ├── boostestFile1.ts
│   │   └── boostestFile1-2.ts
│   └── targetFile1.ts
├── dir2
│   ├── boostest_output
│   │   └── boostestFile2.ts
│   └── targetFile2.ts
├── dir3
│   ├── boostest_output
│   │   └── boostestFile3.ts
│   └── targetFile3.ts

tsconfig

Specify the path to tsconfig.json. This is useful for module resolution, such as being able to use aliases like import {hoge} from "@alias/somethis...".

initial_value

You can set the initial value for each type.

"initial_value": {
  "string": "init string value",
  "number": 10000,
  "bigint": "556455199254740991n",
  "any": "any value"
}
string -> "init string value"
number -> 10000
bigint -> 556455199254740991n
any -> "any value"

Supports

Types

typesupportexampledefault result val
stringstring"test string data"
numbernumber10
bigint100n9007199254740991
booleanbooleantrue
undefinedundefinedundefined
nullnullnull
anyany"any"
unknownunknownundefined
nevernevernull
objectobject{}
voidvoidnull
function()=>void()=>{}
arraystring[][]
unionstring \| number"test string data"
conditionalstring extends number ? true : false;false (Condition Result)
symbolsymbolSymbol()
tuple type[string, number]["test string data", 10]
named tuple type[name: string, age: number]["test string data", 10]
intersection type{name: string} & {age: number}{ name: "init string value", age: 10000 }
keyofkeyof { name: string }name
typeoftypeof Hoge // const Hoge = { name: "hoge" };user { name: "init string value" }
infer--
mapped type--
namespace--
constructor type--
index accessor--
template--

Utilities type

typesupport
ThisType<T>
Array<T>
Partial<T>
Required<T>
Readonly<T>
Pick<T, K>
Omit<T, K>
Extract<T, U>
Exclude<T, U>
NonNullable<T>
Parameters<T>
ConstructorParameters<T>
ReturnType<T>
InstanceType<T>
Promise<T>

Import/Export

ESM

kindsupportexample
default importimport Hoge from '@/hoge';
importimport { Hoge } from '@/hoge';
default exportexport default Hoge;
named exportexport type { Hoge as AnotherName }
named default exportexport type { Hoge as default }
export declexport interface Hoge { name: string; }
default export declexport default interface Hoge { name: string; }
export with importexport type { Hoge } from './index';
named export with importexport type { Hoge as AnotherName } from './index';

CommonJS

kindsupportexample
export assignmentexport = Hoge;
require×const hoge = require('./hoge.js');

declaration

namespace

Not supported in some cases