Cypress 12 Component Tests Wont Load - vuejs2

I am trying to use Cypress 12 to run compnent tests in a Vue.js 2 app. Below is my cypress.config.ts file:
import { defineConfig } from "cypress";
export default defineConfig({
e2e: {
setupNodeEvents(on, config) {
// implement node event listeners here
},
baseUrl: "http://localhost:9090/.......",
defaultCommandTimeout: 60000,
},
component: {
devServer(cypressConfig: CypressConfiguration) {
// return devServer instance or a promise that resolves to
// a dev server here
return {
port: 9090,
close: () => {},
};
},
},
});
I setup a custom devServer in vue.config.js (otherwise Cypress starts uses its own localhost):
module.exports = {
devServer: {
port: 9090,
proxy: 'http://localhost:8080'
}
}
However, the tests wont load
When I run e2e tests, all is fine: tests appears, calls localhost:9090. However, if I want to run only component tests, it just gets stuck trying to load the tests.
It is not a DevTools problem as I have looked into that. All other configuration settings are standard.

Related

TestCafe config setup for multiple environments

I'm wondering if it's possible to have a single .testcaferc.json file that provides config for all environments?
We've got a lot of config that's shared, so I don't really want to create individual config files for each environment if I don't have to.
Something like this would be great, but can't seem to see anything in the tc docs that mentions anything about environments
{
browsers: "chrome",
concurrency: 5,
baseUrl: "http://localhost:3000",
...
env: {
dev: {
baseUrl: "https://dev.example.com",
...
},
test: {
baseUrl: "https://test.example.com",
...
},
prod: {
baseUrl: "https://example.com",
...
},
}
}
Currently we pass a number of extra args into the scripts as below, which is getting really messy and difficult to maintain in our package file.
{
"test:e2e": "npm run test:cafe",
"test:e2e:dev": "npm run test:cafe -- --base-url=https://dev.example.com",
"test:e2e:test": "npm run test:cafe -- --base-url=https://test.example.com",
"test:e2e:prod": "npm run test:cafe -- --base-url=https://example.com","
}
Thanks!
You can use the JavaScript config file instead of a JSON config file. In this case, you can write your own logic right inside the config. For example, you can check the environment variable as follows:
.testcaferc.js:
module.exports = {
baseUrl: process.env.dev ? 'http://localhost:3000' : 'http://localhost:3005',
customActions: {},
}
Moreover, you can create different configuration files for different environments, and one for common settings. In this case, you can use the common settings from the common config file and import specific environment settings from other config files.
Common: .testcaferc.js
function resolveEnv () {
if (process.env.dev)
return './.testcaferc-dev.js'
else if (process.env.test)
return './.testcaferc-test.js'
else
return './.testcaferc-prod.js'
}
const commonConfig = {
skipJsErrors: true,
customActions: {}
}
const envConfigName = resolveEnv();
const envConfig = require(envConfigName);
module.exports = {
...commonConfig,
...envConfig,
}
.testcaferc-dev.js
module.exports = {
baseUrl: 'http://localhost:3000',
skipJsErrors: false,
}

Socksjs infinite loop with vue.js

I'm using vue.js with a flask server.
The 8080 vue.js dev env forwards axios queries to port 80 , cause my python flask server is always running on port 80, waiting for web services calls.
This is my vue.js vue.config.js file :
module.exports = {
outputDir: "dist",
// relative to outputDir
assetsDir: "static" ,
devServer: {
proxy: 'http://localhost:80'
}
};
everything works except that Im getting sock-js infinite loops, especially when developping on port 8080 :
How can I stop theses queries, please .
I there any way to only forwards AXIOS queries to port80, not the others things ?
https://github.com/webpack/webpack-dev-server/issues/1021
EDIT : Tried this with no luck
vue.config.js :
module.exports = {
outputDir: "dist",
// relative to outputDir
assetsDir: "static",
devServer: {
proxy: {
"^/api": {
target: "http://localhost:80"
}
}
}
};
error :
Error: Request failed with status code 404
EDIT : Hey Guys, finally resolved with this code , simply write this in your vue.config.js at the root of the vue.js app, so the wrong sockjs-node queries will get ignored, only web services will be forwarded :
module.exports = {
outputDir: "dist",
assetsDir: "static",
devServer: {
proxy: {
"/api": {
target: "http://localhost:80"
}
}
}
};
Then, do an axios query from vue.js like this :
const path = '/api/read_linear_solution';
axios.post(path, this.form)
Then, in ur python or node server , the web service must look like this đź‘Ť
#app.route('/api/read_linear_solution', methods=['POST'])

Setup of vue.config.js file to imitate production setup (connect two apps)

