Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit 0fa8c76

Browse files
committed
chore(): ensured all tests pass with jest and added serve, uglifyjs tests.
1 parent 4b42211 commit 0fa8c76

File tree

10 files changed

+192
-80
lines changed

10 files changed

+192
-80
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
"@types/express": "^4.0.33",
7777
"@types/fs-extra": "^0.0.33",
7878
"@types/glob": "^5.0.30",
79-
"@types/jasmine": "^2.2.33",
8079
"@types/jest": "^16.0.1",
8180
"@types/mock-fs": "^3.6.29",
8281
"@types/node": "^6.0.38",
@@ -89,7 +88,7 @@
8988
"github": "0.2.4",
9089
"ionic-cz-conventional-changelog": "1.0.0",
9190
"jasmine": "2.5.2",
92-
"jest": "^17.0.3",
91+
"jest": "^18.0.0",
9392
"mock-fs": "3.11.0",
9493
"node-sass": "3.10.1",
9594
"rewire": "^2.5.2",
@@ -122,7 +121,8 @@
122121
},
123122
"typings": "dist/index.d.ts",
124123
"jest": {
125-
"moduleFileExtensions": [
124+
"testEnvironment": "node",
125+
"moduleFileExtensions": [
126126
"ts",
127127
"js"
128128
],

src/clean.spec.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,28 @@ import * as clean from './clean';
44
describe('clean task', () => {
55

66
describe('clean', () => {
7-
it('should empty the build directory', (done: Function) => {
7+
it('should empty the build directory', () => {
88
// arrage
99
spyOn(fs, fs.emptyDirSync.name).and.returnValue('things');
1010
const context = { buildDir: 'something' };
1111

1212
// act
13-
clean.clean(context).then(() => {
13+
return clean.clean(context).then(() => {
1414
// assert
1515
expect(fs.emptyDirSync).toHaveBeenCalledWith(context.buildDir);
16-
done();
1716
});
1817
});
1918

20-
it('should throw when failing to empty dir', async () => {
19+
it('should throw when failing to empty dir', () => {
2120
// arrage
2221
spyOn(fs, fs.emptyDirSync.name).and.throwError('Simulating an error');
2322
const context = { buildDir: 'something' };
2423

2524
// act
26-
let error: Error = null;
27-
try {
28-
await clean.clean(context);
29-
} catch (ex) {
30-
error = ex;
31-
}
32-
33-
// assert
34-
expect(error instanceof Error).toBe(true, 'Error is not an instance of type Error');
35-
expect(typeof error.message).toBe('string', 'error.message is not a string');
25+
return clean.clean(context).catch((ex) => {
26+
expect(ex instanceof Error).toBe(true);
27+
expect(typeof ex.message).toBe('string');
28+
});
3629
});
3730
});
3831
});

src/clean.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ import { Logger } from './logger/logger';
55

66

77
export function clean(context: BuildContext) {
8-
const logger = new Logger('clean');
8+
return new Promise((resolve, reject) => {
9+
const logger = new Logger('clean');
910

10-
try {
11-
Logger.debug(`[Clean] clean: cleaning ${context.buildDir}`);
11+
try {
12+
Logger.debug(`[Clean] clean: cleaning ${context.buildDir}`);
1213

13-
emptyDirSync(context.buildDir);
14-
logger.finish();
14+
emptyDirSync(context.buildDir);
15+
logger.finish();
1516

16-
} catch (ex) {
17-
throw logger.fail(new BuildError(`Failed to clean directory ${context.buildDir} - ${ex.message}`));
18-
}
19-
20-
return Promise.resolve();
17+
} catch (ex) {
18+
reject(logger.fail(new BuildError(`Failed to clean directory ${context.buildDir} - ${ex.message}`)));
19+
}
20+
resolve();
21+
});
2122
}

src/cleancss.spec.ts

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { join } from 'path';
2-
import * as rewire from 'rewire';
3-
4-
const cleanCss = rewire('./cleancss');
2+
import * as cleanCss from './cleancss';
53

64
import * as cleanCssFactory from './util/clean-css-factory';
5+
import { CleanCssConfig, getCleanCssInstance } from './util/clean-css-factory'
76
import * as config from './util/config';
87
import * as helpers from './util/helpers';
98
import * as workerClient from './worker-client';
@@ -12,38 +11,36 @@ import * as workerClient from './worker-client';
1211
describe('clean css task', () => {
1312

1413
describe('cleancss', () => {
15-
it('should return when the worker returns', (done: Function) => {
14+
it('should return when the worker returns', () => {
1615
// arrange
1716
const context = { };
1817
const configFile: any = null;
1918
const spy = spyOn(workerClient, workerClient.runWorker.name).and.returnValue(Promise.resolve());
2019
// act
21-
(cleanCss as any).cleancss(context, null).then(() => {
20+
return (cleanCss as any).cleancss(context, null).then(() => {
2221
// assert
2322
expect(spy).toHaveBeenCalledWith('cleancss', 'cleancssWorker', context, configFile);
24-
done();
2523
});
2624
});
2725

28-
it('should throw when the worker throws', (done: Function) => {
26+
it('should throw when the worker throws', () => {
2927
// arrange
3028
const context = { };
3129
const errorMessage = 'Simulating an error';
3230
spyOn(workerClient, workerClient.runWorker.name).and.returnValue(Promise.reject(new Error(errorMessage)));
3331

3432
// act
35-
(cleanCss as any).cleancss(context, null).then(() => {
33+
return (cleanCss as any).cleancss(context, null).then(() => {
3634
throw new Error('Should never get here');
3735
}).catch((err: Error) => {
3836
// assert
39-
expect(err.message).toEqual(errorMessage, `Expected ex.message ${err.message} to equal ${errorMessage}`);
40-
done();
37+
expect(err.message).toEqual(errorMessage);
4138
});
4239
});
4340
});
4441

4542
describe('cleancssworker', () => {
46-
it('should throw when reading the file throws', (done: Function) => {
43+
it('should throw when reading the file throws', () => {
4744
const errorMessage = 'simulating an error';
4845
// arrange
4946
const context = { buildDir: 'www'};
@@ -53,15 +50,14 @@ describe('clean css task', () => {
5350
spyOn(helpers, helpers.readFileAsync.name).and.returnValue(Promise.reject(new Error(errorMessage)));
5451

5552
// act
56-
(cleanCss as any).cleancssWorker(context, null).then(() => {
53+
return (cleanCss as any).cleancssWorker(context, null).then(() => {
5754
throw new Error('Should never get here');
5855
}).catch((err: Error) => {
5956
expect(err.message).toEqual(errorMessage);
60-
done();
6157
});
6258
});
6359

64-
it('should return what writeFileAsync returns', (done: Function) => {
60+
it('should return what writeFileAsync returns', () => {
6561
// arrange
6662
const context = { buildDir: 'www'};
6763
const cleanCssConfig = { sourceFileName: 'sourceFileName', destFileName: 'destFileName'};
@@ -71,26 +67,25 @@ describe('clean css task', () => {
7167
spyOn(config, config.fillConfigDefaults.name).and.returnValue(cleanCssConfig);
7268
spyOn(helpers, helpers.readFileAsync.name).and.returnValue(Promise.resolve(fileContent));
7369
spyOn(helpers, helpers.writeFileAsync.name).and.returnValue(Promise.resolve());
74-
75-
// use rewire to stub this since jasmine is insufficient
76-
const spy = jasmine.createSpy('mySpy').and.returnValue(Promise.resolve(minifiedContent));
77-
cleanCss.__set__('runCleanCss', spy);
70+
spyOn(cleanCssFactory, 'getCleanCssInstance').and.returnValue({
71+
minify: (content: string, cb: Function) => {
72+
cb(null, { styles: minifiedContent });
73+
}
74+
});
7875

7976
// act
80-
(cleanCss as any).cleancssWorker(context, null).then(() => {
77+
return (cleanCss as any).cleancssWorker(context, null).then(() => {
8178
// assert
8279
expect(config.generateContext).toHaveBeenCalledWith(context);
8380
expect(config.fillConfigDefaults).toHaveBeenCalledWith(null, (cleanCss as any).taskInfo.defaultConfigFile);
8481
expect(helpers.readFileAsync).toHaveBeenCalledWith(join(context.buildDir, cleanCssConfig.sourceFileName));
8582
expect(helpers.writeFileAsync).toHaveBeenCalledWith(join(context.buildDir, cleanCssConfig.destFileName), minifiedContent);
86-
expect(spy).toHaveBeenCalledWith(cleanCssConfig, fileContent);
87-
done();
8883
});
8984
});
9085
});
9186

9287
describe('runCleanCss', () => {
93-
it('should reject when minification errors out', (done: Function) => {
88+
it('should reject when minification errors out', () => {
9489
// arrange
9590
const errorMessage = 'simulating an error';
9691
const configFile = { options: {} };
@@ -108,16 +103,15 @@ describe('clean css task', () => {
108103
const callback = minifySpy.calls.mostRecent().args[1];
109104
callback(new Error(errorMessage), null);
110105

111-
promise.then(() => {
106+
return promise.then(() => {
112107
throw new Error('Should never get here');
113108
}).catch((err: Error) => {
114109
// assert
115110
expect(err.message).toEqual(errorMessage);
116-
done();
117111
});
118112
});
119113

120-
it('should reject when minification has one or more errors', (done: Function) => {
114+
it('should reject when minification has one or more errors', () => {
121115
// arrange
122116
const configFile = { options: {} };
123117
const fileContent = 'fileContent';
@@ -137,16 +131,15 @@ describe('clean css task', () => {
137131
const callback = minifySpy.calls.mostRecent().args[1];
138132
callback(null, minificationResponse);
139133

140-
promise.then(() => {
134+
return promise.then(() => {
141135
throw new Error('Should never get here');
142136
}).catch((err: Error) => {
143137
// assert
144138
expect(err.message).toEqual(minificationResponse.errors[0]);
145-
done();
146139
});
147140
});
148141

149-
it('should return minified content', (done: Function) => {
142+
it('should return minified content', () => {
150143
const configFile = { options: {} };
151144
const fileContent = 'fileContent';
152145
let minifySpy: jasmine.Spy = null;
@@ -166,11 +159,10 @@ describe('clean css task', () => {
166159
const callback = minifySpy.calls.mostRecent().args[1];
167160
callback(null, minificationResponse);
168161

169-
promise.then((result: string) => {
162+
return promise.then((result: string) => {
170163
expect(result).toEqual(minificationResponse.styles);
171164
expect(cleanCssFactory.getCleanCssInstance).toHaveBeenCalledWith(configFile.options);
172165
expect(minifySpy.calls.mostRecent().args[0]).toEqual(fileContent);
173-
done();
174166
});
175167
});
176168
});

src/serve.spec.ts

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import * as serve from './serve';
2+
import * as config from './util/config';
3+
import { BuildContext } from './util/interfaces';
4+
import { ServeConfig } from './dev-server/serve-config';
5+
6+
import * as watch from './watch';
7+
import * as open from './util/open';
8+
import * as notificationServer from './dev-server/notification-server';
9+
import * as httpServer from './dev-server/http-server';
10+
import * as liveReloadServer from './dev-server/live-reload';
11+
import * as network from './util/network';
12+
13+
describe('test serve', () => {
14+
let configResults: ServeConfig;
15+
let context: BuildContext;
16+
17+
beforeEach(() => {
18+
context = {
19+
rootDir: '/',
20+
wwwDir: '/www',
21+
buildDir: '/build',
22+
};
23+
configResults = {
24+
httpPort: 8100,
25+
host: '0.0.0.0',
26+
rootDir: '/',
27+
wwwDir: '/www',
28+
buildDir: '/build',
29+
isCordovaServe: false,
30+
launchBrowser: true,
31+
launchLab: false,
32+
browserToLaunch: null,
33+
useLiveReload: true,
34+
liveReloadPort: 35729,
35+
notificationPort: 53703,
36+
useServerLogs: false,
37+
useProxy: true,
38+
notifyOnConsoleLog: false
39+
};
40+
spyOn(network, 'findClosestOpenPort').and.callFake((host: string, port: number) => Promise.resolve(port));
41+
spyOn(notificationServer, 'createNotificationServer');
42+
spyOn(liveReloadServer, 'createLiveReloadServer');
43+
spyOn(httpServer, 'createHttpServer');
44+
spyOn(watch, 'watch').and.returnValue(Promise.resolve());
45+
spyOn(open, 'default');
46+
});
47+
48+
it('should work with no args on a happy path', () => {
49+
return serve.serve(context).then(() => {
50+
expect(network.findClosestOpenPort).toHaveBeenCalledWith('0.0.0.0', 53703);
51+
expect(notificationServer.createNotificationServer).toHaveBeenCalledWith(configResults);
52+
expect(liveReloadServer.createLiveReloadServer).toHaveBeenCalledWith(configResults);
53+
expect(httpServer.createHttpServer).toHaveBeenCalledWith(configResults);
54+
expect(open.default).toHaveBeenCalledWith('http://localhost:8100', null);
55+
});
56+
});
57+
58+
it('should include ionicplatform in the browser url if platform is passed', () => {
59+
config.addArgv('--platform');
60+
config.addArgv('android');
61+
62+
return serve.serve(context).then(() => {
63+
expect(network.findClosestOpenPort).toHaveBeenCalledWith('0.0.0.0', 53703);
64+
expect(notificationServer.createNotificationServer).toHaveBeenCalledWith(configResults);
65+
expect(liveReloadServer.createLiveReloadServer).toHaveBeenCalledWith(configResults);
66+
expect(httpServer.createHttpServer).toHaveBeenCalledWith(configResults);
67+
expect(open.default).toHaveBeenCalledWith('http://localhost:8100?ionicplatform=android', null);
68+
});
69+
})
70+
71+
it('all args should be set in the config object and should be passed on to server functions', () => {
72+
config.setProcessArgs([]);
73+
config.addArgv('--serverlogs');
74+
configResults.useServerLogs = true;
75+
config.addArgv('--consolelogs');
76+
configResults.notifyOnConsoleLog = true;
77+
config.addArgv('--noproxy');
78+
configResults.useProxy = false;
79+
config.addArgv('--nolivereload');
80+
configResults.useLiveReload = false;
81+
config.addArgv('--lab');
82+
configResults.launchLab = true;
83+
config.addArgv('--browser');
84+
config.addArgv('safari');
85+
configResults.browserToLaunch = 'safari';
86+
config.addArgv('--port');
87+
config.addArgv('8101');
88+
configResults.httpPort = 8101;
89+
config.addArgv('--address');
90+
config.addArgv('127.0.0.1');
91+
configResults.host = '127.0.0.1';
92+
config.addArgv('--livereload-port');
93+
config.addArgv('35729');
94+
configResults.liveReloadPort = 35729;
95+
config.addArgv('--dev-logger-port');
96+
config.addArgv('53703');
97+
configResults.notificationPort = 53703;
98+
99+
return serve.serve(context).then(() => {
100+
expect(network.findClosestOpenPort).toHaveBeenCalledWith('127.0.0.1', 53703);
101+
expect(notificationServer.createNotificationServer).toHaveBeenCalledWith(configResults);
102+
expect(liveReloadServer.createLiveReloadServer).toHaveBeenCalledWith(configResults);
103+
expect(httpServer.createHttpServer).toHaveBeenCalledWith(configResults);
104+
expect(open.default).toHaveBeenCalledWith('http://127.0.0.1:8101/ionic-lab', 'safari');
105+
});
106+
});
107+
});

0 commit comments

Comments
 (0)