maoooricio
maoooricio

Reputation: 2259

Trying to use async/await in mocha

I want to use async/await in mocha in order to make my tests. I have read many post, but I didn't find the solution. I have already install all the babel modules in order to transpiling the code, but it doesn't work.

Here is my code inside the "test" folder:

import test from 'mocha'
import 'babel-polyfill'
import { expect } from 'chai'
import { assert } from 'chai'
import utils from '../lib/utils'

describe('long number', function () {
  it("Sample", mochaAsync(async () => {
    var x = utils.longNums(0);
    expect(x).to.equal(5000);
  }))
})

Here is my package.json where I am using all the babel dependencies and plugins that I have read I have to install, and my test script where I suggest to mocha to use the babel transpiling

   {
     "name": "pos_lisa-test",
     "version": "1.0.0",
     "description": "pos lisa test",
     "main": "index.js",
     "scripts": {
       "test": "mocha --compilers js:babel-core/register ./src/**/*.test.js"
     },
     "standard": {
       "parser": "babel-eslint"
     },
     "babel": {
       "presets": [
         "es2015",
         "react"
       ]
     },
     "keywords": [
       "test"
     ],
     "author": "Mauricio",
     "license": "MIT",
     "devDependencies": {
       "babel-core": "^6.23.1",
       "babel-eslint": "^7.1.1",
       "babel-plugin-transform-async-to-generator": "^6.22.0",
       "babel-preset-es2015": "^6.22.0",
       "babel-preset-react": "^6.23.0",
       "chai": "^3.5.0",
       "mocha": "^3.2.0",
     },
     "plugins": [
       "transform-async-to-generator"
     ],
     "dependencies": {
       "babel-polyfill": "^6.23.0"
     }
   }

And the error that I get is the following

it('should remove items that don\'t evaluate to true when passed to predicate function', async function () {
                                                                                           ^^^^^
SyntaxError: missing ) after argument list

What I am doing wrong? In advance thanks a lot for your help

Upvotes: 3

Views: 3225

Answers (2)

Charles Merriam
Charles Merriam

Reputation: 20520

According to the Tao of Javascript, "Code flows in the moment, so knowledge is but a hint, like the map of a stream."

As of April, 2017, having the 'transform-async-to-generator' will actually cause problems.

As a more general note, every async function returns a promise, or casts its return value and exception to a promise. It is usually cleaner to test the promise and not have your test call await:

it('should have no drops left', () => 
  ocean.listDrops().should.eventually.have.length(0));

Upvotes: 0

Michael Jungo
Michael Jungo

Reputation: 32992

You have added "plugins": ["transform-async-to-generator"]" to the top level of your package.json, but it should be inside the "babel" section. Change it to:

"babel": {
  "presets": [
    "es2015",
    "react"
  ],
  "plugins": [
    "transform-async-to-generator"
  ]
},

Upvotes: 1

Related Questions