Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Jest needs Babel to test async code?

Tags:

babeljs

jestjs

The Jest "An Async Example" guide starts with:

First, enable Babel support in Jest...

But I miss to see why and where does Jest needs Babel for.

like image 859
atoledo Avatar asked Apr 01 '19 20:04

atoledo


1 Answers

Node.js has supported async functions by default since version 7.6.0, so (as you suspected) Babel is not needed for Jest to run tests using async functions.

I just confirmed this by installing only Jest v24.6.0 and ran this test with Node.js v10.15.1:

test('hi', async () => {
  const val = await Promise.resolve('hello');
  expect(val).toBe('hello');
});

...and it passed just fine.


On the other hand, Babel is required to use ES6 module syntax.

Many of the examples in the "An Async Example" doc use ES6 module syntax (export default ..., import * as ..., etc.) so Babel is required for any of those examples to work.

like image 75
Brian Adams Avatar answered Oct 03 '22 08:10

Brian Adams