typescript - Jest with ES6 and Typescript
问题描述
I'm currently trying to test my Express apis with Supertest and Jest however I'm running into a problem that I can't seem to find the answer to.
This is my api.
router.get('/', async (req: Request, res: Response) => {
const products: ProductType[] = await MongooseModel.find({});
res.send(products);
});
This is my test suite.
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);
});
});
And this is my babel.config.js
export const presets = [
['@babel/preset-env', { targets: { node: 'current' } }],
+'@babel/preset-typescript',
+'@babel/plugin-transform-modules-commonjs',
];
When I try to run the tests, I get this response:
.presets[1] must be a string, object, function
Could anyone please help me figure this headache out?
Thanks!
解决方案
Word to the wise:
The +
is the result of following the Jest documentation for Typescript support. If you press their button to copy the babel.config.js
snippet they have, it grabs their formatting rules as well, including the +
(which they are using to illustrate the addition of the line "@babel/preset-typescript"
on top of the boilerplate Babel configuration).
推荐阅读
- java - 如何将参数从 Spring Boot 传递到 Spring Batch
- xamarin.forms - ToolbarItem 显示的是一个点而不是图像?
- hazelcast - 使用hazelcast作为hibernate的二级缓存时如何查看查询缓存统计信息
- ios - 委托返回 nil,不在 ViewController 中,而是在委托创建类中
- python - 拆分总和K方式组合python
- node.js - 如何使用类在 node.js 项目中正确使用 Promise?
- c# - 无法单击具有特定属性的按钮
- python - 如何更优雅地构造一个 2 的幂数的 numpy 数组?
- r - sparklyr 的包或命名空间加载失败
- python - 如何获得一套字典?