ds300/react-native-typescript-transformer

TypeError: Cannot read property 'tokens' of undefined

dluksza opened this issue · 8 comments

I'm using jest-ts-preprocessor.js from #39 (comment) and after upgrading to version 1.2.6 I'm getting this error:

TypeError: Cannot read property 'tokens' of undefined

      18 |       src
      19 |     }).ast;
    > 20 |     return generate.default(
      21 |       ast,
      22 |       {
      23 |         filename: file,
      
      at new Generator (node_modules/babel-generator/lib/index.js:51:22)
      at Object.exports.default (node_modules/babel-generator/lib/index.js:19:13)
      at Object.process (tools/jest-ts-preprocessor.js:20:28)
ds300 commented

Now I do wait for the transformer to resolve, but the ast property of result doesn't have code field nor is a string. Therefore I'm getting this error:

TypeError: Jest: a transform's `process` function must return a string, or an object with `code` key containing this string.

      at ScriptTransformer.transformSource (node_modules/jest-runtime/build/script_transformer.js:316:15)
ds300 commented

Yes. As mentioned here you need to use babel-generator to render the ast as a string.

Adapting the code in that comment to be async:

var tsTransformer = require('react-native-typescript-transformer')
var rnTransformer = require('react-native/jest/preprocessor')
var generate = require('babel-generator')

var preprocessor = Object.assign({}, rnTransformer, {
  async process (src, file) {
    const {ast} = await tsTransformer.transform({
      filename: file,
      localPath: file,
      options: {
        dev: true,
        platform: '',
        projectRoot: '',
      },
      src,
    })

    return generate.default(ast, {
      filename: file,
      retainLines: true
    }, src)
  },
})

module.exports = preprocessor

(I haven't tested this)

ds300 commented

Argh, it might not be possible: jestjs/jest#2711

In which case you'll need to revert to react-native-typescript-transformer@1.2.5

ds300 commented

Actually I'm going to revert to that myself. Updating source-map has raised quite a few issues and I don't believe the alleged perf wins are worth the extra work it has caused for me and other people whose builds it broke.

This looks same as my solution, and it produces the same error.

OK, will revert to 1.2.5. Thanks for the help and quick responses!

ds300 commented

Alright 1.2.9 is the same as 1.2.5.