Skip to content

Commit a3fa811

Browse files
Fix first unit tests execution
First call to `tns test <platform>` always fails as the file with Karma configuration is not sent to device. The reason is that it's craeted in `node_modules/unit-test-runner` after the project is prepared. The second execution of the command will include the file, but it may not be correct. It's regenerated, but again after the project is prepared. Fix the `test-execution-service` to prepare the project after the configuration file is created. Fixes #1636 and possibly #2016 Fixes the problem in Travis builds that console hangs and travis builds never end.
1 parent f80953d commit a3fa811

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

lib/services/test-execution-service.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ class TestExecutionService implements ITestExecutionService {
2828
private $options: IOptions,
2929
private $pluginsService: IPluginsService,
3030
private $errors: IErrors,
31-
private $androidDebugService:IDebugService,
31+
private $androidDebugService: IDebugService,
3232
private $iOSDebugService: IDebugService,
3333
private $devicesService: Mobile.IDevicesService,
3434
private $childProcess: IChildProcess) {
3535
}
3636

3737
public platform: string;
3838

39-
public startTestRunner(platform: string) : IFuture<void> {
39+
public startTestRunner(platform: string): IFuture<void> {
4040
return (() => {
4141
this.platform = platform;
4242
this.$options.justlaunch = true;
@@ -72,7 +72,7 @@ class TestExecutionService implements ITestExecutionService {
7272
debugService.debugStart().wait();
7373
}
7474
blockingOperationFuture.return();
75-
} catch(err) {
75+
} catch (err) {
7676
// send the error to the real future
7777
blockingOperationFuture.throw(err);
7878
}
@@ -91,45 +91,49 @@ class TestExecutionService implements ITestExecutionService {
9191
platform = platform.toLowerCase();
9292
this.platform = platform;
9393

94-
if(this.$options.debugBrk && this.$options.watch) {
94+
if (this.$options.debugBrk && this.$options.watch) {
9595
this.$errors.failWithoutHelp("You cannot use --watch and --debug-brk simultaneously. Remove one of the flags and try again.");
9696
}
9797

98-
if (!this.$platformService.preparePlatform(platform).wait()) {
99-
this.$errors.failWithoutHelp("Verify that listed files are well-formed and try again the operation.");
100-
}
98+
// We need the dependencies installed here, so we can start the Karma server.
99+
this.$pluginsService.ensureAllDependenciesAreInstalled().wait();
101100

102101
let projectDir = this.$projectData.projectDir;
103102
this.$devicesService.initialize({ platform: platform, deviceId: this.$options.device }).wait();
104103

105104
let karmaConfig = this.getKarmaConfiguration(platform),
106105
karmaRunner = this.$childProcess.fork(path.join(__dirname, "karma-execution.js"));
107-
108-
karmaRunner.send({karmaConfig: karmaConfig});
109106
karmaRunner.on("message", (karmaData: any) => {
110107
fiberBootstrap.run(() => {
111108
this.$logger.trace("## Unit-testing: Parent process received message", karmaData);
112109
let port: string;
113-
if(karmaData.url) {
110+
if (karmaData.url) {
114111
port = karmaData.url.port;
115112
let socketIoJsUrl = `http://${karmaData.url.host}/socket.io/socket.io.js`;
116113
let socketIoJs = this.$httpClient.httpRequest(socketIoJsUrl).wait().body;
117114
this.$fs.writeFile(path.join(projectDir, TestExecutionService.SOCKETIO_JS_FILE_NAME), socketIoJs).wait();
118115
}
119116

120-
if(karmaData.launcherConfig) {
117+
if (karmaData.launcherConfig) {
121118
let configOptions: IKarmaConfigOptions = JSON.parse(karmaData.launcherConfig);
122119
let configJs = this.generateConfig(port, configOptions);
123120
this.$fs.writeFile(path.join(projectDir, TestExecutionService.CONFIG_FILE_NAME), configJs).wait();
124121
}
125122

126-
if(this.$options.debugBrk) {
123+
// Prepare the project AFTER the TestExecutionService.CONFIG_FILE_NAME file is created in node_modules
124+
// so it will be sent to device.
125+
if (!this.$platformService.preparePlatform(platform).wait()) {
126+
this.$errors.failWithoutHelp("Verify that listed files are well-formed and try again the operation.");
127+
}
128+
129+
if (this.$options.debugBrk) {
127130
this.getDebugService(platform).debug().wait();
128131
} else {
129132
this.liveSyncProject(platform).wait();
130133
}
131134
});
132135
});
136+
133137
karmaRunner.on("exit", (exitCode: number) => {
134138
if (exitCode !== 0) {
135139
//End our process with a non-zero exit code
@@ -140,6 +144,9 @@ class TestExecutionService implements ITestExecutionService {
140144
karmaFuture.return();
141145
}
142146
});
147+
148+
karmaRunner.send({ karmaConfig: karmaConfig });
149+
143150
return karmaFuture;
144151
}
145152

@@ -172,9 +179,9 @@ class TestExecutionService implements ITestExecutionService {
172179

173180
private getDebugService(platform: string): IDebugService {
174181
let lowerCasedPlatform = platform.toLowerCase();
175-
if(lowerCasedPlatform === this.$devicePlatformsConstants.iOS.toLowerCase()) {
182+
if (lowerCasedPlatform === this.$devicePlatformsConstants.iOS.toLowerCase()) {
176183
return this.$iOSDebugService;
177-
} else if(lowerCasedPlatform === this.$devicePlatformsConstants.Android.toLowerCase()) {
184+
} else if (lowerCasedPlatform === this.$devicePlatformsConstants.Android.toLowerCase()) {
178185
return this.$androidDebugService;
179186
}
180187

0 commit comments

Comments
 (0)