現(xiàn)在很多項目中前端項目越來越龐大,單元測試是不能缺少的一環(huán)。我們工作中也不例外,React中現(xiàn)在大家用的比較多的有jest
、enzyme
。
經(jīng)過一段時間的使用,現(xiàn)在也是擁抱TypeScript了,接口約定比原來的JavaScript更加簡潔清除了。但隨之而來的問題也不少,通常我們編寫TSX文件,引入React是用的默認方式:
//Regular imports
import * as React from 'react';
//Synthetic default imports:
import React from 'react';
按官方的說法第二種是動態(tài)導(dǎo)入,我們通常用斷言的方式,也就是第一種引入React??次浵伒脑创a是用的第二種方式引入的,
"@types/react": "^15.0.38",
"@types/react-dom": "~0.14.18",
"antd-demo-jest": "^1.3.0",
"antd-tools": "~1.6.0",
"babel-cli": "^6.18.0",
"babel-eslint": "^7.1.0",
"babel-jest": "^19.0.0",
"babel-plugin-import": "^1.0.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.18.0",
"babel-preset-react": "^6.16.0",
"babel-preset-stage-0": "^6.16.0",
"bezier-easing": "^2.0.3",
"bisheng": "^0.24.0",
"bisheng-plugin-antd": "^0.15.0",
"bisheng-plugin-description": "^0.1.1",
"bisheng-plugin-react": "^0.5.0",
"bisheng-plugin-toc": "^0.4.0",
"color-standalone": "^0.11.6",
"cross-env": "^4.0.0",
"css-split-webpack-plugin": "^0.2.3",
"dekko": "^0.2.0",
"delegate": "^3.1.2",
"dora-plugin-upload": "^0.3.1",
"enquire.js": "^2.1.1",
"enzyme": "^2.6.0",
"enzyme-to-json": "^1.3.0",
"eslint": "^3.0.1",
"eslint-config-airbnb": "latest",
"eslint-plugin-babel": "^4.0.0",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jsx-a11y": "^5.0.1",
"eslint-plugin-markdown": "~1.0.0-beta.4",
"eslint-plugin-react": "^7.0.1",
"eslint-tinker": "^0.4.0",
"fetch-jsonp": "^1.0.3",
"glob": "^7.1.1",
"jest": "^20.0.4",
"jsonml.js": "^0.1.0",
"lint-staged": "^3.3.1",
"mockdate": "^2.0.1",
"moment-timezone": "^0.5.5",
"pre-commit": "^1.2.2",
"querystring": "^0.2.0",
"rc-queue-anim": "^1.0.1",
"rc-scroll-anim": "^1.0.3",
"rc-tween-one": "^1.1.2",
"react": "^15.0.0",
"react-color": "^2.11.7",
"react-copy-to-clipboard": "^4.0.1",
"react-document-title": "^2.0.1",
"react-dom": "^15.0.0",
"react-github-button": "^0.1.1",
"react-intl": "^2.0.1",
"react-sublime-video": "^0.2.0",
"react-test-renderer": "^15.5.4",
"reqwest": "^2.0.5",
"rimraf": "^2.5.4",
"stylelint": "^7.8.0",
"stylelint-config-standard": "^16.0.0",
"typescript": "~2.3.0",
"typescript-babel-jest": "^1.0.2",
"values.js": "^1.0.3",
"xhr2": "^0.1.3"
我想他們是通過typescript-babel-jest
,babel-jest
的方式可以這么直接導(dǎo)入的?在github上并沒有找到過多關(guān)于這方式的解釋,誰搞過這個嗎?我準(zhǔn)備翻翻babel看看怎么搞~~~
第二個問題是,tsx編寫的組件,是如何支持導(dǎo)入test.js中進行單元測試的?
tsx 導(dǎo)入js文件的時候js,編譯階段沒有報錯,同樣是直接的import進去的
//index.test.js
import React from 'react';
import TestUtils from 'react-dom/test-utils';
import { Col, Row } from '..'; //看過antd源碼的會知道,這兩個是tsx編寫的組件
describe('Grid', () => {
it('should render Col', () => {
const col = TestUtils.renderIntoDocument(
<Col span="2" />
);
const colNode = TestUtils.findRenderedDOMComponentWithTag(col, 'DIV');
expect(colNode.className).toBe('ant-col-2');
});
it('should render Row', () => {
const row = TestUtils.renderIntoDocument(
<Row />
);
const rowNode = TestUtils.findRenderedDOMComponentWithTag(row, 'DIV');
expect(rowNode.className).toBe('ant-row');
});
});
2、第二個他們要解決的問題就是tsx轉(zhuǎn)換為js,進行單元測試吧、他們是如何轉(zhuǎn)換的呢?
TypeScript有一個關(guān)于react的教程,你可以看看那個。里面有關(guān)于單元測試的。
https://github.com/Microsoft/...
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團,成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達內(nèi)教育集團成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負責(zé)iOS教學(xué)及管理工作。
浪潮集團項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。