This repository was archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathcli-build-public-library.spec.js
98 lines (89 loc) · 2.79 KB
/
cli-build-public-library.spec.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
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
96
97
98
/*jshint jasmine: true, node: true */
'use strict';
const mock = require('mock-require');
const fs = require('fs-extra');
const rimraf = require('rimraf');
const logger = require('../utils/logger');
const skyPagesConfigUtil = require('../config/sky-pages/sky-pages.config');
describe('cli build-public-library', () => {
const requirePath = '../cli/build-public-library';
let webpackConfig;
let mockWebpack;
beforeEach(() => {
mockWebpack = () => {
return {
run: (cb) => {
cb(null, {
toJson: () => ({
errors: [],
warnings: []
})
});
}
};
};
mock('../cli/utils/stage-library-ts', () => {});
mock('../cli/utils/prepare-library-package', () => {});
mock('../config/webpack/build-public-library.webpack.config.js', {
getWebpackConfig: () => {
webpackConfig = {
entry: ''
};
return webpackConfig;
}
});
spyOn(process, 'exit').and.callFake(() => {});
spyOn(skyPagesConfigUtil, 'spaPath').and.returnValue('');
spyOn(skyPagesConfigUtil, 'spaPathTemp').and.callFake((fileName = '') => {
return fileName;
});
spyOn(rimraf, 'sync').and.callFake(() => {});
spyOn(fs, 'writeJSONSync').and.callFake(() => {});
});
afterEach(() => {
mock.stopAll();
});
it('should return a function', () => {
const cliCommand = require(requirePath);
expect(cliCommand).toEqual(jasmine.any(Function));
});
it('should clean the dist and temp directories', (done) => {
const cliCommand = require(requirePath);
cliCommand({}, mockWebpack).then(() => {
expect(rimraf.sync).toHaveBeenCalled();
expect(skyPagesConfigUtil.spaPathTemp).toHaveBeenCalled();
expect(skyPagesConfigUtil.spaPath).toHaveBeenCalledWith('dist');
done();
});
});
it('should write a tsconfig.json file', (done) => {
const cliCommand = require(requirePath);
cliCommand({}, mockWebpack).then(() => {
const firstArg = fs.writeJSONSync.calls.argsFor(0)[0];
expect(firstArg).toEqual('tsconfig.json');
done();
});
});
it('should pass config to webpack', (done) => {
const cliCommand = require(requirePath);
cliCommand({}, mockWebpack).then(() => {
expect(webpackConfig).toEqual(jasmine.any(Object));
expect(webpackConfig.entry).toEqual(jasmine.any(String));
done();
});
});
it('should handle errors thrown by webpack', (done) => {
const errorMessage = 'Something bad happened.';
spyOn(logger, 'error');
mockWebpack = () => {
return {
run: (cb) => cb(errorMessage)
};
};
const cliCommand = mock.reRequire(requirePath);
cliCommand({}, mockWebpack).then(() => {
expect(logger.error).toHaveBeenCalledWith(errorMessage);
done();
});
});
});