11import * as childProcess from 'child_process' ;
2+ import * as fs from 'fs' ;
3+ import * as path from 'path' ;
24
35type NodeVersion = '14' | '16' | '18' | '20' | '21' ;
46
57interface VersionConfig {
68 ignoredPackages : Array < `@${'sentry' | 'sentry-internal' } /${string } `> ;
79}
810
11+ const UNIT_TEST_ENV = process . env . UNIT_TEST_ENV as 'node' | 'browser' | undefined ;
12+
913const CURRENT_NODE_VERSION = process . version . replace ( 'v' , '' ) . split ( '.' ) [ 0 ] as NodeVersion ;
1014
1115const RUN_AFFECTED = process . argv . includes ( '--affected' ) ;
1216
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 = [
1522 '@sentry/ember' ,
1623 '@sentry/browser' ,
1724 '@sentry/vue' ,
@@ -26,10 +33,9 @@ const DEFAULT_SKIP_TESTS_PACKAGES = [
2633 '@sentry-internal/replay-worker' ,
2734 '@sentry-internal/feedback' ,
2835 '@sentry/wasm' ,
29- '@sentry/bun' ,
30- '@sentry/deno' ,
3136] ;
3237
38+ // These are Node-version specific tests that need to be skipped because of support
3339const SKIP_TEST_PACKAGES : Record < NodeVersion , VersionConfig > = {
3440 '14' : {
3541 ignoredPackages : [
@@ -40,6 +46,7 @@ const SKIP_TEST_PACKAGES: Record<NodeVersion, VersionConfig> = {
4046 '@sentry/astro' ,
4147 '@sentry/nuxt' ,
4248 '@sentry/nestjs' ,
49+ '@sentry-internal/eslint-plugin-sdk' ,
4350 ] ,
4451 } ,
4552 '16' : {
@@ -56,6 +63,50 @@ const SKIP_TEST_PACKAGES: Record<NodeVersion, VersionConfig> = {
5663 } ,
5764} ;
5865
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+
59110/**
60111 * Run the given shell command, piping the shell process's `stdin`, `stdout`, and `stderr` to that of the current
61112 * process. Returns contents of `stdout`.
@@ -67,41 +118,28 @@ function run(cmd: string, options?: childProcess.ExecSyncOptions): void {
67118/**
68119 * Run tests, ignoring the given packages
69120 */
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+
72126 run ( `yarn test ${ ignoreFlags } ` ) ;
73127}
74128
75129/**
76130 * Run affected tests, ignoring the given packages
77131 */
78- function runAffectedWithIgnores ( skipPackages : string [ ] = [ ] ) : void {
132+ function runAffectedTests ( ignorePackages : Set < string > ) : void {
79133 const additionalArgs = process . argv
80134 . slice ( 2 )
81135 . filter ( arg => arg !== '--affected' )
82136 . 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 ) ) ;
94137
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 ( ' ' ) ;
99141
100- if ( RUN_AFFECTED ) {
101- runAffectedWithIgnores ( Array . from ( ignores ) ) ;
102- } else {
103- runWithIgnores ( Array . from ( ignores ) ) ;
104- }
142+ run ( `yarn test:pr ${ excludeFlags } ${ additionalArgs } ` ) ;
105143}
106144
107145runTests ( ) ;
0 commit comments