I run an R Shiny app on port 3000 which serves my vue.js App like this:
library(shiny)
server <- function(input, output, session) {
histogramData <- reactive({
mtcars
})
observe({
session$sendCustomMessage("histogramData", histogramData())
})
}
ui <- function() {
htmlTemplate("dist/index.html")
}
# Serve the bundle at js/main.js
if (dir.exists("dist/js")) {
addResourcePath("js", "dist/js")
}
# Serve the bundle at js/main.js
if (dir.exists("dist/css")) {
addResourcePath("css", "dist/css")
}
# Serve the bundle at js/main.js
if (dir.exists("dist/img")) {
addResourcePath("img", "dist/img")
}
shinyApp(ui, server)
For development, I would change it like this:
ui <- function() {
htmlTemplate("public/index.html")
}
However, I can not always run the build process just to connect the apps, I want to use the dev server to connect the apps and send data back and forth.
I have setup a vue.config.js with the following configuration to create a connection between the two apps.
const path = require('path')
module.exports = {
publicPath: ".",
devServer: {
port: 4000,
contentBase: path.resolve(__dirname, 'public'),
proxy: {
'/': {
target: 'http://localhost:3000'
},
'/websocket': {
target: 'ws://localhost:3000',
ws: true
}
}
},
transpileDependencies: [
"vuetify"
]
}
This was taken from a github repository, I am acutally quite clueless how to archieve this connection. My idea was to connect go on localhost:4000 and receive the data from localhost:3000, but nothing gets passed:
TypeError: Cannot read property 'addCustomMessageHandler' of undefined at VueComponent.mounted (HelloWorld.vue?140d:42)
This is based on the following method in my vue component (which works perfectly after the build process):
mounted: function () {
window.Shiny.addCustomMessageHandler('histogramData', histogramData =>
this.data.histogramData = histogramData
)
Can anyone tell me what´s wrong and help me to setup the connection correctly?

Storing Enviornment Variables using Service Workers

The problem: I can't have my service worker access my .env variables
The only solution I found was storing the variables into a generated js files, I am using Vue PWA for this.
Vue Config
module.exports = {
pwa: {
name: 'Fintask App',
themeColor: '#3aa9ff',
msTileColor: '#3aa9ff',
appleMobileWebAppCapable: 'yes',
appleMobileWebAppStatusBarStyle: 'black',
// configure the workbox plugin
workboxPluginMode: 'InjectManifest',
workboxOptions: {
// swSrc is required in InjectManifest mode.
swSrc: 'src/service-worker.js',
// ...other Workbox options...
}
}
};
My service worker config
importScripts('swenv.js'); // this generates an error: Uncaught ReferenceError: Cannot access 'process' before initialization
workbox.setConfig({
debug: false,
});
workbox.precaching.precacheAndRoute([]);
workbox.routing.registerRoute(
new RegExp(`${process.env.VUE_APP_API_ROOT_URL}/organization/(.*)`),
workbox.strategies.networkFirst({
cacheName: 'organization',
}),
);
my swEnvBuild that should generate my swenv.js
//swEnvBuild.js - script that is separate from webpack
require('dotenv').config();
const fs = require('fs');
fs.writeFileSync("public/swenv.js",
`const process = {
env: {
VUE_APP_API_ROOT_URL: process.env.VUE_APP_API_ROOT_URL
}
}`);
my swenv.js
const process = {
env: {
VUE_APP_API_ROOT_URL: process.env.VUE_APP_API_ROOT_URL
}
}
This is still not working for me, I did find different solutions based on this method, I just wish there was a better way
My output
swenv.js:3 Uncaught ReferenceError: Cannot access 'process' before initialization
at swenv.js:3
at service-worker.js:3
Error during service worker registration: TypeError: Failed to register a ServiceWorker for scope ('http://localhost/') with script ('http://localhost/service-worker.js'): ServiceWorker script evaluation failed
Don't know why I get this error, this result was generated in my apache server

Vue.js - proxy in vue.config.js is being ignored

I have been searching and reading thru documentation on this topic but I was unbale to make it work.
https://cli.vuejs.org/config/#devserver-proxy
I made my Vue.js application normaly by the commnand
vue create my-app
so I'm running the app by command
npm run serve
on http://localhost:8080/. Pretty standart stuff.
But my app needs a PHP backend which is running at https://localhost/
So I setted up the proxy setting in vue.confic.js file in my root directory.
Content of vue.confic.js file:
module.exports = {
devServer: {
proxy: {
'^/api': {
target: 'https://localhost/',
ws: true,
changeOrigin: true
}
}
}
};
And I'm trying to make axios call for the script on the adress
https://localhost/test.php
The axios call is
mounted() {
this.$axios.get('api/test.php', {})
.then(response => { console.log(response.data) })
.catch(error => { console.log(error) });
},
But for some reason which i cant figure out I'm still getting
GET http://localhost:8080/api/test.php 404 (Not Found)
Thank you in advance. I'll be happy for any tips.
Your axios call is going to make the request to the API with whatever protocol the webpage is on.
The error shows that you’re making an http call but your webpack config is only spoofing https. Are you visiting https from the page making the request?
Eg https://localhost:8080
You can also try updating your webpack proxy server to look like this
module.exports = {
//...
devServer: {
proxy: {
'/api': {
target: 'https://other-server.example.com',
secure: false
}
}
}
};
Additional debug steps: curl your Api endpoint from your terminal to see if it’s a protocol issue
you can try
https: true,
proxy: {
'/api': {
target: 'https://localhost/',
ws: true,
changeOrigin: true
}
}
before try, restart by npm run serve to make it sense