Skip to content

Commit 6785e23

Browse files
committed
some minor fixed and refactoring
1 parent 796da95 commit 6785e23

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

lib/setup.js

+27-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
const path = require('path');
22
const fs = require('fs');
3-
const vite = require('./vite.js');
43
const getPort = require('get-port');
5-
4+
const vite = require('./vite.js');
65
const vite_dev_server = {
76
port: process.argv.includes('--test-worker') ? null : 5173
87
};
@@ -13,18 +12,21 @@ try {
1312
//
1413
}
1514

16-
1715
const projectJsConfigFile = path.join(process.cwd(), 'vite.config.js');
1816
const projectTsConfigFile = path.join(process.cwd(), 'vite.config.ts');
1917

20-
const hasProjectJsConfigFile = () => {
18+
const hasProjectConfigFile = () => {
2119
try {
2220
if (fs.statSync(projectJsConfigFile).isFile()) {
2321
return projectJsConfigFile;
2422
}
2523
} catch (err) {
26-
if (fs.statSync(projectTsConfigFile).isFile()) {
27-
return projectTsConfigFile;
24+
try {
25+
if (fs.statSync(projectTsConfigFile).isFile()) {
26+
return projectTsConfigFile;
27+
}
28+
} catch (err) {
29+
// nothing
2830
}
2931

3032
return false;
@@ -35,18 +37,26 @@ module.exports = async function() {
3537
const viteConfig = {
3638
port: vite_dev_server.port
3739
};
38-
40+
3941
if (!viteConfig.port) {
4042
viteConfig.port = await getPort();
4143
}
42-
const projectConfigFile = hasProjectJsConfigFile();
43-
viteConfig.configFile = projectConfigFile ? projectConfigFile : path.join(__dirname, '../vite.config.js');
44-
44+
45+
const projectConfigFile = hasProjectConfigFile();
46+
47+
let viteConfigFile = path.join(__dirname, '../vite.config.js');
48+
if (projectConfigFile) {
49+
viteConfigFile = projectConfigFile;
50+
}
51+
viteConfig.configFile = viteConfigFile;
52+
4553
const viteServer = await vite.start(viteConfig);
46-
const nightwatchPlugin = viteServer.middlewares.stack.find(item => item.route === '/_nightwatch');
54+
const nightwatchPlugin = viteServer.middlewares.stack.find(item => {
55+
return item.route === '/_nightwatch';
56+
});
4757

4858
if (!nightwatchPlugin) {
49-
const error = new Error('Missing vite-plugin-nightwatch in '+ projectConfigFile);
59+
const error = new Error('Missing vite-plugin-nightwatch in ' + projectConfigFile);
5060
const code = `:
5161
5262
import nightwatchPlugin from 'vite-plugin-nightwatch'
@@ -60,12 +70,13 @@ module.exports = async function() {
6070
]
6171
};
6272
`;
63-
error.help = ['Please ensure that "vite-nightwatch-plugin" is loaded in your Vite config file ' + code];
73+
error.help = ['Please ensure that "vite-plugin-nightwatch" is loaded in your Vite config file ' + code];
74+
error.link = 'https://nightwatchjs.org/guide/component-testing/vite-plugin.html';
75+
6476
throw error;
65-
}
77+
}
78+
6679
global.viteServer = viteServer;
6780

6881
return viteServer;
6982
};
70-
71-

nightwatch/globals.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
const http = require('../lib/setup.js');
21
const setup = require('../lib/setup.js');
2+
const http = require('http');
33

44
let viteServer;
5+
56
const isWorker = process.argv.includes('--test-worker');
6-
const startViteServer = async (settings = {}) =>{
7+
const startViteServer = async function (settings = {}) {
78
settings.vite_dev_server = Object.assign({
89
start_vite: true,
910
port: 5173
@@ -13,11 +14,14 @@ const startViteServer = async (settings = {}) =>{
1314
if (settings.vite_dev_server.start_vite) {
1415
viteServer = await setup();
1516

17+
// This will make sure the launch Url is set correctly when mounting the Vue component
1618
settings.vite_dev_server.port = vite_port = viteServer.config.server.port;
1719
} else {
18-
vite_port = settings.vite_dev_server.port;
20+
vite_port = settings.vite_dev_server.port;
21+
1922
try {
2023
const enabled = await makeViteRequest(vite_port);
24+
2125
if (!enabled) {
2226
const error = new Error('Missing vite-plugin-nightwatch');
2327
const code = `:
@@ -34,18 +38,21 @@ const startViteServer = async (settings = {}) =>{
3438
};
3539
`;
3640
error.help = ['Please ensure that "vite-plugin-nightwatch" is loaded in your Vite config file ' + code];
41+
error.link = 'https://nightwatchjs.org/guide/component-testing/vite-plugin.html';
42+
3743
throw error;
3844
}
3945

4046
return true;
4147
} catch (err) {
42-
const error = new Error('vite dev server is not running: \n ' + err.message);
48+
const error = new Error('Vite dev server is not running: \n ' + err.message);
4349
error.help = [`You can configure Nightwatch to start Vite automatically by adding this to your nightwatch.conf.js:
4450
vite_dev_server: {
4551
start_vite: true,
4652
port: 5173
4753
}
4854
`];
55+
error.link = 'https://nightwatchjs.org/guide/component-testing/vite-plugin.html';
4956
throw error;
5057
}
5158
}

0 commit comments

Comments
 (0)