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

How to use hljsVuePlugin in unit test w/ jest + @testing-library/vue ? #34

Open
Scipionh opened this issue Aug 24, 2022 · 3 comments
Open

Comments

@Scipionh
Copy link

I am currently experienced the following error when running my unit tests:

ReferenceError: Vue is not defined

    > 10 | import hljsVuePlugin from '@highlightjs/vue-plugin';
         | ^

Here is my component code:

    <template>
      <div>
        <highlightjs autodetect :code="'hello world'" />
      </div>
    </template>
    
    <script lang="ts">
    import { defineComponent } from 'vue';
    import hljsVuePlugin from '@highlightjs/vue-plugin';
    
    export default defineComponent({
      components: {
        highlightjs: hljsVuePlugin.component,
      },
    });
    </script>

in my test file, I'm simply trying to mount this component:

    const wrapper = mount(FooBlah, {
      global: {
        stubs: {
          highlightjs: {
            template: '<div />',
          },
        },
      },
    });

Here are my libraries versions:

    "vue": "^3.2.33",
    "@highlightjs/vue-plugin": "^2.1.2",
    "highlight.js": "^11.6.0",
    "@vue/cli-plugin-unit-jest": "~5.0.4",
    "@vue/test-utils": "^2.0.2",
    "@vue/vue3-jest": "^27.0.0",

Is there any way I could tell him to simply ignore the highlightjs the component in the jest.config ?

@joshgoebel
Copy link
Member

Perhaps you need to be loading the ESM module rather than the CJS which is going to expect Vue to be declared globally?

@Scipionh
Copy link
Author

I'm not sure what you are suggesting here. I'm using import and in the vue-plugin/package.json I can see:

"exports": {
    ".": {
      "require": "./dist/highlightjs-vue.min.js",
      "import": "./dist/highlightjs-vue.esm.min.js",
      "types": "./dist/vue.d.ts"
    }
  },

So it should use the esm module.

@joshgoebel
Copy link
Member

Yet, the esm source never once references Vue, so your error makes no sense?...

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

2 participants