Skip to content
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

Traceur not working with Jest. Unable to use ES6. #97

Closed
licx opened this issue Jul 24, 2014 · 10 comments
Closed

Traceur not working with Jest. Unable to use ES6. #97

licx opened this issue Jul 24, 2014 · 10 comments

Comments

@licx
Copy link

licx commented Jul 24, 2014

My codebase is written in ES6 so I tried to set-up Traceur as scriptPreprocessor

Package.json

"jest": {
    "scriptPreprocessor": "<rootDir>/preprocessor.js"
}

preprocessor.js

var traceur = require('traceur');
module.exports = {
  process: function(src, path) {
    var result = traceur.compile(src);
    return result;
  }
};

When I try to run my tests with npm test I always get:

[Error: Malformed child response message: {}]
@JogoShugh
Copy link

Any luck on this?

@hysios
Copy link

hysios commented Oct 26, 2014

package.json

"jest":  {
    "setupEnvScriptFile": "<rootDir>/node_modules/traceur/bin/traceur-runtime.js",
}

preprocessor.js

var traceur = require('traceur');
module.exports = {
  process: function(src, path) {
    if (path.indexOf('node_modules') === -1) {
      src = traceur.compile(src);
    }
    return src;
 }
};

You can try add above code to test.

@dmaj7no5th
Copy link

No luck doing the same. When I try to run the traceur compiler in a preprocess function, I get the following error:

Variable 'require' has already been declared
node_modules/traceur/bin/traceur.js:22696

@dmaj7no5th
Copy link

I'm trying to combine React + ES6 with Jest, and can't get beyond the following:

// in package.json
  "jest": {
    "setupEnvScriptFile": "node_modules/traceur/bin/traceur-runtime.js",
    "scriptPreprocessor": "jest.conf.js",
    "unmockedModulePathPatterns": [
      "node_modules/react"
    ]
  }

// jest.conf.js
var ReactTools = require('react-tools'),
    traceur = require('traceur');
module.exports = {
    process: function (src, path) {
        if (path.indexOf('node_modules') === -1) {
            src = traceur.compile(
                ReactTools.transform(src),
                { experimental: true }
            );
        }
        return src;
    }
};

When I try to run the tests, I get the following error:

Found 1 matching test...
/Users/pperry/example/node_modules/traceur/bin/traceur.js:22696 

  var require = function(id) { 
      ^^^^^^^ 
 FAIL  app/js/models/__tests__/example-class-test.js 

SyntaxError: /Users/pperry/example/jest.conf.js: Variable 'require' has already been declared

The module I'm trying to test exports an ES6 class (it has no imports). The test just tries to create an instance of the class and verify a property on it. No matter how I wire it, when I get Traceur involved, I hit this error.

@jamiebuilds
Copy link
Contributor

Hey guys, contributor to 6to5 here. I'm not sure how to get traceur working, but 6to5 has an official jest plugin to make it easier. It also plays well with react/jsx if you're using those. If you need any help let me know or feel free to stop by our gitter room.

@randylien
Copy link

If you are working with React, react-tools supports ES6 transform.

var ReactTools = require('react-tools');
module.exports = {
    process: function(src) {
        return ReactTools.transform(src, {
            harmony: true
        });
    }
};

@jamiebuilds
Copy link
Contributor

@randylien You can get much more ES6 support in addition to full React/JSX support in 6to5. http://6to5.org/docs/usage/jsx/

@leebyron
Copy link
Contributor

leebyron commented Jan 8, 2015

It may be failing if you're not also including the Traceur runtime.

Your transform should also concat the traceur runtime to the top of the file.

I have no idea where the error about defining require is coming from. That seems like a bad idea on Traceur's part if they're doing that.

@licx
Copy link
Author

licx commented Feb 2, 2015

Looks like this is how you can use ES6 with React

package.son

  "dependencies": {
    "react": "*",
    "react-tools": "*"
  },
  "jest": {
    "scriptPreprocessor": "<rootDir>/preprocessor.js",
    "unmockedModulePathPatterns": ["<rootDir>/node_modules/react"]
  }

preprocessor.js

var ReactTools = require('react-tools');
module.exports = {
    process: function(src) {
        return ReactTools.transform(src, {
            harmony: true
        });
    }
};

It's basically the same procedure shown here http://facebook.github.io/jest/docs/tutorial-react.html#content with the addition of the harmony flag to the ReactTools.transform call.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants