我目前正在尝试使用 Supertest 和 Jest 测试我的 Express api,但是我遇到了一个我似乎找不到答案的问题。
这是我的 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
谁能帮我解决这个令人头疼的问题吗?
谢谢!
最佳答案
对智者的话:
+
是遵循 Jest documentation for Typescript support 的结果。如果您按下他们的按钮来复制他们拥有的 babel.config.js
代码段,它也会获取他们的格式规则,包括 +
(他们用它来说明在样板 Babel 配置顶部添加行 "@babel/preset-typescript"
)。
关于typescript - 用 ES6 和 Typescript 开 Jest ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67708049/