typescript - 开玩笑不转换导入的打字稿
问题描述
我没有使用 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 配置中有正确的设置。
解决方案
推荐阅读
- python - 数据框拆分列值,如何解决错误消息?
- css - 如何在 React 中更改引导导航栏切换器
- reactjs - 在 react-three-fiber 和 NextJS 中加载 bm-font
- javascript - 使用 CSS 和 jQuery 添加上一个/下一个按钮以滚动容器
- javascript - 需要有人帮助我处理数组中的 javascript 数组
- linux - ec2 实例上的实例可达性检查失败
- html - 带有 5 个盒子的 flexbox
- c++ - 使用 Windows 挂钩时出现滞后
- python - 在settings.py中,如果环境变量只在一个环境中使用,应该如何加载/设置?
- java - SwipeRefreshLayout 后数据重复