From 662d1b938780b70837b8c82a8cd14e7b18d69556 Mon Sep 17 00:00:00 2001 From: fatme Date: Wed, 3 Apr 2019 13:22:54 +0300 Subject: [PATCH] feat: add support for ts unit tests --- declarations.d.ts | 3 +- main-view-model.ts | 4 +- services/karma-files-service.ts | 63 ++++++++++++++++++++++++------ services/test-execution-service.ts | 7 +--- 4 files changed, 55 insertions(+), 22 deletions(-) diff --git a/declarations.d.ts b/declarations.d.ts index 4b98b06..ecbdfac 100644 --- a/declarations.d.ts +++ b/declarations.d.ts @@ -44,6 +44,7 @@ interface IScriptInfo { localPath?: string; contents?: string; type?: ScriptTypes; + shouldEval?: boolean; } interface IKarmaHostResolver { @@ -55,7 +56,7 @@ interface IKarmaConnectionService { } interface IKarmaFilesService { - getServedFilesData(baseUrl: string, config: IHostConfiguration): Promise; + getServedFilesData(baseUrl: string): Promise; } interface ITestExecutionService { diff --git a/main-view-model.ts b/main-view-model.ts index 96a24fd..6160f79 100644 --- a/main-view-model.ts +++ b/main-view-model.ts @@ -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) @@ -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)); } diff --git a/services/karma-files-service.ts b/services/karma-files-service.ts index 91ca0a4..411934d 100644 --- a/services/karma-files-service.ts +++ b/services/karma-files-service.ts @@ -1,10 +1,21 @@ 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 { + 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 { 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); @@ -12,15 +23,12 @@ export class KarmaFilesService { }) .then(scriptUrls => { return Promise.all(scriptUrls.map((url): Promise => { - 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) @@ -28,7 +36,8 @@ export class KarmaFilesService { return { url, type, - contents + contents, + shouldEval: !extension || extension.toLowerCase() === "js" }; }); } @@ -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; + } } \ No newline at end of file diff --git a/services/test-execution-service.ts b/services/test-execution-service.ts index 68bcc59..64dfda6 100644 --- a/services/test-execution-service.ts +++ b/services/test-execution-service.ts @@ -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