Skip to content

Instantly share code, notes, and snippets.

@MuriloEduardo
Last active May 20, 2021 13:06
Show Gist options
  • Save MuriloEduardo/1eeecf457bc5cc0cc5c52b2c4c2125be to your computer and use it in GitHub Desktop.
Save MuriloEduardo/1eeecf457bc5cc0cc5c52b2c4c2125be to your computer and use it in GitHub Desktop.
Are you trying to set up Jest to unitarily test your Vue 3 files in a Laravel project?
module.exports = {
roots: [
'<rootDir>/resources/js/',
],
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/resources/js/$1',
},
moduleFileExtensions: ['js', 'json', 'vue'],
transform: {
'^.+\\.vue$': 'vue-jest',
'^.+\\js$': 'babel-jest'
},
};

I found some errors when trying to run the tests together with the "@" alias configured in the webpack.config.js file

This was the error printed on the console:

FAIL  resources/js/Pages/Posts/tests/Index.test.js
  ● Test suite failed to run

    Cannot find module '@/Layouts/AppLayout' from 'resources/js/Pages/Posts/Index.vue'

    Require stack:
      resources/js/Pages/Posts/Index.vue
      resources/js/Pages/Posts/tests/Index.test.js

      26 | </template>
      27 | <script>
    > 28 | import AppLayout from '@/Layouts/AppLayout';

After about 6 hours of research I managed to solve this configuration of Jest that will be in the file just above.

I hope this helps someone!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment