使用 ES6 和 Typescript 开玩笑

Jest with ES6 and Typescript

我目前正在尝试使用 Supertest 和 Jest 测试我的 Express apis,但是我 运行 遇到了一个我似乎无法找到答案的问题。

这是我的api。

router.get('/', async (req: Request, res: Response) => {
  const products: ProductType[] = await MongooseModel.find({});
  res.send(products);
});

这是我的测试套件。

import request from 'supertest';
import products from './products';

describe('GET /', () => {
  it('responds with status 200', async () => {
    const response = await request(products).get('/');
    expect(response.statusCode).toBe(200);
  });
});

这是我的 babel.config.js

export const presets = [
  ['@babel/preset-env', { targets: { node: 'current' } }],
  +'@babel/preset-typescript',
  +'@babel/plugin-transform-modules-commonjs',
];

当我尝试 运行 测试时,我得到了这样的回复:

.presets[1] must be a string, object, function

谁能帮我解决这个令人头疼的问题?

谢谢!

删除预设中无意义的++'@babel/preset-typescript' 是一个数值为 NaN 的数字,它既不是字符串、对象也不是函数。 '@babel/preset-typescript'(以及下一个)可能就是你想要的。

致智者的话:

+ 是遵循 Jest documentation for Typescript support 的结果。如果你按下他们的按钮来复制他们拥有的 babel.config.js 代码片段,它也会获取他们的格式规则,包括 + (他们用它来说明添加行 "@babel/preset-typescript"样板 Babel 配置的顶部)。