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

[babel-jest] Failures on decorators with async methods #5326

Closed
julien-f opened this issue Jan 16, 2018 · 7 comments · Fixed by #5594
Closed

[babel-jest] Failures on decorators with async methods #5326

julien-f opened this issue Jan 16, 2018 · 7 comments · Fixed by #5594

Comments

@julien-f
Copy link

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

Syntax error when using decorators on async methods:

$ jest index.spec.js
 FAIL  ./index.spec.js
  ● Test suite failed to run

    index.spec.js:6
        bar() {}}, (_applyDecoratedDescriptor(_class.prototype, "bar", [foo], Object.getOwnPropertyDescriptor(_class.prototype, "bar"), _class.prototype)), _class);
        ^^^

    SyntaxError: Unexpected identifier

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:318:17)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.459s
Ran all test suites.

Repro repo: https://github.com/julien-f/issue-babel-jest-decorators-async-methods

What is the expected behavior?

Should compile and work as if it were previously processed by @babel/cli.

@modosc
Copy link

modosc commented Jan 16, 2018

we're seeing this too. i wonder if this has anything to do with jest-runtime pulling in babel-core ^6.0.0?

@modosc
Copy link

modosc commented Jan 16, 2018

this seems to go away if we use @babel/plugin-transform-async-to-generator even though that shouldn't be necessary with our current node version (8.9)

@SimenB
Copy link
Member

SimenB commented Jan 26, 2018

Hah, this is weird. You've found a bug in Babel caused by our use of retainLines: https://github.com/facebook/jest/blob/c243f67efbae70bd416a61b0c899485bde3a702b/packages/babel-jest/src/index.js#L72

Using that option produces the following output (reusing your example repo):

$ yarn --silent babel --retain-lines index.spec.js
warning package.json: No license field
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {var desc = {};Object['ke' + 'ys'](descriptor).forEach(function (key) {desc[key] = descriptor[key];});desc.enumerable = !!desc.enumerable;desc.configurable = !!desc.configurable;if ('value' in desc || desc.initializer) {desc.writable = true;}desc = decorators.slice().reverse().reduce(function (desc, decorator) {return decorator(target, property, desc) || desc;}, desc);if (context && desc.initializer !== void 0) {desc.value = desc.initializer ? desc.initializer.call(context) : void 0;desc.initializer = undefined;}if (desc.initializer === void 0) {Object['define' + 'Property'](target, property, desc);desc = null;}return desc;}it('supports decorators on async methods', () => {var _class;
  const foo = () => {};let

  C = (_class = class C {
    async
    bar() {}}, (_applyDecoratedDescriptor(_class.prototype, "bar", [foo], Object.getOwnPropertyDescriptor(_class.prototype, "bar"), _class.prototype)), _class);

});
async
bar() {}

is not valid JS.

$ yarn --silent babel --retain-lines index.spec.js | node
warning package.json: No license field
[stdin]:6
    bar() {}}, (_applyDecoratedDescriptor(_class.prototype, "bar", [foo], Object.getOwnPropertyDescriptor(_class.prototype, "bar"), _class.prototype)), _class);
    ^^^

SyntaxError: Unexpected identifier
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Object.<anonymous> ([stdin]-wrapper:6:22)
    at Module._compile (module.js:643:30)
    at evalScript (bootstrap_node.js:462:27)
    at Socket.<anonymous> (bootstrap_node.js:233:15)
    at emitNone (events.js:111:20)
    at Socket.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1055:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)

For comparison, without --retain-lines:

function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object['ke' + 'ys'](descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object['define' + 'Property'](target, property, desc); desc = null; } return desc; }

it('supports decorators on async methods', () => {
  var _class;

  const foo = () => {};

  let C = (_class = class C {
    async bar() {}

  }, (_applyDecoratedDescriptor(_class.prototype, "bar", [foo], Object.getOwnPropertyDescriptor(_class.prototype, "bar"), _class.prototype)), _class);
});

@julien-f Can you open up an issue with Babel? This is a bug with either retainLines or the transform.

@cpojer thoughts on dropping the retainLines on our side? We ask babel to give us sourcemaps, so it doesn't really provide much value in babel-jest, does it?

@modosc Not related to jest-runtime version, as we support babel 7 in babel-jest. And using the extra transform works as the async keyword causing the syntax error is compiled away.

@modosc
Copy link

modosc commented Feb 12, 2018

the fix was reverted here and as of babel-jest@22.2.2 the problem is back, can you reopen this please?

@SimenB SimenB reopened this Feb 12, 2018
@SimenB
Copy link
Member

SimenB commented Feb 12, 2018

After merging #5177, we should be OK to remove it again.

@SimenB
Copy link
Member

SimenB commented Feb 17, 2018

See #5594

@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 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants