@xxxxst/tsunit
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

tsunit

a typescript unit test like csharp

https://github.com/xxxxst/tsunit

English 简体中文

speciality

  • pretty API
  • run in chrome
  • easy to locate the error
  • support async test

Install

npm install @xxxxst/tsunit

Config

  • enable decortor
  • enable source map

see more at the example

https://github.com/xxxxst/example-tsunit

Usage

test file - ComTest.ts

@TestClass()
export default class ComTest {
    @Test()
    matchTest() {
        Assert.equal(1, 1);
        Assert.notEqual("aa", "bb");
    }

    @Ignore()
    @Test()
    ignoreTest() {
        Assert.equal(1, 2, "info");
    }

    @Test(1, 2)
    @Test("a", "b")
    noMatchTest(except, actual) {
        Assert.equal(except, actual, "info");
    }

    @AsyncTest()
    async asyncTest() {
        var rst = await this.getHttp();
        Assert.equal("data", rst);
    }

    async getHttp() {
        return new Promise(resolve => setTimeout(()=>{
            resolve("data");
        }, 0));
    }
}

test file - ComTest2.ts

@TestClass({autoRun:false})
export default class ComTest2 {
    constructor(arg1, arg2) {

    }
}

entry point file

// dont't need to create the test class
import './ComTest';

// second way to import
// make sure the class is used
import ComTest from './ComTest';
ComTest

// run test by yourself
import ComTest2 from './ComTest2';
// ... your init code
setTimeout(()=>{
    // run test
    Assert.run(ComTest2, "arg1", "arg2");
}, 0);

open console to view test info

API

decorators

@TestClass(option?:{autoRun?:boolean}, ...args)

  • class decorator
  • mark test class
  • autoRun auto run test, default is true
  • args test class constructor arguments

@Test(...args)

  • function decorator
  • mark test function
  • args arguments

@AsyncTest(...args)

  • function decorator
  • mark asynchronous test function
  • need to use async/await
  • args arguments

@Ignore()

  • function decorator
  • ignore (async) test function

Assert

Assert.run(class, ...args)

  • run class by yourself
  • need tu use autoRun:false in @TestClass()
  • class test class
  •  args test class constructor arguments

Assert.setLang(obj)

  • set language, default is english
  • obj language key/map, see more at source code
    • build-in language:
    • Assert.lang_en_us english
    • Assert.lang_zh_cn chinese

Assert.equal(expected:any, actual:any, desc: string = "")

  • assert if is equal, compare each param if is object
  • expected expected value
  • actual actual value
  • desc describe info

orther assert

- -
Assert.equal (expected:any, actual:any, desc: string = "")
Assert.notEqual (expected:any, actual:any, desc: string = "")
Assert.strongEqual (expected:any, actual:any, desc: string = "")
Assert.strongNotEqual (expected:any, actual:any, desc: string = "")
Assert.same (expected:any, actual:any, desc: string = "")
Assert.notSame (expected:any, actual:any, desc: string = "")
Assert.isTrue (actual:any, desc: string = "")
Assert.isFalse (actual:any, desc: string = "")
Assert.isEmpty (actual:any, desc: string = "")
Assert.isNotEmpty (actual:any, desc: string = "")
Assert.isNull (actual:any, desc: string = "")
Assert.isNotNull (actual:any, desc: string = "")
Assert.isUndefined (actual:any, desc: string = "")
Assert.isNotUndefined (actual:any, desc: string = "")
Assert.isBool (actual:any, desc: string = "")
Assert.isArray (actual:any, desc: string = "")
Assert.isObject (actual:any, desc: string = "")
Assert.isString (actual:any, desc: string = "")
Assert.isNumber (actual:any, desc: string = "")
Assert.success (desc: string = "")
Assert.fail (desc: string = "")

assert usage

function approximate expression useage
equal a==b(iterate over object) Assert.equal(1,1)
Assert.equal(true,true)
Assert.equal(1,'1')
Assert.equal({},{})
Assert.equal([1],[1])
=> success
=> success
=> success
=> success
=> success
notEqual a!=b(iterate over object)
strongEqual typeof(a)==typeof(b) && equal(a,b) Assert.strongEqual(1,'1') => failed
strongNotEqual !strongEqual(a,b)
same a===b Assert.same(1,'1')
Assert.same({},{})
a=b={},Assert.same(a,b)
=> failed
=> failed
=> success
notSame a!==b
isTrue !!a Assert.isTrue(0)
Assert.isTrue(false)
Assert.isTrue("")
Assert.isTrue(null)
Assert.isTrue(undefined)
=> failed
=> failed
=> failed
=> failed
=> failed
isFalse !a
isEmpty !a Assert.isEmpty("") => success
isNotEmpty !!a
isNull a===null Assert.isNull(null)
Assert.isNull(undefined)
=> success
=> failed
isNotNull a!==null
isUndefined a===undefined Assert.isUndefined(undefined)
Assert.isUndefined(null)
=> success
=> failed
isNotUndefined a!==undefined
isBool typeof(a)=="boolean" Assert.isBool(undefined)
Assert.isBool(null)
Assert.isBool(0)
Assert.isBool("")
=> failed
=> failed
=> failed
=> failed
isArray Array.isArray(a) Assert.isArray([])
Assert.isArray({})
=> success
=> failed
isObject a!=null && typeof(a)=="object" &&
!Array.isArray(a)
Assert.isObject({})
Assert.isObject([])
=> success
=> failed
isString typeof(a)=="string" Assert.isString("") => success
isNumber typeof(a)=="number" Assert.isString(1) => success
success true Assert.success() => success
fail false Assert.fail() => failed

Example

base on webpack + typescript + vue(ui test)

https://github.com/xxxxst/example-tsunit

Readme

Keywords

none

Package Sidebar

Install

npm i @xxxxst/tsunit

Weekly Downloads

1

Version

0.0.1

License

none

Unpacked Size

72 kB

Total Files

18

Last publish

Collaborators

  • xxxxst