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

Issue with script and script setup in the same vue file #535

Closed
renansoares opened this issue Mar 2, 2023 · 5 comments
Closed

Issue with script and script setup in the same vue file #535

renansoares opened this issue Mar 2, 2023 · 5 comments

Comments

@renansoares
Copy link
Contributor

renansoares commented Mar 2, 2023

Hi,

I have been having an issue with my tests when testing components that have two script tags in the same vue file.

For example, this component:

<template>
    <h1>{{ message }}</h1>
</template>

<script>
import logger from '@/utils/logger';

const test = 'value';
</script>

<script setup>
const props = defineProps({
    message: { type: String, default: '' }
});
</script>

Is throwing this error:

image

I did some tests debugging the implementation of the output from the process.js file. That's what I got:

  code: '"use strict";\n' +
    '\n' +
    "const test = 'value';\n" +
    '"use strict";\n' +
    '\n' +
    'Object.defineProperty(exports, "__esModule", {\n' +
    '  value: true\n' +
    '});\n' +
    'exports.default = void 0;\n' +
    "const test = 'value';\n" +
    'var _default = {\n' +
    "  __name: 'test-setup',\n" +
    '  props: {\n' +
    '    message: {\n' +
    '      type: String,\n' +
    "      default: ''\n" +
    '    }\n' +
    '  },\n' +
    '\n' +
    '  setup(__props) {\n' +
    '    const props = __props;\n' +
    '    return {\n' +
    '      __sfc: true,\n' +
    '      test,\n' +
    '      props\n' +
    '    };\n' +
    '  }\n' +
    '\n' +
    '};\n' +
    'exports.default = _default;\n' +
    "var __options__ = typeof exports.default === 'function' ? exports.default.options : exports.default\n" +
    'var render = function render() {\n' +
    '  var _vm = this,\n' +
    '    _c = _vm._self._c,\n' +
    '    _setup = _vm._self._setupProxy\n' +
    '  return _c("h1", [_vm._v(_vm._s(_vm.message))])\n' +
    '}\n' +
    'var staticRenderFns = []\n' +
    'render._withStripped = true\n' +
    '\n' +
    '__options__.render = render\n' +
    '__options__.staticRenderFns = staticRenderFns\n',

The const test defined in the script setup was added twice to the final output leading the test to fail because of the error above.

I figure out that changing the implementation of scriptResult and scriptSetupResult in the process.js file to the following, made my tests to run again:

let scriptResult;
  const scriptSetupResult = processScriptSetup(descriptor, filename, config)

  if (!scriptSetupResult) {
    scriptResult = processScript(descriptor.script, filename, config)
  }

From my understanding, when there is a script setup, it is adding all the code of the normal setup twice. Only adding the scriptSetup in case of not having a scriptSetupResult seems to solve the issue.

This issue is blocking the migration to Jest 19 and consequently to test our script setup components.

Could I help on this? I'm happy to create a merge request if my fix sounds a good one.

@lmiller1990
Copy link
Member

Just to clarify this is for Vue 2 (not Vue 3)? That's the PR you've submitted.

@quienti
Copy link
Contributor

quienti commented Apr 21, 2023

Hi!

It is possible to do the same fix for Vue3 ?
We have the same issue

Thanks !

@lmiller1990
Copy link
Member

@quienti sure thing, would you like to make a PR similar to #536?

I don't have much time to work on vue-jest nowadays, but I can help by reviewing, merging and releasing new versions. I hope we can find a maintainer with more bandwidth sometime.

@quienti
Copy link
Contributor

quienti commented Apr 24, 2023

@lmiller1990 I tried to do this => #541

I don't know if it will work :/

@quienti
Copy link
Contributor

quienti commented Apr 27, 2023

This issue can be closed

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

3 participants