/
config.test.js
66 lines (61 loc) · 1.87 KB
/
config.test.js
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
/* eslint-env jest */
// mock config for testing
jest.mock('../src/config', () => require('./__mocks__/config'));
// npm packages
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const sinon = require('sinon');
const inquirer = require('inquirer');
// our packages
const {handler: config} = require('../src/commands/config');
const configData = {
name: 'test',
domain: 'test.dev',
project: 'test-project',
env: 'ENV=1, OTHER=2',
labels: 'label=1, other=2',
hostname: 'host',
restart: 'no',
template: 'static',
};
const configPath = path.join(process.cwd(), 'exoframe.json');
beforeAll(() => {
try {
fs.statSync(configPath);
fs.unlinkSync(configPath);
} catch (e) {
// no config, just exit
}
});
// test config generation
test('Should generate config file', done => {
// stup inquirer answers
sinon.stub(inquirer, 'prompt').callsFake(() => Promise.resolve(configData));
// spy on console
const consoleSpy = sinon.spy(console, 'log');
// execute login
config().then(() => {
// first check console output
expect(consoleSpy.args).toMatchSnapshot();
// then check config changes
const cfg = yaml.safeLoad(fs.readFileSync(configPath, 'utf8'));
expect(cfg.name).toEqual(configData.name);
expect(cfg.restart).toEqual(configData.restart);
expect(cfg.domain).toEqual(configData.domain);
expect(cfg.project).toEqual(configData.project);
expect(cfg.hostname).toEqual(configData.hostname);
expect(cfg.env.ENV).toEqual('1');
expect(cfg.env.OTHER).toEqual('2');
expect(cfg.labels.label).toEqual('1');
expect(cfg.labels.other).toEqual('2');
expect(cfg.template).toEqual(configData.template);
// restore inquirer
inquirer.prompt.restore();
// restore console
console.log.restore();
// remove corrupted config
fs.unlinkSync(configPath);
done();
});
});