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

Bug: ReferenceError: Vue is not defined for v2.0.0 #1525

Closed
pdsuwwz opened this issue May 22, 2022 · 12 comments
Closed

Bug: ReferenceError: Vue is not defined for v2.0.0 #1525

pdsuwwz opened this issue May 22, 2022 · 12 comments
Labels
bug Something isn't working

Comments

@pdsuwwz
Copy link

pdsuwwz commented May 22, 2022

Describe the bug

In the latest version (v2.0.0), the introduction of @vue/test-utils will result in an error.

Minimal reproduction: https://github.com/pdsuwwz/vue-unit/pull/17/files

Related action: https://github.com/pdsuwwz/vue-unit/runs/6535330150?check_suite_focus=true

image

image

image

To Reproduce

Upgrade the version of @vue/test-utils from v2.0.0-rc.21 to v2.0.0
https://www.npmjs.com/package/@vue/test-utils/v/2.0.0

Expected behavior

Unit test passed.

Related information:

  • @vue/test-utils version: v2.0.0
  • Vue version: v3.2.35
  • node version: v14.19.2
  • npm (or yarn) version: v6.14.17

Additional context

@pdsuwwz pdsuwwz added the bug Something isn't working label May 22, 2022
@wxsms
Copy link
Member

wxsms commented May 23, 2022

same issue here

@freakzlike
Copy link
Collaborator

Maybe this solution will help #234 (comment)

@freakzlike
Copy link
Collaborator

Now I faced the same issue in some of my projects which are using jest v28 with jsdom.
The solution from #234 (comment) worked for me

  testEnvironment: 'jsdom',
  testEnvironmentOptions: {
    customExportConditions: [
      'node',
      'node-addons'
    ]
  },

When I manually remove the changes of #1484 from the package.json then it is working without testEnvironmentOptions. I'm not sure what is actually the correct way to fix this.

@pdsuwwz
Copy link
Author

pdsuwwz commented May 23, 2022

  testEnvironment: 'jsdom',
  testEnvironmentOptions: {
    customExportConditions: [
      'node',
      'node-addons'
    ]
  },

@freakzlike Thank you! I tried it and found it works. I think this can be used as a temporary solution :)

@lmiller1990
Copy link
Member

Is this a bug in test utils? Sounds like it's more related to Jest 28 and Vue Jest?

@stephan-roolvink
Copy link

The fix suggested by @pdsuwwz works 👍

@lmiller1990
Copy link
Member

This sounds like it is Jest/jsdom, and nothing something in Test Utils... going to close this, unless there's an obvious way to improve things on this end.

@sheremet-va
Copy link
Contributor

I think the issue might be on our end. Jest by default uses browser field in exports. Do we need it? Or is it just for unpkg?

@lmiller1990
Copy link
Member

lmiller1990 commented Jun 23, 2022

Just normally runs in a Node.js environment (using CommonJS modules) so it should be using the require field. I am not sure why it's using the browser field, that's definitely incorrect, but I do not think this is an issue in Test Utils - we specify for require to use the cjs build:

https://github.com/vuejs/test-utils/blob/main/package.json#L14-L15

I don't think browser is an official one, it's not documented: https://nodejs.org/api/packages.html#subpath-exports. I think it's just a kind of pseudo standard, implemented by various tools. I'm not sure why it's getting consumed in your project, would need more information.

@sheremet-va
Copy link
Contributor

sheremet-va commented Jun 23, 2022

It uses browser field, because jest resolves to it for jsdom (since v28?)

https://jestjs.io/docs/next/configuration#testenvironmentoptions-object

@lmiller1990
Copy link
Member

Oh, I see. I do not think we can do anything about this here, though. Seems #1525 (comment) is the solution for now.

@RaphaelDDL
Copy link

RaphaelDDL commented Sep 18, 2023

A bit late to the party but just wanted to double check something regarding #1525 (comment) "fix", as I'm upgrading a codebase from jest26->29, using test-utils.

I have testEnvironment: 'jsdom', and without customExportConditions or with it set as browser, tests fails with ReferenceError: Vue is not defined. Expected as per the discussion. So, changing to suggested node and node-addons does work and tests run. But then, all window.something fails pointing to " use jsdom"

    The error below may be caused by using the wrong test environment, see https://jestjs.io/docs/configuration#testenvironment-string.
    Consider using the "jsdom" test environment.

    ReferenceError: window is not defined

I have jest@29, jest-environment-jsdom installed, and I have all expected configs (.vue transform to @vue/vue3-jest; .js transform to babel-jest).

What am I missing to make jsdom features (aka browser window and other implementations) work in test-utils tests?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants