Method “text” is meant to be run on 1 node. 0 found instead.

来源:5-3 React 测试工具简介

慕妹5168478

2020-03-14

老师您好,能帮忙看看这个问题是什么原因造成的吗?
使用 npm test
报错信息如下 :

		FAIL  src/components/__test__/TotalPrice.test.js
		test TotalPrice component
		× component should render correct income&outcome number (51ms)

	  ● test TotalPrice component › component should render correct income&outcome number

	    Method “text” is meant to be run on 1 node. 0 found instead.

	      11 |   it('component should render correct income&outcome number', () => {
	      12 |     const wrapper = shallow(<TotalPrice {...props} />)
	    > 13 |     expect(wrapper.find('.income span').text() * 1).toEqual(1000)
	         |                                         ^
	      14 |     expect(wrapper.find('.outcome span').text() * 1).toEqual(2000)
	      15 |   })
	      16 | })

	      at ShallowWrapper.single (node_modules/enzyme/src/ShallowWrapper.js:1652:13)
	      at ShallowWrapper.text (node_modules/enzyme/src/ShallowWrapper.js:1093:17)
	      at Object.it (src/components/__test__/TotalPrice.test.js:13:41)

	Test Suites: 1 failed, 1 total
	Tests:       1 failed, 1 total
	Snapshots:   0 total
	Time:        4.595s
	Ran all test suites related to changed files.

	Watch Usage: Press w to show more.

TotalPrice .js


	import React from 'react'
	import { shallow } from 'enzyme'
	import TotalPrice from '../TotalPrice'

const props = {
  income: 1000,
  outcome: 2000
}

describe('test TotalPrice component', () => {
  it('component should render correct income&outcome number', () => {
    const wrapper = shallow(<TotalPrice {...props} />)
    expect(wrapper.find('.income span').text() * 1).toEqual(1000)
    expect(wrapper.find('.outcome span').text() * 1).toEqual(2000)
  })
})

package.json:

{
  "name": "my-project",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.5.0",
    "@testing-library/user-event": "^7.2.1",
    "bootstrap": "^3.3.7",
    "react": "^16.13.0",
    "react-bootstrap": "^1.0.0-beta.17",
    "react-dom": "^16.13.0",
    "react-ionicons": "^2.1.6",
    "react-scripts": "^3.4.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "enzyme": "^3.11.0",
    "enzyme-adapter-react-16": "^1.15.2"
  }
}

setupTests.js

import Adapter from 'enzyme-adapter-react-16';
import {configure} from 'enzyme';


configure({adapter:new Adapter()})
写回答

1回答

张轩

2020-03-14

同学你好 看提示的意思是说 没有找到这个节点 但是你在这个节点上使用了 .text() 方法,所以它就报错了,加强英语学习哈,这里的错误提示已经很明显啦

1
3
张轩
回复
慕妹5168478
同学 请提供你TotalPrice组件的源代码看一下 谢谢
2020-03-15
共3条回复

React16组件化+测试+全流程 实战在线账本项目

轻松上手,从设计图到上线,精通组件化思维和组件测试

713 学习 · 177 问题

查看课程