dsherret / ts-nameof

nameof in TypeScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

running jest with babel plugin misses type arguments and throws

zucchinho opened this issue · comments

Problem

React native project, with typescript. Have installed the babel-plugin-ts-nameof and set it up via babel.config.js, and it seems to work flawlessly for compilation for a real build of the app. However have come across an issue where it is not working during compilation of unit tests, which are run with jest and compiled with ts-jest. i have set the corresponding option in jest.config.js:

globals: { 'ts-jest': { babelConfig: true, diagnostics: { warnOnly: true, }, }, }

This is my babel config:
{ presets: ['module:metro-react-native-babel-preset'], env: { production: {}, }, plugins: [ [ '@babel/plugin-proposal-decorators', { legacy: true, }, ], ['@babel/plugin-proposal-optional-catch-binding'], [ 'babel-plugin-inline-import', { extensions: ['.html'], }, ], ['babel-plugin-ts-nameof'], ], sourceMaps: true, }

Reproduce

Simple test:
it('should correctly recoginize type arguments in nameof transform', () => { type TThing = { something: 'yes' }; expect(nameof<TThing>()).toBe('TThing'); });

Current behaviour

  • The above test throws error during babel compilation:

Call expression must have one argument or type argument: nameof()

at Object.throwErrorForSourceFile

(node_modules/@ts-nameof/common/dist/errors.js:8:11)
at PluginPass.CallExpression (node_modules/@ts-nameof/transforms-babel/dist/index.js:19:33)
at newFn (node_modules/@babel/core/node_modules/@babel/traverse/lib/visitors.js:179:21)
at NodePath._call (node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:55:20)
at NodePath.call (node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:42:17)
at NodePath.visit (node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:90:31)
at TraversalContext.visitQueue (node_modules/@babel/core/node_modules/@babel/traverse/lib/context.js:112:16)
at TraversalContext.visitMultiple (node_modules/@babel/core/node_modules/@babel/traverse/lib/context.js:79:17)
at TraversalContext.visit (node_modules/@babel/core/node_modules/@babel/traverse/lib/context.js:138:19)
at Function.traverse.node (node_modules/@babel/core/node_modules/@babel/traverse/lib/index.js:84:17)

Expected behaviour

  • The nameof transform is parsed successfully, as it does during the regular babel compilation, and the test runs (and passes)

Version

babel-plugin-ts-nameof: 4.2.1

It is possible this is somehow a ts-jest issue, as it seems odd that it should compile fine during the normal react native build, but not here. Now that I think about it, it seems likely ts-jest is stripping out the type arguments before the calling the babel plugins.

Okay, I overcame this issue pretty easily. Just installed 'ts-nameof' package, and added line to jest config, as per the docs

globals: { 'ts-jest': { babelConfig: true, astTransformers: ['ts-nameof'], diagnostics: { warnOnly: true, }, }, },

I don't think there's any drawbacks to doing it this way, so will close.