加了类型断言反而测试不通过了

来源:5-5 添加Button 测试代码 第二部分

_麦当

2021-03-09

yarn run v1.22.10
$ jest
 FAIL  src/components/Button/button.test.tsx
  ● Test suite failed to run

    SyntaxError: /Users/mj/Documents/learning/downzoo-ui/src/components/Button/button.test.tsx: Missing semicolon (24:45)

      22 |   it('should render the correct default component', () => {
      23 |     const wrapper = render(<Button {...defaultProps}>Nice</Button>)
    > 24 |     const element = wrapper.getByText('Nice') as HTMLButtonElement
         |                                              ^
      25 |     expect(element).toBeInTheDocument();
      26 |     expect(element?.tagName).toEqual('BUTTON');
      27 |     expect(element.disabled).toBeFalsy();

      at _temp._raise (node_modules/@babel/parser/src/parser/error.js:97:45)
      at _temp.raiseWithData (node_modules/@babel/parser/src/parser/error.js:92:17)
      at _temp.raise (node_modules/@babel/parser/src/parser/error.js:41:17)
      at _temp.semicolon (node_modules/@babel/parser/src/parser/util.js:131:10)
      at _temp.parseVarStatement (node_modules/@babel/parser/src/parser/statement.js:707:10)
      at _temp.parseStatementContent (node_modules/@babel/parser/src/parser/statement.js:223:21)
      at _temp.parseStatement (node_modules/@babel/parser/src/parser/statement.js:163:17)
      at _temp.parseStatement (node_modules/@babel/parser/src/plugins/flow/index.js:1815:26)
      at _temp.parseBlockOrModuleBlockBody (node_modules/@babel/parser/src/parser/statement.js:880:25)
      at _temp.parseBlockBody (node_modules/@babel/parser/src/parser/statement.js:856:10)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        2.725 s, estimated 3 s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Process finished with exit code 1


加上类型断言(as HTMLButtonElement)我虽然eslint不会报错,但是jest跑不通过,当我把类型断言去掉以后,虽然eslint会标红,jest倒是能跑通,请问要怎么解决呀

写回答

2回答

张轩

2021-03-10

同学你好 你跑测试的时候使用的是哪个命令? 是 npm run test 吗? 你运行测试以后也爆出的是一个不存在的 lint 错误,有点奇怪?可以把你的代码库(git)提供给我,我本地帮你跑跑看

1
0

_麦当

提问者

2021-03-09

{
  "name": "downzoo-ui",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@babel/plugin-syntax-flow": "^7.12.13",
    "@babel/plugin-syntax-jsx": "^7.12.13",
    "@babel/preset-flow": "^7.12.13",
    "@babel/preset-react": "^7.12.13",
    "@testing-library/user-event": "^12.1.10",
    "@types/node": "^12.0.0",
    "@types/react": "^17.0.0",
    "@types/react-dom": "^17.0.0",
    "classnames": "^2.2.6",
    "jest-dom": "^4.0.0",
    "node-sass": "^5.0.0",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-scripts": "4.0.3",
    "typescript": "^4.1.2",
    "web-vitals": "^1.0.1"
  },
  "babel": {
    "presets": [
      "@babel/preset-react",
      "@babel/preset-env",
      "@babel/preset-flow"
    ],
    "plugins": [
      "@babel/plugin-transform-react-jsx"
    ]
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "jest",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@testing-library/jest-dom": "^5.11.9",
    "@testing-library/react": "^11.2.5",
    "@types/classnames": "^2.2.11"
  }
}

我的package.json

0
0

React18+TS高仿AntD从零到一打造组件库

设计,开发,测试,发布再到 CI/CD,从0到1造轮子

2123 学习 · 959 问题

查看课程