-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathvitest.browser.config.mts
More file actions
177 lines (165 loc) · 5.45 KB
/
vitest.browser.config.mts
File metadata and controls
177 lines (165 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* Copyright 2025-2026, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/// <reference types="@vitest/browser/providers/webdriverio" />
import path from 'path';
import { defineConfig } from 'vitest/config'
import { requestLoggerPlugin } from './vitest/request-logger-plugin';
import { consoleCapturePlugin } from './vitest/console-capture-plugin';
// Check if we should use local browser instead of BrowserStack
const useLocalBrowser = process.env.USE_LOCAL_BROWSER === 'true';
// Get browser configuration from TEST_* environment variables
const testBrowser = process.env.TEST_BROWSER || 'chrome';
const testBrowserVersion = process.env.TEST_BROWSER_VERSION;
const testOsName = process.env.TEST_OS_NAME;
const testOsVersion = process.env.TEST_OS_VERSION;
// const browserConfig = {
// name: testBrowser,
// browserName: testBrowser,
// browserVersion: testBrowserVersion,
// os: testOsName,
// osVersion: testOsVersion,
// };
// const browserConfigs = [browserConfig];
// Build local browser capabilities
function buildLocalCapabilities() {
return {
browserName: testBrowser,
'goog:chromeOptions': {
args: [
'--disable-blink-features=AutomationControlled',
'--disable-dev-shm-usage',
'--no-sandbox',
],
},
};
}
// Build BrowserStack capabilities
function buildBrowserStackCapabilities() {
const localIdentifier = process.env.BROWSERSTACK_LOCAL_IDENTIFIER;
return {
browserName: testBrowser,
'wdio:enforceWebDriverClassic': true, // this doesn't work due to vitest bug, still keeping here for future reference
'goog:chromeOptions': {
args: [
'--disable-blink-features=AutomationControlled',
'--disable-dev-shm-usage',
'--no-sandbox',
],
},
'bstack:options': {
os: testOsName,
osVersion: testOsVersion,
browserVersion: testBrowserVersion,
buildName: process.env.VITEST_BUILD_NAME || 'Vitest Browser Tests',
projectName: 'Optimizely JavaScript SDK',
sessionName: `${testBrowser} ${testBrowserVersion || ''} on ${testOsName} ${testOsVersion}`,
local: true,
// Include localIdentifier for parallel tunnel support
...(localIdentifier && { localIdentifier }),
debug: false,
networkLogs: false,
consoleLogs: 'errors' as const,
seleniumLogs: false,
idleTimeout: 900, // 15 minutes idle timeout - prevents premature session closure during long test runs
},
};
}
function buildBrowserInstances() {
if (useLocalBrowser) {
// Local browser configurations - all browsers
return [{
browser: testBrowser,
capabilities: buildLocalCapabilities(),
}];
} else {
const username = process.env.BROWSERSTACK_USERNAME || process.env.BROWSER_STACK_USERNAME;
const key = process.env.BROWSERSTACK_ACCESS_KEY || process.env.BROWSER_STACK_ACCESS_KEY;
return [{
browser: testBrowser,
user: username,
key: key,
capabilities: buildBrowserStackCapabilities(),
connectionRetryTimeout: 120000, // 2 minutes connection retry timeout
connectionRetryCount: 3, // Retry 3 times on connection failure
waitforTimeout: 30000, // 30 seconds wait timeout - matches test expectations
waitforInterval: 1000, // Poll every 1 second - faster feedback
keepAlive: true,
keepAliveInterval: 30000,
}];
}
}
export default defineConfig({
plugins: [
...(process.env.VITEST_REQUEST_LOGGER === 'true' ? [requestLoggerPlugin()] : []),
...(process.env.VITEST_CONSOLE_CAPTURE === 'true' ? [consoleCapturePlugin()] : []),
],
resolve: {
alias: {
'error_message': path.resolve(__dirname, './lib/message/error_message'),
'log_message': path.resolve(__dirname, './lib/message/log_message'),
},
},
esbuild: {
target: 'es2015',
format: 'esm',
},
build: {
target: 'es2015',
},
optimizeDeps: {
// Force chai to be pre-bundled with ES6 target to remove class static blocks
// This avoids issues with browsers that do not support class static blocks like firefox 91
include: ['chai'],
esbuildOptions: {
target: 'es2015',
},
},
// Serve public files from vitest/public directory
publicDir: 'vitest/public',
server: {
host: '0.0.0.0',
// for safari, browserstack redirects localhost to bs-local.com
allowedHosts: ['bs-local.com', 'localhost'],
},
test: {
isolate: false,
fileParallelism: true,
maxConcurrency: 5,
onConsoleLog: () => true,
browser: {
enabled: true,
provider: 'webdriverio',
headless: false,
instances: buildBrowserInstances(),
connectTimeout: 300000,
},
testTimeout: 60000,
hookTimeout: 30000,
include: [
'lib/**/*.spec.ts',
],
exclude: [
'lib/**/*.react_native.spec.ts',
'lib/**/*.node.spec.ts',
'lib/*.umd.spec.ts'
],
typecheck: {
enabled: true,
tsconfig: 'tsconfig.spec.json',
exclude: ['.build/**', 'dist/**', 'node_modules/**'],
},
},
});