Skip to content

feat: add support for ts unit tests #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ interface IScriptInfo {
localPath?: string;
contents?: string;
type?: ScriptTypes;
shouldEval?: boolean;
}

interface IKarmaHostResolver {
Expand All @@ -55,7 +56,7 @@ interface IKarmaConnectionService {
}

interface IKarmaFilesService {
getServedFilesData(baseUrl: string, config: IHostConfiguration): Promise<IScriptInfo[]>;
getServedFilesData(baseUrl: string): Promise<IScriptInfo[]>;
}

interface ITestExecutionService {
Expand Down
4 changes: 2 additions & 2 deletions main-view-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class TestBrokerViewModel extends Observable {
this.startEmitted = false;

this.karmaHostResolver = new KarmaHostResolver(http);
this.karmaFilesService = new KarmaFilesService(http);
this.karmaFilesService = new KarmaFilesService(http, config);
this.testExecutionService = new TestExecutionService();

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

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

this.karmaFilesService.getServedFilesData(this.baseUrl, config)
this.karmaFilesService.getServedFilesData(this.baseUrl)
.then((scriptsContents: IScriptInfo[]) => setTimeout(() => this.runTests(scriptsContents), 0));
}

Expand Down
63 changes: 50 additions & 13 deletions services/karma-files-service.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
export class KarmaFilesService {
constructor(private http) { }
private extensionRegex = /\.([^.\/]+)$/;
private appPrefix = null;
private testsPrefix = null;
private nodeModulesPrefix = null;
private bundle = false;

public getServedFilesData(baseUrl: string, config: IHostConfiguration): Promise<IScriptInfo[]> {
constructor(private http, config: IHostConfiguration) {
this.appPrefix = `/base/${config.options.appDirectoryRelativePath}/`;
this.testsPrefix = `/base/${config.options.appDirectoryRelativePath}/tests`;
this.nodeModulesPrefix = `/base/node_modules/`;
this.bundle = config.options.bundle;
}

public getServedFilesData(baseUrl: string): Promise<IScriptInfo[]> {
const contextUrl = `${baseUrl}/context.json`;
console.log("NSUTR: downloading " + contextUrl);
const bundle = config && config.options && config.options.bundle;

const result = this.http.getString(contextUrl)
.then(content => {
var parsedContent: IKarmaContext = JSON.parse(content);
return parsedContent.files;
})
.then(scriptUrls => {
return Promise.all(scriptUrls.map((url): Promise<IScriptInfo> => {
var appPrefix = `/base/${config.options.appDirectoryRelativePath}/`;
const type = this.getScriptType(url, config);
if (!bundle && url.startsWith(appPrefix)) {
var paramsStart = url.indexOf('?');
var relativePath = url.substring(appPrefix.length, paramsStart);
const { extension, localPath, type } = this.getScriptData(url);
if (localPath) {
return Promise.resolve({
url,
type,
localPath: '../../../' + relativePath,
localPath,
});
} else {
return this.http.getString(baseUrl + url)
.then(contents => {
return {
url,
type,
contents
contents,
shouldEval: !extension || extension.toLowerCase() === "js"
};
});
}
Expand All @@ -38,15 +47,43 @@ export class KarmaFilesService {
return result;
}

private getScriptType(url: string, config: IHostConfiguration): ScriptTypes {
private getScriptData(url: string): { extension: string, localPath: string, type: ScriptTypes } {
const queryStringStartIndex = url.lastIndexOf('?');
const pathWithoutQueryString = url.substring(0, queryStringStartIndex);
const extension = this.extensionRegex.exec(pathWithoutQueryString)[1];

const type = this.getScriptType(url);

let localPath = null;
if (!this.bundle && url.startsWith(this.appPrefix)) {
localPath = this.getScriptLocalPath(url, extension);
}

return { extension, localPath, type };
}

private getScriptType(url: string): ScriptTypes {
let type = ScriptTypes.CodeUnderTestType;

if (url.startsWith(`/base/${config.options.appDirectoryRelativePath}/tests`)) {
if (url.startsWith(this.testsPrefix)) {
type = ScriptTypes.TestType;
} else if (url.startsWith(`/base/node_modules/`)) {
} else if (url.startsWith(this.nodeModulesPrefix)) {
type = ScriptTypes.FrameworkAdapterType;
}

return type;
}

private getScriptLocalPath(url: string, scriptExtension: string): string {
let localPath = null;
const queryStringStartIndex = url.lastIndexOf('?');
const relativePath = url.substring(this.appPrefix.length, queryStringStartIndex);
localPath = '../../../' + relativePath;

if (scriptExtension === "ts") {
localPath = localPath.substring(0, localPath.length - 2) + "js";
}

return localPath;
}
}
7 changes: 1 addition & 6 deletions services/test-execution-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ export class TestExecutionService implements ITestExecutionService {
require(script.localPath);
}
} else {
const queryStringStart = script.url.lastIndexOf('?');
const pathWithoutQueryString = script.url.substring(0, queryStringStart);
const extensionRegex = /\.([^.\/]+)$/;
const fileExtension = extensionRegex.exec(pathWithoutQueryString)[1];

if (!fileExtension || fileExtension.toLowerCase() === "js") {
if (script.shouldEval) {
console.log('NSUTR: eval script ' + script.url);
this.loadShim(script.url);
//call eval indirectly to execute the scripts in the global scope
Expand Down