Skip to content

Commit 662d1b9

Browse files
committed
feat: add support for ts unit tests
1 parent c5a24a6 commit 662d1b9

File tree

4 files changed

+55
-22
lines changed

4 files changed

+55
-22
lines changed

declarations.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ interface IScriptInfo {
4444
localPath?: string;
4545
contents?: string;
4646
type?: ScriptTypes;
47+
shouldEval?: boolean;
4748
}
4849

4950
interface IKarmaHostResolver {
@@ -55,7 +56,7 @@ interface IKarmaConnectionService {
5556
}
5657

5758
interface IKarmaFilesService {
58-
getServedFilesData(baseUrl: string, config: IHostConfiguration): Promise<IScriptInfo[]>;
59+
getServedFilesData(baseUrl: string): Promise<IScriptInfo[]>;
5960
}
6061

6162
interface ITestExecutionService {

main-view-model.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class TestBrokerViewModel extends Observable {
7070
this.startEmitted = false;
7171

7272
this.karmaHostResolver = new KarmaHostResolver(http);
73-
this.karmaFilesService = new KarmaFilesService(http);
73+
this.karmaFilesService = new KarmaFilesService(http, config);
7474
this.testExecutionService = new TestExecutionService();
7575

7676
this.karmaHostResolver.resolveKarmaHost(config.ips, config.port)
@@ -110,7 +110,7 @@ export class TestBrokerViewModel extends Observable {
110110

111111
this.set('goToTestsText', 'View Test Run');
112112

113-
this.karmaFilesService.getServedFilesData(this.baseUrl, config)
113+
this.karmaFilesService.getServedFilesData(this.baseUrl)
114114
.then((scriptsContents: IScriptInfo[]) => setTimeout(() => this.runTests(scriptsContents), 0));
115115
}
116116

services/karma-files-service.ts

+50-13
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,43 @@
11
export class KarmaFilesService {
2-
constructor(private http) { }
2+
private extensionRegex = /\.([^.\/]+)$/;
3+
private appPrefix = null;
4+
private testsPrefix = null;
5+
private nodeModulesPrefix = null;
6+
private bundle = false;
37

4-
public getServedFilesData(baseUrl: string, config: IHostConfiguration): Promise<IScriptInfo[]> {
8+
constructor(private http, config: IHostConfiguration) {
9+
this.appPrefix = `/base/${config.options.appDirectoryRelativePath}/`;
10+
this.testsPrefix = `/base/${config.options.appDirectoryRelativePath}/tests`;
11+
this.nodeModulesPrefix = `/base/node_modules/`;
12+
this.bundle = config.options.bundle;
13+
}
14+
15+
public getServedFilesData(baseUrl: string): Promise<IScriptInfo[]> {
516
const contextUrl = `${baseUrl}/context.json`;
617
console.log("NSUTR: downloading " + contextUrl);
7-
const bundle = config && config.options && config.options.bundle;
18+
819
const result = this.http.getString(contextUrl)
920
.then(content => {
1021
var parsedContent: IKarmaContext = JSON.parse(content);
1122
return parsedContent.files;
1223
})
1324
.then(scriptUrls => {
1425
return Promise.all(scriptUrls.map((url): Promise<IScriptInfo> => {
15-
var appPrefix = `/base/${config.options.appDirectoryRelativePath}/`;
16-
const type = this.getScriptType(url, config);
17-
if (!bundle && url.startsWith(appPrefix)) {
18-
var paramsStart = url.indexOf('?');
19-
var relativePath = url.substring(appPrefix.length, paramsStart);
26+
const { extension, localPath, type } = this.getScriptData(url);
27+
if (localPath) {
2028
return Promise.resolve({
2129
url,
2230
type,
23-
localPath: '../../../' + relativePath,
31+
localPath,
2432
});
2533
} else {
2634
return this.http.getString(baseUrl + url)
2735
.then(contents => {
2836
return {
2937
url,
3038
type,
31-
contents
39+
contents,
40+
shouldEval: !extension || extension.toLowerCase() === "js"
3241
};
3342
});
3443
}
@@ -38,15 +47,43 @@ export class KarmaFilesService {
3847
return result;
3948
}
4049

41-
private getScriptType(url: string, config: IHostConfiguration): ScriptTypes {
50+
private getScriptData(url: string): { extension: string, localPath: string, type: ScriptTypes } {
51+
const queryStringStartIndex = url.lastIndexOf('?');
52+
const pathWithoutQueryString = url.substring(0, queryStringStartIndex);
53+
const extension = this.extensionRegex.exec(pathWithoutQueryString)[1];
54+
55+
const type = this.getScriptType(url);
56+
57+
let localPath = null;
58+
if (!this.bundle && url.startsWith(this.appPrefix)) {
59+
localPath = this.getScriptLocalPath(url, extension);
60+
}
61+
62+
return { extension, localPath, type };
63+
}
64+
65+
private getScriptType(url: string): ScriptTypes {
4266
let type = ScriptTypes.CodeUnderTestType;
4367

44-
if (url.startsWith(`/base/${config.options.appDirectoryRelativePath}/tests`)) {
68+
if (url.startsWith(this.testsPrefix)) {
4569
type = ScriptTypes.TestType;
46-
} else if (url.startsWith(`/base/node_modules/`)) {
70+
} else if (url.startsWith(this.nodeModulesPrefix)) {
4771
type = ScriptTypes.FrameworkAdapterType;
4872
}
4973

5074
return type;
5175
}
76+
77+
private getScriptLocalPath(url: string, scriptExtension: string): string {
78+
let localPath = null;
79+
const queryStringStartIndex = url.lastIndexOf('?');
80+
const relativePath = url.substring(this.appPrefix.length, queryStringStartIndex);
81+
localPath = '../../../' + relativePath;
82+
83+
if (scriptExtension === "ts") {
84+
localPath = localPath.substring(0, localPath.length - 2) + "js";
85+
}
86+
87+
return localPath;
88+
}
5289
}

services/test-execution-service.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,7 @@ export class TestExecutionService implements ITestExecutionService {
3030
require(script.localPath);
3131
}
3232
} else {
33-
const queryStringStart = script.url.lastIndexOf('?');
34-
const pathWithoutQueryString = script.url.substring(0, queryStringStart);
35-
const extensionRegex = /\.([^.\/]+)$/;
36-
const fileExtension = extensionRegex.exec(pathWithoutQueryString)[1];
37-
38-
if (!fileExtension || fileExtension.toLowerCase() === "js") {
33+
if (script.shouldEval) {
3934
console.log('NSUTR: eval script ' + script.url);
4035
this.loadShim(script.url);
4136
//call eval indirectly to execute the scripts in the global scope

0 commit comments

Comments
 (0)