question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Named export is not defined when used in destructuring object

See original GitHub issue

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 https://github.com/vitest-dev/vitest/discussions/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

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sheremet-vacommented, Aug 8, 2022

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

1reaction
inakiabtcommented, Aug 4, 2022

🤦

Read more comments on GitHub >

github_iconTop Results From Across the Web

Named export is not defined when used destructuring an object
I've asked in vitest repo in case it was an issue on their side, but seems it is not. Looks like imported named...
Read more >
Destructuring a default export object - Stack Overflow
When this module is imported as export-file , the name default will not be used and the names foo and bar will not...
Read more >
export - JavaScript - MDN Web Docs
This re-exports all named exports from mod as the named exports of the current module, but the default export of mod is not...
Read more >
Named ES6 imports aren't destructured objects - QNTM
It looks like it is, but it isn't! It certainly is not destructuring of the default export of my-module.js . (That default export...
Read more >
Object Destructuring and Integration with Modules in JavaScript
What if I have used a property name that doesn't exist? Well, you'll get a beautiful undefined value, though you can supply a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found