This repository was archived by the owner on Jan 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathknexfile-generator.test.ts
More file actions
95 lines (84 loc) · 3.49 KB
/
knexfile-generator.test.ts
File metadata and controls
95 lines (84 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* eslint-disable function-paren-newline */
// @ts-ignore
const generateKnexConfig = require('../database/generate-knexfile');
const {
getNodeEnvSpecificDefaults,
getConfigEntry,
} = require('../database/generate-knexfile');
const {
// @ts-ignore
NODE_ENV,
KNEX_DEBUG,
DB_CLIENT_TEST,
DB_HOST_TEST,
DB_USER_TEST,
DB_NAME_TEST,
DB_PASS_TEST,
DB_CLIENT,
DB_HOST,
DB_USER,
DB_NAME,
DB_PASS,
} = process.env;
const KNEX_DEBUG_BOOL = KNEX_DEBUG === 'true';
const testNodeBlogConfig = {
client: 'testclient',
host: 'testhost',
database: 'testhost',
user: 'testuser',
password: 'testpass',
debug: true,
};
describe('Knexfile generator', () => {
const defaultKnexConfig = generateKnexConfig();
const testKnexConfig = generateKnexConfig(testNodeBlogConfig);
test('Jest should run under "test" node_env (knexfile generator)', () => {
expect(NODE_ENV).toBe('test');
});
test('If no config is given, fall back on env variables', () => {
expect(defaultKnexConfig.client).toBe(DB_CLIENT_TEST);
expect(defaultKnexConfig.connection.host).toBe(DB_HOST_TEST);
expect(defaultKnexConfig.connection.database).toBe(DB_NAME_TEST);
expect(defaultKnexConfig.connection.user).toBe(DB_USER_TEST);
expect(defaultKnexConfig.connection.password).toBe(DB_PASS_TEST);
expect(defaultKnexConfig.debug).toBe(KNEX_DEBUG_BOOL);
expect(defaultKnexConfig.asyncStackTraces).toBe(KNEX_DEBUG_BOOL);
});
test('Debug should be turned off by default', () => {
expect(defaultKnexConfig.debug).toBe(false);
expect(defaultKnexConfig.asyncStackTraces).toBe(false);
});
test('If a config is given, it should be loaded and returned', () => {
expect(testKnexConfig.client).toBe(testNodeBlogConfig.client);
expect(testKnexConfig.connection.host).toBe(testNodeBlogConfig.host);
expect(testKnexConfig.connection.database).toBe(
testNodeBlogConfig.database,
);
expect(testKnexConfig.connection.user).toBe(testNodeBlogConfig.user);
expect(testKnexConfig.connection.password).toBe(
testNodeBlogConfig.password,
);
expect(testKnexConfig.debug).toBe(testNodeBlogConfig.debug);
expect(testKnexConfig.asyncStackTraces).toBe(testNodeBlogConfig.debug);
});
test('Knexfile defaults should be set based on NODE_ENV', () => {
const developmentDefaults = getNodeEnvSpecificDefaults('development');
const testDefaults = getNodeEnvSpecificDefaults('test');
const productionDefaults = getNodeEnvSpecificDefaults('production');
expect(developmentDefaults).not.toEqual(testDefaults);
expect(developmentDefaults).toEqual(productionDefaults);
expect(developmentDefaults.client).toBe(DB_CLIENT);
expect(testDefaults.client).toBe(DB_CLIENT_TEST);
expect(developmentDefaults.host).toBe(DB_HOST);
expect(developmentDefaults.database).toBe(DB_NAME);
expect(developmentDefaults.user).toBe(DB_USER);
expect(developmentDefaults.password).toBe(DB_PASS);
});
test('Config entries should return value, otherwise default', () => {
const testValue = 'givenvalue';
const configWithValueGiven = getConfigEntry('client', testValue);
const configWithoutValueGiven = getConfigEntry('client');
expect(configWithValueGiven).toBe(testValue);
expect(configWithoutValueGiven).toBe(process.env.DB_CLIENT_TEST);
});
});