Jest: Flow type support?

Created on 11 Nov 2015  路  17Comments  路  Source: facebook/jest

I'm trying to use flow syntax within unit tests, but i don't see any way to test it

 - Error: Line 5: Unexpected token :
        at createError (node_modules/jest-cli/node_modules/istanbul/node_modules/esprima/esprima.js:2392:21)
        at unexpectedTokenError (node_modules/jest-cli/node_modules/istanbul/node_modules/esprima/esprima.js:2467:13)
        at throwUnexpectedToken (node_modules/jest-cli/node_modules/istanbul/node_modules/esprima/esprima.js:2472:15)
        at parseClassBody (node_modules/jest-cli/node_modules/istanbul/node_modules/esprima/esprima.js:5045:21)
        at parseClassDeclaration (node_modules/jest-cli/node_modules/istanbul/node_modules/esprima/esprima.js:5067:21)
        at parseStatementListItem (node_modules/jest-cli/node_modules/istanbul/node_modules/esprima/esprima.js:3935:24)
        at parseFunctionSourceElements (node_modules/jest-cli/node_modules/istanbul/node_modules/esprima/esprima.js:4766:23)
        at parseFunctionExpression (node_modules/jest-cli/node_modules/istanbul/node_modules/esprima/esprima.js:4972:16)
        at parsePrimaryExpression (node_modules/jest-cli/node_modules/istanbul/node_modules/esprima/esprima.js:3232:24)
        at inheritCoverGrammar (node_modules/jest-cli/node_modules/istanbul/node_modules/esprima/esprima.js:2648:18)

Most helpful comment

_.babelrc_

{
  "presets": [
    "flow"
  ]
}

That's all.

All 17 comments

I suggest using babel-jest to preprocess your files and strip flow-annotations before tests are being run on them. Follow the guide for React testing on the bottom here: http://facebook.github.io/jest/docs/tutorial-react.html#content

(Note: we are in the process of upgrading babel-jest to babel6. For now I recommend using babel-jest 5 until we have ironed out the issues).

Thanks, problem was exactly with usage of babel 6 ! Babel-jest 5 works like a charm.

@cpojer, that tutorial didn't explain how to install the babel plugin transform-flow-strip-types. Could we add a note?

I think we should add some notes for this somewhere. cc @hramos.

FWIW Here is the transformer I put together to get jest running with transform-flow-strip-types after npm install --save-dev babel-plugin-transform-flow-strip-types

const babelJest = require('babel-jest');
const jestPreset = require('babel-preset-jest');

module.exports = babelJest.createTransformer({
    plugins: [
        "transform-flow-strip-types"
    ],
    presets: [jestPreset]
});

Ooof, I wasn't aware this was an issue people might need help with. If you still think this should be documented, feel free to open a new issue and assign it to me.

The only reason I commented here (which I now realize was closed, my bad) is the last comment saying "yeah, we should document this!"

Yeah! That is on me to document. Unfortunately it's not been on my mind as the issue is closed.

@thomb transform-flow-strip-types plugin (or flow preset) just works with Jest when added to your Babel config.

Hi @thymikee, do you have a sample? I imagine that requires several configurations for prod, dev and testing.

_.babelrc_

{
  "presets": [
    "flow"
  ]
}

That's all.

Is it exepected that this doesn't work with jest?

export default class {
  urlsToVisit: { [string]: boolean } = {};
}

Error being Unexpected token:

image

Flow is supported, if you are getting this error, it's because you didn't properly set up your babel build pipeline.

@cpojer Well I'm using the flow preset, and I think it's an error specific to jest, given it works fine when I just use yarn run flow.

@aksonov
Finally, can you tell me how to solve this problem?

I am adding a jest feature to unity tests, @thomb, compilation broken. It does not work for me. am I missing anything?

I have the same problem as @rayrutjes. Is there any solution to it?

Was this page helpful?
0 / 5 - 0 ratings