Skip to content

TypeError: Cannot read property 'tokens' of undefined #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dluksza opened this issue May 24, 2018 · 8 comments
Closed

TypeError: Cannot read property 'tokens' of undefined #62

dluksza opened this issue May 24, 2018 · 8 comments

Comments

@dluksza
Copy link

dluksza commented May 24, 2018

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
Copy link
Owner

ds300 commented May 24, 2018 via email

@dluksza
Copy link
Author

dluksza commented May 25, 2018

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
Copy link
Owner

ds300 commented May 25, 2018

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
Copy link
Owner

ds300 commented May 25, 2018

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

In which case you'll need to revert to [email protected]

@ds300
Copy link
Owner

ds300 commented May 25, 2018

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.

@dluksza
Copy link
Author

dluksza commented May 25, 2018

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

@dluksza
Copy link
Author

dluksza commented May 25, 2018

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

@dluksza dluksza closed this as completed May 25, 2018
@ds300
Copy link
Owner

ds300 commented May 25, 2018

Alright 1.2.9 is the same as 1.2.5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants