鍍金池/ 問答/測(cè)試  網(wǎng)絡(luò)安全  HTML/ React中編寫的tsx組件,如何導(dǎo)入到j(luò)s中進(jìn)行單元測(cè)試?看了螞蟻的源碼,還是

React中編寫的tsx組件,如何導(dǎo)入到j(luò)s中進(jìn)行單元測(cè)試?看了螞蟻的源碼,還是有點(diǎn)不太明白,進(jìn)來一起研究下

現(xiàn)在很多項(xiàng)目中前端項(xiàng)目越來越龐大,單元測(cè)試是不能缺少的一環(huán)。我們工作中也不例外,React中現(xiàn)在大家用的比較多的有jestenzyme。
經(jīng)過一段時(shí)間的使用,現(xiàn)在也是擁抱TypeScript了,接口約定比原來的JavaScript更加簡(jiǎn)潔清除了。但隨之而來的問題也不少,通常我們編寫TSX文件,引入React是用的默認(rèn)方式:

//Regular imports
import * as React from 'react';

//Synthetic default imports:
import React from 'react';

按官方的說法第二種是動(dòng)態(tài)導(dǎo)入,我們通常用斷言的方式,也就是第一種引入React。看螞蟻的源碼是用的第二種方式引入的,

"@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)于這方式的解釋,誰搞過這個(gè)嗎?我準(zhǔn)備翻翻babel看看怎么搞~~~

第二個(gè)問題是,tsx編寫的組件,是如何支持導(dǎo)入test.js中進(jìn)行單元測(cè)試的?

  1. tsx 導(dǎo)入js文件的時(shí)候js,編譯階段沒有報(bào)錯(cuò),同樣是直接的import進(jìn)去的

//index.test.js
import React from 'react';
import TestUtils from 'react-dom/test-utils';
import { Col, Row } from '..'; //看過antd源碼的會(huì)知道,這兩個(gè)是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、第二個(gè)他們要解決的問題就是tsx轉(zhuǎn)換為js,進(jìn)行單元測(cè)試吧、他們是如何轉(zhuǎn)換的呢?

回答
編輯回答
故林

TypeScript有一個(gè)關(guān)于react的教程,你可以看看那個(gè)。里面有關(guān)于單元測(cè)試的。
https://github.com/Microsoft/...

2018年6月6日 03:03
編輯回答
執(zhí)念

研究了一段時(shí)間,猜想,不論是用的什么第三方插件,測(cè)試環(huán)境還是基于nodeJS的,nodejs環(huán)境下運(yùn)行的是什么?最終還是要編譯成為js,再去跑測(cè)試的。于是我去搜了一下,怎么把tsx、ts文件轉(zhuǎn)為js文件,果然,有一個(gè)typescript-babel-jest是可以做到的,到此我們的第二個(gè)問題就解決掉了。

2017年12月17日 01:38