0

我正在尝试在 React 应用程序中调试我的打字稿测试,使用ts-jest.

我的项目是用create-react-app. 我可以使用提供的完美运行我的打字稿测试"test": "react-scripts-ts test --env=jsdom"

但是为了调试它们,我尝试在 package.json 中添加一个脚本条目,如下所示: "test:debug": "node --inspect-brk ./node_modules/jest/bin/jest.js --config=jest.config.debug.json"

当我像这样运行时:npm run test:debug我收到预期的消息: Debugger listening on ws://127.0.0.1:9229/bc733a7d-01d8-42eb-a18d-e59fe30a6393 但是当我连接 Chrome DevTools 时,我单击运行,然后,我的许多测试运行并通过,但是当我运行可能是我在 . test.tsx 文件。

 PASS  src/chrome/ChromeBrowserController.test.ts
 PASS  src/utils/session-string-parser.test.ts
 PASS  src/model/mutators/WindowAndTabMutator.test.ts
 PASS  src/utils/initialise-fake-chrome-api.test.ts
 PASS  src/factory/BananaFactory.test.ts
 PASS  src/model/DefaultSessionProvider.test.ts
 PASS  src/serialisation/MarkdownSerialisation.test.ts
 PASS  src/serialisation/JSONSerialisation.test.ts
 FAIL  src/BananaTabs.test.tsx
  ● 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/julian/work/personal/bananatabs/src/view/icons/share.svg:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){<?xml version="1.0" encoding="iso-8859-1"?>
                                                                                             ^

    SyntaxError: Unexpected token <

      14 |      rename: boolean;
      15 |      delete: boolean;
    > 16 |  };
         |            ^
      17 |  onRenameAction?(): void;
      18 |  onDeleteAction?(): void;
      19 |  onCopyAction?(): void;

      at ScriptTransformer._transformAndBuildScript (node_modules/jest/node_modules/jest-runtime/build/script_transformer.js:403:17)
      at Object.<anonymous> (src/view/TabToolsView.tsx:16:12)

Test Suites: 1 failed, 1 skipped, 10 passed, 11 of 12 total
Tests:       23 skipped, 64 passed, 87 total
Snapshots:   0 total
Time:        4.848s, estimated 7s
Ran all test suites.

该错误指向另一个 .tsx 文件,它是一个 React 组件,而不是一个测试。

这是我的内容jest.config.debug.json

{
  "preset": "ts-jest",
  "transformIgnorePatterns": ["node_modules/"]
}

我究竟做错了什么?有任何想法吗?

4

1 回答 1

1

In your Jest config, you need to stub some files that Jest doesn't understand.

https://jestjs.io/docs/en/webpack

moduleNameMapper: { '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': '<rootDir>/config/jest/mock/fileMock.js', '\\.(css|less)$': 'identity-obj-proxy', }

fileMock.js:

module.exports = 'test-file-stub';

于 2018-12-17T09:00:09.490 回答