スクラッチ試験から構築されたシンプルなユニットテストユニットを冗談

JS、冗談を使用して試験したTS相単位セル

多くのチュートリアルが、私はこれは、よりユーザーフレンドリーな、クローズアップ、スプレーしないかもしれません。

インストールが依存します

cnpm install ts-jest jest  @types/jest --save-dev

コンフィギュレーション

1:次のように、 "冗談": "sciptsは" "test" を追加して、package.jsoinを変更

"scripts": {
    "start": "webpack-dev-server",
    "build": "webpack --mode production",
    "test": "jest"
},

2:追加jest.config.jsのファイルを

以下の塗りつぶし

module.exports = {
    "moduleFileExtensions": [
        "js",
        "ts",
        "tsx"
    ],
    "transform": {
        "^.+\\.tsx?$": "ts-jest",
    },
    "testMatch": [
        "<rootDir>/tests/**/*.ts?(x)"
    ]
}

私たちは、テストプロジェクト/テスト/ファイルの下testMatch内容によって見ることができます

一般的に以下のテストでファイルを書き込むことをお勧めしますが、私は外に書くことを好むています。

そして、テストファイルを追加

テストファイルを追加します

6759456-ce23515f2248e364.png

これは私のディレクトリである、あなたはを参照することができます

テストを書きます

公式ドキュメントを参照してくださいするために特別にQAQを参照してくださいhttps://jestjs.io/docs/zh-Hans/getting-started

import { getSTyleStr, id } from '../../src/utils/utils'

test('id', () => {
    expect(id(1)).toBe(1)
    expect(id(null)).toBe(null)
    expect(id(void 0)).toBe(void 0)
})
describe('css class utils getSTyleStr', () => {
    it('带大写的属性', () => {
        expect(getSTyleStr({backgroundColor: "rgba(216,52,52,1)"}))
        .toBe('background-color: rgba(216,52,52,1);')
    })
    it('单个属性', () => {
        expect(getSTyleStr({width: "30px", height: "30px"}))
        .toBe('width: 30px;height: 30px;')
    })
})

ラン

npm run test
6759456-1575938ccdd170a9.png

完璧な

我々は間違っている場合は、次のようにつまり、


6759456-b908e0ca6159ed13.png

ソースを表示:https://github.com/0123cf/layout-ui

関連リンク:

https://jestjs.io/docs/zh-Hans/getting-started
https://github.com/0123cf/layout-ui

- 終わり -

ます。https://www.jianshu.com/p/a2cfb462f583で再現

おすすめ

転載: blog.csdn.net/weixin_33716557/article/details/91218413