Jest测试typescript文件语法错误:"interface是严格模式下的保留字"

10

我的分支: https://github.com/Futuratum/moonholdings.io/tree/JestTests

当前PR: https://github.com/Futuratum/moonholdings.io/pull/29

enter image description here

我的Astronaut.tsx组件

import React from 'react';

import { moonHoldings } from '../../shared/models';
import { astronaut } from '../../styles';

const { AstronautContainer, Heading } = astronaut;

interface LogoCheck {
  showLogo: boolean;
}

export default (showLogo: LogoCheck) =>  (
  <AstronautContainer>
    { showLogo.showLogo === true ? <Heading>{moonHoldings}</Heading> : null }
    <img src="static/astronaut.png" alt="astronaut" />
  </AstronautContainer>
);

简单测试:

import React from 'react';
import { shallow } from 'enzyme';
import toJson from 'enzyme-to-json';

import Astronaut from '../components/Astronaut/Astronaut.tsx';

describe('<Astronaut /> component', () => {
  console.log('Astronaut', Astronaut);
  describe('when rendering', () => {
    const wrapper = shallow(<Astronaut showLogo={true} />);

    it('should render a component matching the snapshot', () => {
      const tree = toJson(wrapper);
      expect(tree).toMatchSnapshot();
      expect(wrapper).toHaveLength(1);
    });
  });
});

SyntaxError: /Users/leongaban/projects/Futuratum/moonholdings.io/components/Astronaut/Astronaut.tsx:在严格模式下,interface是一个保留字(第8行,第0个字符)。

enter image description here

我尝试将tsconfig中的compilerOptions中的strict改为false,但这并没有起到帮助作用。
其他人在使用Jest测试和Typescript时遇到过这个问题吗?

package.json

{
  "name": "moonholdings.io",
  "version": "2.0.0",
  "description": "Moonholdings.io",
  "main": "index.ts",
  "scripts": {
    "dev": "next -p 7777",
    "build": "next build",
    "start": "next start -p 8000",
    "test": "NODE_ENV=test jest --watch",
    "test-win": "SET NODE_ENV=test&& jest --watch",
    "heroku-postbuild": "next build"
  },
  "author": "Futuratum",
  "license": "ISC",
  "dependencies": {
    "@zeit/next-sass": "^1.0.1",
    "@zeit/next-typescript": "^1.1.1",
    "apollo-boost": "^0.1.16",
    "apollo-client": "^2.4.2",
    "decko": "^1.2.0",
    "downshift": "^2.2.3",
    "enzyme": "^3.6.0",
    "enzyme-adapter-react-16": "^1.5.0",
    "graphql": "^14.0.2",
    "graphql-tag": "^2.9.2",
    "graphql-tools": "^4.0.0",
    "lodash.debounce": "^4.0.8",
    "next": "^7.0.2",
    "next-with-apollo": "^3.1.3",
    "node-sass": "^4.11.0",
    "nprogress": "^0.2.0",
    "prop-types": "^15.6.2",
    "react": "^16.7.0",
    "react-adopt": "^0.6.0",
    "react-apollo": "^2.2.1",
    "react-dom": "^16.7.0",
    "react-transition-group": "^2.5.0",
    "styled-components": "^3.4.9",
    "tslint": "^5.12.1",
    "tslint-react": "^3.6.0",
    "typescript": "^3.2.4",
    "waait": "^1.0.2"
  },
  "devDependencies": {
    "@babel/plugin-proposal-decorators": "^7.3.0",
    "@babel/preset-typescript": "^7.1.0",
    "@types/enzyme": "^3.1.15",
    "@types/jest": "^23.3.13",
    "@types/next": "^7.0.6",
    "@types/react": "^16.7.20",
    "@types/react-dom": "^16.0.11",
    "@types/react-redux": "^7.0.0",
    "@types/zeit__next-typescript": "^0.1.1",
    "babel-core": "^7.0.0-bridge.0",
    "babel-jest": "^23.6.0",
    "babel-plugin-styled-components": "^1.7.1",
    "casual": "^1.5.19",
    "enzyme-to-json": "^3.3.4",
    "jest": "^23.6.0",
    "jest-transform-graphql": "^2.1.0"
  },
  "jest": {
    "setupTestFrameworkScriptFile": "<rootDir>/jest.setup.js",
    "testPathIgnorePatterns": [
      "<rootDir>/.next/",
      "<rootDir>/node_modules/"
    ],
    "transform": {
      "\\.(gql|graphql)$": "jest-transform-graphql",
      ".*": "babel-jest",
      "^.+\\.js?$": "babel-jest"
    }
  }
}

