I keep on getting this error with flow types and jest:
TypeError: Cannot read property 'returnType' of undefined
at builder (src/index.js:22:195)
at Object.<anonymous> (__test__/index.spec.js:6:53)
at process._tickCallback (internal/process/next_tick.js:103:7)
The following is my entire setup for this:
application code:
// @flow
type BuilderReturnType = {
path: string,
query: string
}
type BuilderOptionsType = {
returnType?: string
}
export default function builder(path: string, {returnType = 'object'}: BuilderOptionsType): BuilderReturnType {
const query = {};
let queryResult = null;
if (returnType === 'string') {
queryResult = doSomething(query);
}
return {
path,
query: queryResult !== null ? queryResult : query
}
}
.babelrc
config:
{
"presets": [
"es2015", "jest"
],
"plugins": [
"transform-object-rest-spread",
"transform-flow-strip-types"
],
"env": {
"test": {
"presets": [
"es2015", "jest"
],
"plugins": [
"transform-object-rest-spread",
"transform-flow-strip-types"
]
}
}
}
jest.json
config:
{
"bail": true,
"verbose": true,
"collectCoverage": true,
"coverageDirectory": "./coverage",
"coverageThreshold": {
"global": {
"branches": 100,
"functions": 100,
"lines": 100,
"statements": 100
}
},
"transform": {
"^.+\\.js$": "<rootDir>/node_modules/babel-jest"
}
}
It seems that there are issues with the method signature using destructuring and flow types at the same time ( BuilderOptionsType
object):
export default function builder(path: string, {returnType = 'object'}: BuilderOptionsType): BuilderReturnType { ... }
If I change {returnType = 'object'}
to options
and then destructure within the method, it seems to work completely fine. With that in mind, is that the only way to allow for using jest
and flow
types together? I'd prefer to be able to destructure in the signature instead of inside the method body.
Looking at your error, you could just not be passing the second argument to the function. If you call builder('foo')
, you'll be attempting to destructure returnType
off of undefined
.
Either call it with the second arg: builder('foo', {})
, or provide a default value for the argument:
export default function builder(path: string, {returnType = 'object'}: BuilderOptionsType = {}): BuilderReturnType { ... }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.