Skip to content

Commit 6b9e303

Browse files
committed
feat: add support for ts unit tests
1 parent c5a24a6 commit 6b9e303

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

declarations.d.ts

+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 {

services/karma-files-service.ts

+26-5
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,38 @@
11
export class KarmaFilesService {
2+
private extensionRegex = /\.([^.\/]+)$/;
3+
24
constructor(private http) { }
35

46
public getServedFilesData(baseUrl: string, config: IHostConfiguration): Promise<IScriptInfo[]> {
57
const contextUrl = `${baseUrl}/context.json`;
68
console.log("NSUTR: downloading " + contextUrl);
79
const bundle = config && config.options && config.options.bundle;
10+
const appPrefix = `/base/${config.options.appDirectoryRelativePath}/`;
811
const result = this.http.getString(contextUrl)
912
.then(content => {
1013
var parsedContent: IKarmaContext = JSON.parse(content);
1114
return parsedContent.files;
1215
})
1316
.then(scriptUrls => {
1417
return Promise.all(scriptUrls.map((url): Promise<IScriptInfo> => {
15-
var appPrefix = `/base/${config.options.appDirectoryRelativePath}/`;
1618
const type = this.getScriptType(url, config);
19+
const scriptExtension = this.getScriptExtension(url);
1720
if (!bundle && url.startsWith(appPrefix)) {
18-
var paramsStart = url.indexOf('?');
19-
var relativePath = url.substring(appPrefix.length, paramsStart);
21+
const localPath = this.getScriptLocalPath(url, scriptExtension, appPrefix);
2022
return Promise.resolve({
2123
url,
2224
type,
23-
localPath: '../../../' + relativePath,
25+
localPath,
2426
});
2527
} else {
2628
return this.http.getString(baseUrl + url)
2729
.then(contents => {
30+
const shouldEval = !scriptExtension || scriptExtension.toLowerCase() === "js";
2831
return {
2932
url,
3033
type,
31-
contents
34+
contents,
35+
shouldEval
3236
};
3337
});
3438
}
@@ -49,4 +53,21 @@ export class KarmaFilesService {
4953

5054
return type;
5155
}
56+
57+
private getScriptExtension(url: string): string {
58+
const queryStringStart = url.lastIndexOf('?');
59+
const pathWithoutQueryString = url.substring(0, queryStringStart);
60+
const scriptExtension = this.extensionRegex.exec(pathWithoutQueryString)[1];
61+
return scriptExtension;
62+
}
63+
64+
private getScriptLocalPath(url: string, scriptExtension: string, appPrefix: string): string {
65+
const paramsStart = url.indexOf('?');
66+
const relativePath = url.substring(appPrefix.length, paramsStart);
67+
let localPath = '../../../' + relativePath;
68+
if (scriptExtension === "ts") {
69+
localPath = localPath.substring(0, localPath.length - 2) + "js";
70+
}
71+
return localPath;
72+
}
5273
}

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)