2
2
// This product includes software developed at Datadog (https://www.datadoghq.com/).
3
3
// Copyright 2019-Present Datadog, Inc.
4
4
5
+ import { runServer } from '@dd/core/helpers/server' ;
6
+ import { API_PREFIX , DEFAULT_PORT } from '@dd/synthetics-plugin/constants' ;
5
7
import { getPlugins } from '@dd/synthetics-plugin' ;
6
8
import { getContextMock } from '@dd/tests/_jest/helpers/mocks' ;
7
- import { runBundlers } from '@dd/tests/_jest/helpers/runBundlers' ;
9
+ import nock from 'nock' ;
10
+
11
+ jest . mock ( '@dd/core/helpers/server' , ( ) => {
12
+ const original = jest . requireActual ( '@dd/core/helpers/server' ) ;
13
+ return {
14
+ ...original ,
15
+ runServer : jest . fn ( original . runServer ) ,
16
+ } ;
17
+ } ) ;
18
+
19
+ const runServerMocked = jest . mocked ( runServer ) ;
8
20
9
21
describe ( 'Synthetics Plugin' , ( ) => {
10
22
describe ( 'getPlugins' , ( ) => {
@@ -22,9 +34,77 @@ describe('Synthetics Plugin', () => {
22
34
} ) ;
23
35
} ) ;
24
36
25
- test ( 'Should run the server at the end of the build.' , async ( ) => {
26
- await runBundlers ( {
27
- logLevel : 'debug' ,
37
+ describe ( 'Server' , ( ) => {
38
+ beforeAll ( ( ) => {
39
+ // Allow local server.
40
+ nock . enableNetConnect ( '127.0.0.1' ) ;
41
+ } ) ;
42
+
43
+ afterEach ( async ( ) => {
44
+ // Kill the server.
45
+ try {
46
+ await fetch ( `http://127.0.0.1:${ DEFAULT_PORT } /${ API_PREFIX } /kill` ) ;
47
+ } catch ( e ) {
48
+ // Do nothing.
49
+ }
50
+ } ) ;
51
+
52
+ afterAll ( ( ) => {
53
+ nock . cleanAll ( ) ;
54
+ nock . disableNetConnect ( ) ;
55
+ } ) ;
56
+
57
+ describe ( 'to run or not to run' , ( ) => {
58
+ afterEach ( ( ) => {
59
+ // Remove the variables we've set.
60
+ delete process . env . BUILD_PLUGINS_S8S_LOCAL ;
61
+ delete process . env . BUILD_PLUGINS_S8S_PORT ;
62
+ } ) ;
63
+ const expectations = [
64
+ {
65
+ description : 'not run with no variables' ,
66
+ env : { } ,
67
+ shouldRun : false ,
68
+ } ,
69
+ {
70
+ description : 'not run with missing port' ,
71
+ env : {
72
+ BUILD_PLUGINS_S8S_LOCAL : '1' ,
73
+ } ,
74
+ shouldRun : false ,
75
+ } ,
76
+ {
77
+ description : 'not run with missing local' ,
78
+ env : {
79
+ BUILD_PLUGINS_S8S_PORT : JSON . stringify ( DEFAULT_PORT ) ,
80
+ } ,
81
+ shouldRun : false ,
82
+ } ,
83
+ {
84
+ description : 'run with both variables' ,
85
+ env : {
86
+ BUILD_PLUGINS_S8S_PORT : JSON . stringify ( DEFAULT_PORT ) ,
87
+ BUILD_PLUGINS_S8S_LOCAL : '1' ,
88
+ } ,
89
+ shouldRun : true ,
90
+ } ,
91
+ ] ;
92
+
93
+ test . each ( expectations ) (
94
+ 'Should $description.' ,
95
+ async ( { description, env, shouldRun } ) => {
96
+ // Set the variables.
97
+ Object . assign ( process . env , env ) ;
98
+ // Run the plugin.
99
+ getPlugins ( { } , getContextMock ( ) ) ;
100
+ // Check the server.
101
+ if ( shouldRun ) {
102
+ expect ( runServerMocked ) . toHaveBeenCalled ( ) ;
103
+ } else {
104
+ expect ( runServerMocked ) . not . toHaveBeenCalled ( ) ;
105
+ }
106
+ } ,
107
+ ) ;
28
108
} ) ;
29
109
} ) ;
30
110
} ) ;
0 commit comments