首页 > 解决方案 > 开玩笑不转换导入的打字稿

问题描述

我没有使用 ts-jest。根据本指南,我不必为了测试我的打字稿模块。

我有一个带有 jest 和 typescript 的简单项目设置。您可以在此处查看代码

当我运行时jest,我总是收到如下错误:

[ts-test] jest
 FAIL  src/__test__/index.spec.ts
  ● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    /Users/tconn/dev/ts-test/src/index.ts:9
      static type = "something";
                  ^

    SyntaxError: Unexpected token =

    > 1 | import Animal from '../index'
        | ^
      2 |
      3 | describe('generate', () => {
      4 |   it('does something', () => {

      at ScriptTransformer._transformAndBuildScript (../../../../usr/local/lib/node_modules/jest/node_modules/@jest/transform/build/ScriptTransformer.js:451:17)
      at Object.<anonymous> (src/__test__/index.spec.ts:1:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.411s
Ran all test suites.

本质上,导入的类似乎没有被转译为 JS,但测试是。正如开玩笑的网站所示,我的 babel 配置中有正确的设置。

标签: typescriptjestjsbabeljs

解决方案


推荐阅读