首页 > 解决方案 > 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!

标签: typescriptecmascript-6jestjsts-jest

解决方案


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).


推荐阅读