使用react-dom时react组件测试文件中的console.log语句不起作用

时间:2019-03-29 06:24:26

标签: reactjs typescript unit-testing jestjs create-react-app

我使用带有typescript的create-react-app创建了一个react应用,并尝试从测试文件中控制台记录了几件事,但是它不起作用。

This是我用来设置带打字稿的create react应用程序的教程。我只是在安装程序随附的App.test.tsx文件中添加了console.log语句,但它不起作用。

当我删除了两行使用ReactDOM的代码时,效果很好

package.json

{
  "name": "testing2",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@types/jest": "24.0.11",
    "@types/node": "11.12.0",
    "@types/react": "16.8.10",
    "@types/react-dom": "16.8.3",
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-scripts": "2.1.8",
    "typescript": "3.3.4000"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ]
}

App.test.tsx (无效)

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

it('renders without crashing', () => {
  console.log('hello thereeeeeeee');
  const div = document.createElement('div');
  ReactDOM.render(<App />, div);
  ReactDOM.unmountComponentAtNode(div);
});

App.test.tsx (有效)

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

it('renders without crashing', () => {
  console.log('hello thereeeeeeee');
  const div = document.createElement('div');
  // ReactDOM.render(<App />, div);
  // ReactDOM.unmountComponentAtNode(div);
});

console.log语句应该打印。但这不是打印。测试通过了,但是当我在测试中使用ReactDOM时,控制台语句未打印

0 个答案:

没有答案