-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.ts
75 lines (65 loc) · 2.77 KB
/
index.ts
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
import injectDevServer from '@cypress/react/plugins/babel';
import injectCodeCoverage from '@cypress/code-coverage/task';
import path from 'path';
import { RuleSetRule } from 'webpack';
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
export default (
on: Cypress.PluginEvents,
config: Cypress.PluginConfigOptions
): Cypress.ResolvedConfigOptions => {
if (config.testingType === 'component') {
injectDevServer(on, config, {
setWebpackConfig: (webpackConfig) => {
const isProductionSmokeTest = process.env.BABEL_ENV === 'production';
// If this is a production smoke test, we should run our tests against
// the /dist directory; otherwise we'll use /src.
// (NOTE that a production build will need to have been performed before
// running smoke tests to ensure /dist exists and has up-to-date contents)
const componentDirectoryName = isProductionSmokeTest ? 'dist' : 'src';
// We'll alias `react-hover-video-player` imports in the test files
// to whatever component directory we're using
webpackConfig.resolve.alias = {
'react-hover-video-player': path.resolve(
__dirname,
`../../../${componentDirectoryName}`
),
};
// Inject babel presets and plugins so we can transpile the component for cypress tests
// without needing to create an entire babelrc file that's only for tests
webpackConfig.module.rules.forEach((webpackRule: RuleSetRule) => {
if (webpackRule?.loader === 'babel-loader') {
webpackRule.options = {
// Standard babel presets for a react + typescript project
presets: [
'@babel/preset-env',
'@babel/preset-typescript',
'@babel/preset-react',
],
// Use istanbul plugin to instrument the component code for checking code coverage
plugins: ['istanbul'],
};
}
});
return webpackConfig;
},
});
}
injectCodeCoverage(on, config);
// Override the args passed to the browser to ensure we
// can test against its autoplay policy
on('before:browser:launch', (browser, launchOptions) => {
launchOptions.args = launchOptions.args.filter(
(arg) => !arg.startsWith('--autoplay-policy')
);
const autoplayPolicyOptionIndex = launchOptions.args.indexOf(
'--autoplay-policy=no-user-gesture-required'
);
if (autoplayPolicyOptionIndex >= 0) {
launchOptions.args[autoplayPolicyOptionIndex] =
'--disable-features=PreloadMediaEngagementData, MediaEngagementBypassAutoplayPolicies';
}
return launchOptions;
});
return config; // IMPORTANT to return a config
};