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

Named export is not defined when used in destructuring object #1791

Closed
6 tasks done
inakiabt opened this issue Aug 4, 2022 · 7 comments
Closed
6 tasks done

Named export is not defined when used in destructuring object #1791

inakiabt opened this issue Aug 4, 2022 · 7 comments
Labels

Comments

@inakiabt
Copy link

inakiabt commented Aug 4, 2022

Describe the bug

Looks like imported named exports are not being correctly transformed when used in object desctructuring.

// types.js
export const NAME = 'test';
// helper.js
import { NAME } from './types';

console.log('NAME', NAME);
export const run = ({ [NAME]: result }) => result;

When running I get this error:

 FAIL  test/helper1.spec.js > should 
ReferenceError: NAME is not defined
 ❯ Module.run test/helper1.js:4:31
      2| 
      3| console.log('NAME', NAME);
      4| export const run = ({ [NAME]: result }) => result;
       |                               ^
      5| 

This is the transform result of my helper.js file:
image

As can be seen, NAME isn't not correctly referenced.

Originally posted by @inakiabt in #1789

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-eg6hlk?file=test%2Fhelper1.js

There are 3 tests. All of them tests a simple function that uses an imported constant to destruct an object and return the result.

for test 1 and 2, the constant (NAME) seems to be "not defined" to vitest.
for test 3, I use a different the constant (n) that seems to be defined but it doesn't have the correct value when running the test.

System Info

System:
    OS: macOS 12.5
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 795.55 MB / 16.00 GB
    Shell: 5.1.16 - /usr/local/bin/bash
  Binaries:
    Node: 16.10.0 - ~/.nvm/versions/node/v16.10.0/bin/node
    npm: 7.24.0 - ~/.nvm/versions/node/v16.10.0/bin/npm
    Watchman: 2022.07.04.00 - /usr/local/bin/watchman
  Browsers:
    Chrome: 103.0.5060.134
    Firefox: 102.0.1
    Safari: 15.6
  npmPackages:
    vite: ^2.9.14 => 2.9.14 
    vitest: ^0.20.3 => 0.20.3

Used Package Manager

npm

Validations

@sheremet-va
Copy link
Member

sheremet-va commented Aug 4, 2022

Please, open an issue in Vite repository, VItest doesn't do any transformations on its own.

@inakiabt
Copy link
Author

inakiabt commented Aug 4, 2022

🤦

@inakiabt inakiabt closed this as completed Aug 4, 2022
@inakiabt
Copy link
Author

inakiabt commented Aug 4, 2022

@sheremet-va While I was opening the issue in vite repository, I realized that running it with vite works.
So I think it could be a vitest issue, couldn't be?

Here's an example:
https://stackblitz.com/edit/vitejs-vite-euhfit?file=main.js

@inakiabt inakiabt reopened this Aug 5, 2022
@sheremet-va
Copy link
Member

sheremet-va commented Aug 5, 2022

No, this is error with Vite SSR. Vite doesn't do this transformation for browser.

@sheremet-va sheremet-va closed this as not planned Won't fix, can't repro, duplicate, stale Aug 5, 2022
@inakiabt
Copy link
Author

inakiabt commented Aug 8, 2022

@sheremet-va I see. How can I reproduce it using just vite? I want to create an example reproducing the error to create the issue

@sheremet-va
Copy link
Member

@sheremet-va I see. How can I reproduce it using just vite? I want to create an example reproducing the error to create the issue

You can give the same reproduction. Vitest internally uses Vite's server.ssrTransform.

@sheremet-va
Copy link
Member

Here is the more generalized reproduction tho: https://stackblitz.com/edit/node-pmokln?file=index.mjs

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

No branches or pull requests

2 participants