const testAsync = () => Promise.resolve('hello world');
describe('async await test', () => {
test('try 1', async () => {
const result = await testAsync();
expect(result).toBe('hello world');
});
});
async await test
encountered a declaration exception (5ms)
async await test › encountered a declaration exception
ReferenceError: regeneratorRuntime is not defined
4 |
5 | describe('async await test', () => {
> 6 | test('try 1', async () => {
7 | const result = await testAsync();
8 | expect(result).toBe('hello world');
9 | });
Found a way:
In the file jest.config.js set:
setupFiles: ["<rootDir>/jest.setup.js"],
create new file jest.setup.js with this content:
import '@babel/polyfill';
Working!
Hey, Thank you! Do you think that this should not be included?
New versions of node should support this seamplesly I believe :slightly_smiling_face:
I used node version 8.9.4 on Windows 7 and had the problem. Generally, including polyfill makes available using any last ES6/ES7/ES8 semantic innovations in tests.
you are running test with babel-node ?
regeneratorRuntime is used by babel. There is also babel-plugin-regenerator-runtime plugin which can inject this dependency automatically I believe..
Best will be to use simply node instead of babel-node
BTW most of new features are already supported in node 9, but node 8 have native support for most useful features too..
I'm running test with node 8.9.4.
But you use babel/polyfill in webpack despite what feauteres support node.
Figuring out how to make the webdriver.io + mocha work with RSK found another way for solution the issue. Instead of changing jest.config.js and adding jest.setup.js file we:
yarn add --dev @babel/[email protected] @babel/[email protected]
and insert this row:
plugins: [['@babel/plugin-transform-runtime', { polyfill: false }]],
in the .babelrc.js file
yes I too would prefer to not have to compile/use polyfills for my test suite Node v 10.0.0 I still get async
beforeEach(async () => {
^
SyntaxError: Unexpected token (
after using the above solution:
PASS __tests__/utils.js // (test which has no async await)
RUNS __tests__/blocks.js
✨ Done in 5.44s.
so it seems to fail silently... but after a bit more research something is broken with one of the functions, but it fails silently and never tells me why (which is sad).
Most helpful comment
Found a way:
In the file jest.config.js set:
setupFiles: ["<rootDir>/jest.setup.js"],create new file jest.setup.js with this content:
import '@babel/polyfill';Working!