1
1
import * as childProcess from 'child_process' ;
2
+ import * as fs from 'fs' ;
3
+ import * as path from 'path' ;
2
4
3
5
type NodeVersion = '14' | '16' | '18' | '20' | '21' ;
4
6
5
7
interface VersionConfig {
6
8
ignoredPackages : Array < `@${'sentry' | 'sentry-internal' } /${string } `> ;
7
9
}
8
10
11
+ const UNIT_TEST_ENV = process . env . UNIT_TEST_ENV as 'node' | 'browser' | undefined ;
12
+
9
13
const CURRENT_NODE_VERSION = process . version . replace ( 'v' , '' ) . split ( '.' ) [ 0 ] as NodeVersion ;
10
14
11
15
const RUN_AFFECTED = process . argv . includes ( '--affected' ) ;
12
16
13
- const DEFAULT_SKIP_TESTS_PACKAGES = [
14
- '@sentry-internal/eslint-plugin-sdk' ,
17
+ // These packages are tested separately in CI, so no need to run them here
18
+ const DEFAULT_SKIP_PACKAGES = [ '@sentry/profiling-node' , '@sentry/bun' , '@sentry/deno' ] ;
19
+
20
+ // All other packages are run for multiple node versions
21
+ const BROWSER_TEST_PACKAGES = [
15
22
'@sentry/ember' ,
16
23
'@sentry/browser' ,
17
24
'@sentry/vue' ,
@@ -26,10 +33,9 @@ const DEFAULT_SKIP_TESTS_PACKAGES = [
26
33
'@sentry-internal/replay-worker' ,
27
34
'@sentry-internal/feedback' ,
28
35
'@sentry/wasm' ,
29
- '@sentry/bun' ,
30
- '@sentry/deno' ,
31
36
] ;
32
37
38
+ // These are Node-version specific tests that need to be skipped because of support
33
39
const SKIP_TEST_PACKAGES : Record < NodeVersion , VersionConfig > = {
34
40
'14' : {
35
41
ignoredPackages : [
@@ -40,6 +46,7 @@ const SKIP_TEST_PACKAGES: Record<NodeVersion, VersionConfig> = {
40
46
'@sentry/astro' ,
41
47
'@sentry/nuxt' ,
42
48
'@sentry/nestjs' ,
49
+ '@sentry-internal/eslint-plugin-sdk' ,
43
50
] ,
44
51
} ,
45
52
'16' : {
@@ -56,6 +63,50 @@ const SKIP_TEST_PACKAGES: Record<NodeVersion, VersionConfig> = {
56
63
} ,
57
64
} ;
58
65
66
+ function getAllPackages ( ) : string [ ] {
67
+ const { workspaces } : { workspaces : string [ ] } = JSON . parse (
68
+ fs . readFileSync ( path . join ( process . cwd ( ) , 'package.json' ) , 'utf-8' ) ,
69
+ ) ;
70
+
71
+ return workspaces . map ( workspacePath => {
72
+ const { name } : { name : string } = JSON . parse (
73
+ fs . readFileSync ( path . join ( process . cwd ( ) , workspacePath , 'package.json' ) , 'utf-8' ) ,
74
+ ) ;
75
+ return name ;
76
+ } ) ;
77
+ }
78
+
79
+ /**
80
+ * Run the tests, accounting for compatibility problems in older versions of Node.
81
+ */
82
+ function runTests ( ) : void {
83
+ const ignores = new Set < string > ( DEFAULT_SKIP_PACKAGES ) ;
84
+
85
+ const packages = getAllPackages ( ) ;
86
+
87
+ if ( UNIT_TEST_ENV === 'browser' ) {
88
+ // Since we cannot "include" for affected mode, we instead exclude all other packages
89
+ packages . forEach ( pkg => {
90
+ if ( ! BROWSER_TEST_PACKAGES . includes ( pkg ) ) {
91
+ ignores . add ( pkg ) ;
92
+ }
93
+ } ) ;
94
+ } else if ( UNIT_TEST_ENV === 'node' ) {
95
+ BROWSER_TEST_PACKAGES . forEach ( pkg => ignores . add ( pkg ) ) ;
96
+ }
97
+
98
+ const versionConfig = SKIP_TEST_PACKAGES [ CURRENT_NODE_VERSION ] ;
99
+ if ( versionConfig ) {
100
+ versionConfig . ignoredPackages . forEach ( dep => ignores . add ( dep ) ) ;
101
+ }
102
+
103
+ if ( RUN_AFFECTED ) {
104
+ runAffectedTests ( ignores ) ;
105
+ } else {
106
+ runAllTests ( ignores ) ;
107
+ }
108
+ }
109
+
59
110
/**
60
111
* Run the given shell command, piping the shell process's `stdin`, `stdout`, and `stderr` to that of the current
61
112
* process. Returns contents of `stdout`.
@@ -67,41 +118,28 @@ function run(cmd: string, options?: childProcess.ExecSyncOptions): void {
67
118
/**
68
119
* Run tests, ignoring the given packages
69
120
*/
70
- function runWithIgnores ( skipPackages : string [ ] = [ ] ) : void {
71
- const ignoreFlags = skipPackages . map ( dep => `--ignore="${ dep } "` ) . join ( ' ' ) ;
121
+ function runAllTests ( ignorePackages : Set < string > ) : void {
122
+ const ignoreFlags = Array . from ( ignorePackages )
123
+ . map ( dep => `--ignore="${ dep } "` )
124
+ . join ( ' ' ) ;
125
+
72
126
run ( `yarn test ${ ignoreFlags } ` ) ;
73
127
}
74
128
75
129
/**
76
130
* Run affected tests, ignoring the given packages
77
131
*/
78
- function runAffectedWithIgnores ( skipPackages : string [ ] = [ ] ) : void {
132
+ function runAffectedTests ( ignorePackages : Set < string > ) : void {
79
133
const additionalArgs = process . argv
80
134
. slice ( 2 )
81
135
. filter ( arg => arg !== '--affected' )
82
136
. join ( ' ' ) ;
83
- const ignoreFlags = skipPackages . map ( dep => `--exclude="${ dep } "` ) . join ( ' ' ) ;
84
- run ( `yarn test:pr ${ ignoreFlags } ${ additionalArgs } ` ) ;
85
- }
86
-
87
- /**
88
- * Run the tests, accounting for compatibility problems in older versions of Node.
89
- */
90
- function runTests ( ) : void {
91
- const ignores = new Set < string > ( ) ;
92
-
93
- DEFAULT_SKIP_TESTS_PACKAGES . forEach ( pkg => ignores . add ( pkg ) ) ;
94
137
95
- const versionConfig = SKIP_TEST_PACKAGES [ CURRENT_NODE_VERSION ] ;
96
- if ( versionConfig ) {
97
- versionConfig . ignoredPackages . forEach ( dep => ignores . add ( dep ) ) ;
98
- }
138
+ const excludeFlags = Array . from ( ignorePackages )
139
+ . map ( dep => `--exclude="${ dep } "` )
140
+ . join ( ' ' ) ;
99
141
100
- if ( RUN_AFFECTED ) {
101
- runAffectedWithIgnores ( Array . from ( ignores ) ) ;
102
- } else {
103
- runWithIgnores ( Array . from ( ignores ) ) ;
104
- }
142
+ run ( `yarn test:pr ${ excludeFlags } ${ additionalArgs } ` ) ;
105
143
}
106
144
107
145
runTests ( ) ;
0 commit comments