我的 .babelrc 文件

{
  "env": {
    "development": {
      "presets": [
        "next/babel",
        "@zeit/next-typescript/babel"
      ],
      "plugins": [
        [
          "styled-components",
          {
            "ssr": true,
            "displayName": true
          }
        ],
        [
          "@babel/plugin-proposal-decorators",
          {
            "legacy": true
          }
        ]
      ]
    },
    "production": {
      "presets": [
        "next/babel",
        "@zeit/next-typescript/babel"
      ],
      "plugins": [
        [
          "styled-components",
          {
            "ssr": true,
            "displayName": true
          }
        ],
        [
          "@babel/plugin-proposal-decorators",
          {
            "legacy": true
          }
        ]
      ]
    },
    "test": {
      "presets": [
        [
          "next/babel",
          {
            "preset-env": {
              "modules": "commonjs"
            }
          }
        ]
      ],
      "plugins": [
        [
          "styled-components",
          {
            "ssr": true,
            "displayName": true
          }
        ]
      ]
    }
  }
}

1
似乎你的 TypeScript 没有编译。你使用 ts-jest 吗? - Stramski
我已经安装了这个软件包,但是你怎么使用它呢 @Stramski - Leon Gaban
以下是我在 package.json 中的一段代码片段 => "jest": { "globals": { "ts-jest": { "tsConfig": "tsconfig_renderer.json" } }, "moduleFileExtensions": [ "ts", "tsx", "js", "jsx" ], "roots": [ "<rootDir>/test" ], "setupFiles": [ "<rootDir>/test/test-setup/test-setup.ts" ], "transform": { ".(ts|tsx)": "ts-jest" }, "testRegex": "(/test/.*)(.test.ts|.test.tsx|.test.js)$" }, - Stramski
@Stramski 好的,我尝试添加了你的转换和testRegex行,但现在它找不到任何测试。未找到测试 - Leon Gaban
因为我的正则表达式与你的测试不匹配 ;) - Stramski
3个回答

26

在我添加了这个 @babel/preset-typescript 到我的预设中之后,那个错误消失了:

"test": {
  "presets": [
    "@babel/preset-typescript",  // <---
    [
      "next/babel",
      {
        "preset-env": {
          "modules": "commonjs"
        }
      }
    ]
  ],
  "plugins": [
    [
      "styled-components",
      {
        "ssr": true,
        "displayName": true
      }
    ]
  ]
}

0
如果仅使用 @babel/typescript 预设不能解决问题,您可以尝试以下方法:
  1. 将失败的测试文件重命名为 .ts.tsx(取决于您的测试中是否有 JSX,在这种情况下为 Astronaut.test.tsx
  2. 更新 jest 配置docs(在这种情况下为 package.json),添加
"moduleFileExtensions": [
    "js",
    "ts",
    "tsx"
],

-2

您的常量声明没有变量名。 const 变量名: 类型 = new 类型() 或者 const 变量名: 类型; 或者 const 变量名 = new 类型;


4
这句话的意思是:你是指这一行代码吗? const { AstronautContainer, Heading } = astronaut; 我正在从 astronaut 样式文件中解构出这两个变量名。 - Leon Gaban

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接