Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What babel or other settings do I need to run Mocha Test Explorer on a vue-cli-3 project?

I've created a vue3 cli project with Mocha testing:

vue create vue_proj_with_mocha_testing 
(accept defaults)
cd vue_proj_with_mocha_testing
vue add unit-mocha

Then in Visual Code I install the Mocha Test Explorer extension, restart, add the folder to the workspace, click the folder, ctrl-shift-p and Mocha Test Explorer : "Enable for a workspace folder". Out of the box Mocha Test Explorer doesn't seem to like vuecli's example.spec.js test:

import { expect } from 'chai'
import { shallowMount } from '@vue/test-utils'
import HelloWorld from '@/components/HelloWorld.vue'

describe('HelloWorld.vue', () => {
  it('renders props.msg when passed', () => {
    const msg = 'new message'
    const wrapper = shallowMount(HelloWorld, {
      propsData: { msg }
    })
    expect(wrapper.text()).to.include(msg)
  })
})

I add this entry to settings.json so that Test Explorer finds the vue "tests" folder, which is different from the default of "test".

"mochaExplorer.files": ["tests/**/*.spec.js"],

And then receive this error in Test Explorer:

import { expect } from 'chai';
^^^^^^

SyntaxError: Cannot use import statement outside a module

This indicates I have some transpiling work to do, and Mocha Test Explorer indicates the way to do that is the "mochaTestExplorer" fields in settings.json, but I'm not sure what combination of babel packages would be required. What should be done to run this out-of-the-box vue-cli-3 test in Mocha Test Explorer in Visual Studio Code? Here is my current guess:

"mochaExplorer.require": [
    "@babel/preset-env",
    "@babel/register", 
    "@babel/polyfill",
    "@babel/plugin-transform-runtime"
],
like image 423
mpr Avatar asked Nov 07 '22 12:11

mpr


1 Answers

First, add @babel/register in yours devDependencies.

After, add in your Visual Studio Code settings.json:

"mochaExplorer.files": "tests/**/*.spec.js",
"mochaExplorer.env": {
  "NODE_ENV": "test"
},
"mochaExplorer.require": [
  "@babel/register"
]

Finally, changes your babel.config.js to like this:

const presets = [];

if (process.env.NODE_ENV === 'test') presets.push('@babel/preset-env');
else presets.push('@vue/cli-plugin-babel/preset');

module.exports = {
  presets,
};
like image 104
Fernando Franco Avatar answered Nov 16 '22 05:11

Fernando Franco