I would like to do vue util test using jest, but since i used import statement, it keeps making errors.
it says "SyntaxError: Cannot use import statement outside a module "
" at ScriptTransformer._transformAndBuildScript (node_modules/#jest/transform/build/ScriptTransformer.js:537:17)"
i've tried adding type:module to my package.json, transformIgnorePatterns to jest.config.js , and module to my script tag. none of them worked. i think it is related to version, as i am using vue 2 to get track of lessons im listening to. please take a look at my code
this is my package.json
{
"name": "jest-test",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"test:unit": "vue-cli-service test:unit",
"lint": "vue-cli-service lint"
},
"dependencies": {
"axios": "^1.2.3",
"core-js": "^3.6.5",
"vue": "^2.6.11",
"vuex": "^3.4.0"
},
"devDependencies": {
"#babel/preset-env": "^7.20.2",
"#vue/cli-plugin-babel": "^4.1.2",
"#vue/cli-plugin-eslint": "^4.1.2",
"#vue/cli-plugin-unit-jest": "^4.1.2",
"#vue/cli-plugin-vuex": "^4.1.2",
"#vue/cli-service": "^4.1.2",
"#vue/eslint-config-standard": "^5.1.2",
"#vue/test-utils": "^1.0.3",
"babel-eslint": "^10.1.0",
"eslint": "^6.7.2",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.0",
"eslint-plugin-vue": "^6.2.2",
"node-sass": "^4.12.0",
"sass-loader": "^8.0.2",
"vue-template-compiler": "^2.6.11"
}
}
this is my error code to test
<template>
<div>
{{ todo.title }}
</div>
</template>
<script>
import axios from "axios";
export default {
data() {
return {
todo: {},
};
},
created() {
this.fetchTodo();
},
methods: {
async fetchTodo() {
const { data } = await axios.get(
"https://jsonplaceholder.typicode.com/todos/1"
);
this.todo = data;
},
},
};
</script>
and this is testing code
import { shallowMount } from "#vue/test-utils";
import TodoTitle from "#/components/TodoTitle";
describe("TodoTitle Component", () =\> {
test("rendering", () =\> {
const wrapper = shallowMount(TodoTitle);
console.log(wrapper);
expect(wrapper.text()).toBe("");
});
});
Related
I am new to graphql and I have been getting this error when trying to use .graphql file inside vue apollo. while using gql tag , it works fine , but while importing graphql file this error is showing up.
i have tried to use graphql loader but it doesnt seems to be working.
Failed to resolve loader: cache-loader
You may need to install it.
ERROR in ./src/client/pages/Clients.vue?vue&type=script&lang=js&
(./node_modules/babel-loader/lib/index.js??clonedRuleSet-
40.use[0]!./node_modules/#vue/cli-service/lib/config/vue-loader-v15-resolve-
compat/vue-loader.js??vue-loader-options!./src/client/pages/Clients.vue?
vue&type=script&lang=js&) 100:15-55
Module not found: Error: Can't resolve 'cache-loader' in '/home/noakash/temp/gym-
client'
# ./src/client/pages/Clients.vue?vue&type=script&lang=js& 1:0-246 1:262-265 1:267- 510 1:267-510
# ./src/client/pages/Clients.vue 2:0-59 3:0-54 3:0-54 9:2-8
# ./src/router/index.js 5:0-50 31:13-20
# ./src/main.js 3:0-30 18:2-8
webpack compiled with 1 error
my Clients.vue file look like this
apollo: {
clients() {
return {
query:require('../../graphql/clients.graphql'),
result({ loading }) {
this.loading = loading;
},
};
},
},
i am using vue-cli
my vue.config.js look like this
const { defineConfig } = require('#vue/cli-service')
module.exports = defineConfig({
configureWebpack:{
module:{
rules:[
{
test: /\.graphql$/,
use: 'graphql-tag/loader'
}
]
}
},
transpileDependencies: [
'vuetify'
],
})
my package.json
{
"name": "gym-client",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"apollo": "vue-cli-service apollo:dev --generate-schema",
"apollo:schema:generate": "vue-cli-service apollo:schema:generate",
"apollo:schema:publish": "vue-cli-service apollo:schema:publish",
"apollo:start": "vue-cli-service apollo:start"
},
"dependencies": {
"apollo-boost": "^0.4.9",
"apollo-cache-inmemory": "^1.6.6",
"apollo-client": "^2.6.10",
"apollo-link": "^1.2.14",
"apollo-link-http": "^1.5.17",
"core-js": "^3.25.0",
"graphql": "^15.8.0",
"graphql-tag": "^2.12.6",
"jspdf": "^2.5.1",
"lowdb": "^1.0.0",
"mkdirp": "^0.5.1",
"moment": "^2.29.4",
"regenerator-runtime": "^0.13.9",
"shortid": "^2.2.8",
"vue": "^2.6.14",
"vue-apollo": "^3.1.0",
"vue-router": "^3.5.1",
"vuetify": "^2.6.0",
"vuex": "^3.6.2"
},
"devDependencies": {
"#vue/cli-plugin-babel": "~5.0.0",
"#vue/cli-plugin-router": "~5.0.0",
"#vue/cli-plugin-vuex": "~5.0.0",
"#vue/cli-service": "~5.0.0",
"sass": "~1.32.0",
"sass-loader": "^10.0.0",
"vue-cli-plugin-apollo": "~0.22.2",
"vue-cli-plugin-vuetify": "~2.5.5",
"vue-template-compiler": "^2.6.14",
"vuetify-loader": "^1.7.0"
},
"browserslist": [
"> 1%",
"last 2 versions",
"not dead"
]
}
I'm trying to create a MicroFrontend in Vue2 using module federation, like Pinia provides a singleton behaviour about stores that could be awesome for use in module federation, the problem is when I inject this an error is prompt ( I make previous test in a unique application and that works )
That is the error:
And the piece of code relative is:
My configuration is the next:
Child1: Who exposed it's components contains this vue.config.js
const { defineConfig } = require('#vue/cli-service')
const {ModuleFederationPlugin} = require("webpack").container;
const deps = require("./package.json");
module.exports = defineConfig({
transpileDependencies: true,
publicPath: 'auto',
configureWebpack: {
plugins: [
new ModuleFederationPlugin({
name: 'child1',
filename: 'remoteEntry.js',
exposes: {
'./ResultsPage': './src/pages/ResultsPage',
'./Test1Page' : './src/pages/Test1Page',
'./Test2Page' : './src/pages/Test2Page',
'./appStore' : './src/store/appStore.js'
},
shared: require("./package.json").dependencies,
}),
],
}
})
And the package.json:
{
"name": "child2",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"test:unit": "vue-cli-service test:unit",
"lint": "vue-cli-service lint"
},
"dependencies": {
"#vue/composition-api": "^1.7.0",
"core-js": "^3.8.3",
"pinia": "^2.0.17",
"vue": "^2.6.14",
"vue-router": "^4.1.5"
},
"devDependencies": {
"#babel/core": "^7.12.16",
"#babel/eslint-parser": "^7.12.16",
"#vue/cli-plugin-babel": "~5.0.8",
"#vue/cli-plugin-eslint": "~5.0.8",
"#vue/cli-plugin-router": "~5.0.8",
"#vue/cli-plugin-unit-mocha": "~5.0.8",
"#vue/cli-service": "~5.0.8",
"#vue/test-utils": "^1.1.3",
"chai": "^4.2.0",
"eslint": "^7.32.0",
"eslint-plugin-vue": "^8.0.3",
"sass": "^1.32.7",
"sass-loader": "^12.0.0",
"vue-cli-plugin-pinia": "~0.1.4",
"vue-template-compiler": "^2.6.14"
}
}
Parent: This is how works vue.config.js
const { defineConfig } = require("#vue/cli-service");
const { ModuleFederationPlugin } = require("webpack").container;
const ExternalTemplateRemotesPlugin = require("external-remotes-plugin");
const deps = require("./package.json");
module.exports = defineConfig({
transpileDependencies: true,
configureWebpack: {
plugins: [
new ModuleFederationPlugin({
name: "microfrontends-library",
remotes: {
child1: "child1#http://localhost:3013/remoteEntry.js",
//child2: "child2#http://localhost:3014/remoteEntry.js",
},
shared: require('./package.json').dependencies,
})
],
},
});
And its package.json
{
"name": "parent",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"test:unit": "vue-cli-service test:unit",
"lint": "vue-cli-service lint"
},
"dependencies": {
"#vue/composition-api": "^1.7.0",
"core-js": "^3.8.3",
"pinia": "^2.0.17",
"vue": "^2.6.14",
"vue-router": "^4.1.5"
},
"devDependencies": {
"#babel/core": "^7.12.16",
"#babel/eslint-parser": "^7.12.16",
"#vue/cli-plugin-babel": "~5.0.0",
"#vue/cli-plugin-eslint": "~5.0.0",
"#vue/cli-plugin-router": "~5.0.0",
"#vue/cli-plugin-unit-jest": "~5.0.0",
"#vue/cli-service": "~5.0.0",
"#vue/test-utils": "^1.1.3",
"#vue/vue2-jest": "^27.0.0-alpha.2",
"babel-jest": "^27.0.6",
"eslint": "^7.32.0",
"eslint-plugin-vue": "^8.0.3",
"jest": "^27.0.5",
"sass": "^1.32.7",
"sass-loader": "^12.0.0",
"vue-cli-plugin-pinia": "~0.1.4",
"vue-template-compiler": "^2.6.14"
}
}
In resume: I don't have a HTTP server because I'm making a build of the child and them using a HTTP server for support ( In the future I would like to add CDN to this children's )
All help would be accepted, really I'm in a stuck solution, so any movement to a solution would be great
// vue.config.js
module.exports = {
configureWebpack: {
module: {
rules: [
{
test: /\.mjs$/,
include: /node_modules/,
type: "javascript/auto"
}
]
}
}
}
I find this solution in answer
Works for me
Project was setup using Ionic's CLI, nothing custom, except some linter configs.
The problem: every time i try to use IonMenu, it just breaks the whole page.
Page Component:
<template>
<ion-page>
<ion-menu
side="start"
content-id="menuContent"
>
<ion-header>
<ion-toolbar color="secondary">
<ion-title>Menu</ion-title>
</ion-toolbar>
</ion-header>
<ion-content padding>
<ion-label>START MENU CONTENTS</ion-label>
</ion-content>
</ion-menu>
<ion-router-outlet id="menuContent" />
</ion-page>
</template>
<script>
import {
IonPage,
IonRouterOutlet,
IonContent,
IonHeader,
IonMenu,
IonTitle,
IonToolbar
} from '#ionic/vue';
import { defineComponent } from 'vue';
export default defineComponent({
name: 'index-page',
components: {
IonPage,
IonRouterOutlet,
IonContent,
IonHeader,
IonMenu,
IonTitle,
IonToolbar
}
});
</script>
Result (it's identical in browser and in emulator): nested menu content renders, but the page looks like this
If I remove <ion-menu> component, everything works just fine
My package.json
{
"name": "notes",
"version": "0.0.1",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"test:unit": "vue-cli-service test:unit",
"test:e2e": "vue-cli-service test:e2e",
"lint:eslint": "./node_modules/.bin/eslint -c .eslintrc.js .",
"lint:eslint:fix": "npm run lint:eslint -- --fix",
"lint:stylelint": "./node_modules/.bin/stylelint --config stylelint.config.js .",
"lint:stylelint:fix": "npm run lint:stylelint -- --fix"
},
"dependencies": {
"#capacitor/app": "1.1.1",
"#capacitor/core": "3.5.1",
"#capacitor/haptics": "1.1.4",
"#capacitor/keyboard": "1.2.2",
"#capacitor/status-bar": "1.0.8",
"#ionic/vue": "6.0.0",
"#ionic/vue-router": "6.0.0",
"core-js": "3.6.5",
"vue": "3.2.21",
"vue-router": "4.0.12"
},
"devDependencies": {
"#capacitor/cli": "3.5.1",
"#types/jest": "27.0.2",
"#typescript-eslint/eslint-plugin": "5.6.0",
"#typescript-eslint/parser": "5.6.0",
"#vue/cli-plugin-babel": "~5.0.0-rc.1",
"#vue/cli-plugin-e2e-cypress": "~5.0.0-rc.1",
"#vue/cli-plugin-eslint": "~5.0.0-rc.1",
"#vue/cli-plugin-router": "~5.0.0-rc.1",
"#vue/cli-plugin-typescript": "~5.0.0-rc.1",
"#vue/cli-plugin-unit-jest": "~5.0.0-rc.1",
"#vue/cli-service": "~5.0.0-rc.1",
"#vue/eslint-config-typescript": "9.1.0",
"#vue/test-utils": "2.0.0-rc.16",
"#vue/vue3-jest": "27.0.0-alpha.3",
"#vuebits/bem": "1.2.2",
"babel-jest": "27.3.1",
"cypress": "8.7.0",
"eslint": "8.4.1",
"eslint-config-airbnb-base": "15.0.0",
"eslint-plugin-unused-imports": "2.0.0",
"eslint-plugin-vue": "8.2.0",
"jest": "27.3.1",
"npm-run-all": "4.1.5",
"sass": "1.52.2",
"sass-loader": "13.0.0",
"stylelint": "13.13.1",
"stylelint-config-rational-order": "0.1.2",
"stylelint-config-standard": "22.0.0",
"stylelint-order": "4.1.0",
"stylelint-webpack-plugin": "2.1.1",
"ts-jest": "27.0.7",
"typescript": "4.3.5",
"vue-auto-routing": "1.0.1",
"vue-eslint-parser": "9.0.2"
},
"description": "An Ionic project"
}
My vue.config.js
const VueAutoRoutingPlugin = require('vue-auto-routing/lib/webpack-plugin');
const { definePlugin } = require('./config/webpack/define-plugin');
const { linterPlugins } = require('./config/webpack/linter-plugins');
const { getWebpackAliases } = require('./config/webpack/aliases');
module.exports = {
configureWebpack: config => {
config.stats = 'normal';
const routerPlugin = new VueAutoRoutingPlugin({
pages: 'src/pages',
importPrefix: '~/src/pages/'
});
config.plugins = config.plugins.concat([definePlugin, routerPlugin]);
if (process.env.LINT_ON_BUILD === 'true') {
plugins.push(...linterPlugins);
}
config.resolve.alias = { ...config.resolve.alias, ...getWebpackAliases() };
}
};
I create project through vue cli with options: vue3, TS, JEST and add #testing-library/vue . my package.json looks like this
{
"name": "todo-app",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"test:unit": "vue-cli-service test:unit",
"lint": "vue-cli-service lint"
},
"dependencies": {
"vue": "^3.0.0",
"vue-class-component": "^8.0.0-0",
"vue-router": "^4.0.0-0",
"vuex": "^4.0.0-0"
},
"devDependencies": {
"#testing-library/vue": "^5.8.2",
"#types/jest": "^24.0.19",
"#typescript-eslint/eslint-plugin": "^4.18.0",
"#typescript-eslint/parser": "^4.18.0",
"#vue/cli-plugin-eslint": "~4.5.0",
"#vue/cli-plugin-router": "~4.5.0",
"#vue/cli-plugin-typescript": "~4.5.0",
"#vue/cli-plugin-unit-jest": "~4.5.0",
"#vue/cli-plugin-vuex": "~4.5.0",
"#vue/cli-service": "~4.5.0",
"#vue/compiler-sfc": "^3.0.0",
"#vue/eslint-config-standard": "^5.1.2",
"#vue/eslint-config-typescript": "^7.0.0",
"#vue/test-utils": "^2.0.0-0",
"eslint": "^6.7.2",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.0",
"eslint-plugin-vue": "^7.0.0",
"node-sass": "^4.12.0",
"sass-loader": "^8.0.2",
"typescript": "~4.1.5",
"vue-jest": "^5.0.0-0"
}
}
my jest.config.js is the one I am getting from vue CLI
module.exports = {
preset: '#vue/cli-plugin-unit-jest/presets/typescript',
transform: {
'^.+\\.vue$': 'vue-jest'
}
}
my test is
import { fireEvent, render, waitFor } from '#testing-library/vue';
import ContentBlock from '#/components/ContentBlock.vue';
describe('ContentBlock.spec.vue', () => {
const mockedProps = {
title: 'testTitle',
content: 'testTitle',
btn: 'testBtnText'
}
const getMountedInstance = () => {
return render(ContentBlock, {
props: mockedProps
});
};
it('renders block title, content and button', async (): Promise<void> => {
const { getByTestId } = getMountedInstance();
const [title, content, btn] = await waitFor(() => {
return [
getByTestId('blockTitle'),
getByTestId('blockContent'),
getByTestId('blockBtn'),
];
});
expect(title).toBeDefined();
expect(title).toContain(mockedProps.title);
expect(content).toBeDefined();
expect(content).toContain(mockedProps.content);
expect(btn).toBeDefined();
expect(content).toContain(mockedProps.btn);
});
})
tried node versions: 14, 12
I am getting error:
Test suite failed to run
Cannot find module 'vue-template-compiler' from 'vue-test-utils.js'
However, Jest was able to find:
'./render.js'
You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'jsx', 'json', 'vue', 'ts', 'tsx'].
See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string
I checked similar questions such as Test suite failed to run Cannot find module 'vue-template-compiler when using jest in vue3 but it did not help me.
ah, I managed to find an answer in https://github.com/testing-library/vue-testing-library/issues/176
install this version of library #testing-library/vue#next such as
yarn add -D #testing-library/vue#next
or
npm i --save-dev #testing-library/vue#next
i am wrighting test cases for LoginNew.vue component , when i am trying to import that component inside the spec.js file i am getting shallowMount error [check my error here] ,i don't know why i am getting this error please help me to fix this issue
jest.config.js
module.exports = {
moduleNameMapper:{
"~(.*)$": "<rootDir>/resources/js/$1",
},
setupFilesAfterEnv: ['<rootDir>resources/src/tests/setup.js'],
resolver: require.resolve(`jest-pnp-resolver`),
testEnvironment: `node`,
};
LoginNew.vue
<template>
<div>
<h1 id="title">Login</h1>
<input type="text" id="input-username"/>
<input type="password" id="input-password"/>
<button id="btn-sign-in">Sign In</button>
</div>
</template>
<script>
export default {
name: 'LoginNew'
}
</script>
Login.spec.js
import LoginNew from '../../src/Pages/LoginNew.vue';
import{shallowMount} from '#vue/test-utils';
describe('LoginNew.vue',()=>{
describe('When Loaded',()=>{
it('has the required elements',()=>{
const wrapper =shallowMount(LoginNew);
expect(wrapper.find('#input-username').exists()).toBe(false);
expect(wrapper.find('#input-password').exists()).toBe(false);
});
});
});
package.json
{
"name": "vue-fundoo",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve --port 3000",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint",
"test": "jest"
},
"dependencies": {
"#vue/composition-api": "^1.0.0-rc.9",
"#vuelidate/core": "^2.0.0-alpha.18",
"#vuelidate/validators": "^2.0.0-alpha.15",
"axios": "^0.21.1",
"bootstrap": "^5.0.1",
"core-js": "^3.6.5",
"dotenv": "^10.0.0",
"jquery": "^3.6.0",
"vue": "^2.6.11",
"vue-axios": "^3.2.4",
"vue-router": "^3.5.1",
"vue-template-compiler": "^2.6.12",
"vuelidate": "^0.7.6"
},
"devDependencies": {
"#vue/cli-plugin-babel": "~4.5.0",
"#vue/cli-plugin-eslint": "~4.5.0",
"#vue/cli-service": "~4.5.0",
"#vue/test-utils": "^1.2.0",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^10.1.0",
"babel-jest": "^27.0.2",
"eslint": "^6.7.2",
"eslint-plugin-vue": "^6.2.2",
"jest": "^27.0.3",
"node-sass": "^5.0.0",
"sass": "^1.19.0",
"sass-loader": "^10.1.1",
"vue-jest": "^3.0.7",
"vue-test-utils": "^1.0.0-beta.11",
"webpack": "^4.0.0"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"eslint:recommended"
],
"parserOptions": {
"parser": "babel-eslint"
},
"rules": {}
},
"browserslist": [
"> 1%",
"last 2 versions",
"not dead"
],
"jest": {
"transform": {
"^.+\\.vue$": "vue-jest",
"^.+\\.js$": "babel-jest"
}
}
}
Update the jest configuration to
testEnvironment: 'jsdom'
The defaults has been changed for Jest 27
Read more about it here.
https://jestjs.io/blog/2021/05/25/jest-27