我没有使用ts-jest。 According to this guide,我不必测试我的打字稿模块。
我有一个带有笑话和打字稿的简单项目设置。 You can see the code here。
运行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配置中设置了正确的设置。