Skip to content

Commit e20d811

Browse files
Add environment variable checks and refactor live preview tests
1 parent 2e70392 commit e20d811

File tree

2 files changed

+29
-37
lines changed

2 files changed

+29
-37
lines changed

test/config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
'use strict';
22
require('dotenv').config()
33

4+
const requiredVars = ['API_KEY', 'DELIVERY_TOKEN', 'ENVIRONMENT', 'HOST'];
5+
const missingVars = requiredVars.filter((key) => !process.env[key]);
6+
7+
if (missingVars.length > 0) {
8+
const errorMessage = `\x1b[31mError: Missing environment variables - ${missingVars.join(', ')}`;
9+
10+
if (process.env.NODE_ENV === 'test' || process.env.JEST_WORKER_ID !== undefined) {
11+
throw new Error(errorMessage);
12+
} else {
13+
console.error(errorMessage);
14+
process.exit(1);
15+
}
16+
}
17+
418
module.exports = {
519
stack: { 'api_key': process.env.API_KEY, 'delivery_token': process.env.DELIVERY_TOKEN, 'environment': process.env.ENVIRONMENT, },
620
host: process.env.HOST,

test/live-preview/live-preview-test.js

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
'use strict';
2+
const init = require("../config.js");
23

34
const Contentstack = require('../../dist/node/contentstack.js');
45

56
describe('Contentstack Live Preview Tests', () => {
67
test('should check for values initialized', () => {
8+
const stack1 = Contentstack.Stack(init.stack)
79
const stack = Contentstack.Stack({
8-
'api_key': process.env.region_API_KEY,
10+
'api_key': process.env.API_KEY,
911
'delivery_token': process.env.DELIVERY_TOKEN,
1012
'environment': process.env.ENVIRONMENT
1113
});
@@ -16,15 +18,9 @@ describe('Contentstack Live Preview Tests', () => {
1618
});
1719

1820
test('should check host when live preview is enabled and management token is provided', () => {
19-
const stack = Contentstack.Stack({
20-
'api_key': process.env.API_KEY,
21-
'delivery_token': process.env.DELIVERY_TOKEN,
22-
'environment': process.env.ENVIRONMENT,
23-
live_preview: {
24-
enable: true,
25-
management_token: 'management_token'
26-
}
27-
});
21+
init.stack.live_preview.enable = true;
22+
init.stack.live_preview.management_token = 'management_token';
23+
const stack = Contentstack.Stack(init.stack);
2824

2925
const livePreviewObject = stack.config.live_preview;
3026
expect(livePreviewObject).not.toBe('undefined');
@@ -34,15 +30,9 @@ describe('Contentstack Live Preview Tests', () => {
3430
});
3531

3632
test('should check host when live preview is disabled and management token is provided', () => {
37-
const stack = Contentstack.Stack({
38-
'api_key': process.env.API_KEY,
39-
'delivery_token': process.env.DELIVERY_TOKEN,
40-
'environment': process.env.ENVIRONMENT,
41-
live_preview: {
42-
enable: false,
43-
management_token: 'management_token'
44-
}
45-
});
33+
init.stack.live_preview.enable = false;
34+
init.stack.live_preview.management_token = 'management_token';
35+
const stack = Contentstack.Stack(init.stack);
4636

4737
const livePreviewObject = stack.config.live_preview;
4838
expect(livePreviewObject).not.toBe('undefined');
@@ -51,15 +41,9 @@ describe('Contentstack Live Preview Tests', () => {
5141
});
5242

5343
test('should check host when live preview is enabled and preview token is provided', () => {
54-
const stack = Contentstack.Stack({
55-
'api_key': process.env.API_KEY,
56-
'delivery_token': process.env.DELIVERY_TOKEN,
57-
'environment': process.env.ENVIRONMENT,
58-
live_preview: {
59-
enable: true,
60-
preview_token: 'preview_token'
61-
}
62-
});
44+
init.stack.live_preview.enable = true;
45+
init.stack.live_preview.preview_token = 'preview_token';
46+
const stack = Contentstack.Stack(init.stack);
6347

6448
const livePreviewObject = stack.config.live_preview;
6549
expect(livePreviewObject).not.toBe('undefined');
@@ -70,15 +54,9 @@ describe('Contentstack Live Preview Tests', () => {
7054
});
7155

7256
test('should check host when live preview is disabled and preview token is provided', () => {
73-
const stack = Contentstack.Stack({
74-
'api_key': process.env.API_KEY,
75-
'delivery_token': process.env.DELIVERY_TOKEN,
76-
'environment': process.env.ENVIRONMENT,
77-
live_preview: {
78-
enable: false,
79-
preview_token: 'preview_token'
80-
}
81-
});
57+
init.stack.live_preview.enable = false;
58+
init.stack.live_preview.preview_token = 'preview_token';
59+
const stack = Contentstack.Stack(init.stack);
8260

8361
const livePreviewObject = stack.config.live_preview;
8462
expect(livePreviewObject).not.toBe('undefined');

0 commit comments

Comments
 (0)