使用tsc

时间:2017-10-28 17:07:27

标签: typescript testing istanbul test-coverage

对于我想测试的功能,似乎覆盖率不正确:

public isSelfTweet = function(tweet: Tweet, twitterScreenName: string) {
    if(null == tweet || null == tweet.user || null == tweet.user.screen_name) {
        return false;
    }
    if(tweet.user.screen_name.toLowerCase() === twitterScreenName) {
        return true;
    }
    return false;
}

我写了三个测试,我有100%的覆盖率:

  • tweet = null,twitterScreenName =" toto"
  • tweet with screen_name =" toto",twitterScreenName =" toto"
  • tweet with screen_name =" toto",twitterScreenName =" tutu"

这是对我来说似乎不正确的报道。我从未测试过tweet not null,但是tweet.user null例如

enter image description here

在我的包json中:

"nyc": "^11.2.1",

1 个答案:

答案 0 :(得分:0)

您认为究竟什么不起作用?

当我在Typescript Playground中尝试以下操作时,测试工作正常。顺便说一句,你不能使用null调用isSelfTweet,因为你声明它必须是Tweet ......

interface Tweet { 
    user:User
}

interface User { 
    screen_name:string
}

class Test {
    public isSelfTweet(tweet: Tweet, twitterScreenName: string) : boolean {
        if (tweet.user) {
            if (tweet.user.screen_name) {
                if (tweet.user.screen_name.toLowerCase() === twitterScreenName) {
                    return true;
                }
            }
        }
        return false;
    }
}

let t = new Test()
let tweet = { user: {screen_name:"willy"}}

let test1 = t.isSelfTweet(tweet, "willy")
console.log(test1)   // returns true

let test2 = t.isSelfTweet(tweet, "wally")
console.log(test2)   // returns false