Skip to content

Commit 90be1ad

Browse files
committed
Add tests
1 parent 0082161 commit 90be1ad

File tree

1 file changed

+84
-4
lines changed

1 file changed

+84
-4
lines changed

packages/plugins/synthetics/src/index.test.ts

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,21 @@
22
// This product includes software developed at Datadog (https://www.datadoghq.com/).
33
// Copyright 2019-Present Datadog, Inc.
44

5+
import { runServer } from '@dd/core/helpers/server';
6+
import { API_PREFIX, DEFAULT_PORT } from '@dd/synthetics-plugin/constants';
57
import { getPlugins } from '@dd/synthetics-plugin';
68
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);
820

921
describe('Synthetics Plugin', () => {
1022
describe('getPlugins', () => {
@@ -22,9 +34,77 @@ describe('Synthetics Plugin', () => {
2234
});
2335
});
2436

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+
);
28108
});
29109
});
30110
});

0 commit comments

Comments
 (0)