@@ -9,90 +9,113 @@ const karmaPluginName = "karma";
99const unitTestsPluginName = "@nativescript/unit-test-runner" ;
1010
1111function getTestExecutionService ( ) : ITestExecutionService {
12- const injector = new InjectorStub ( ) ;
13- injector . register ( "testExecutionService" , TestExecutionService ) ;
14- injector . register ( "runController" , { } ) ;
12+ const injector = new InjectorStub ( ) ;
13+ injector . register ( "testExecutionService" , TestExecutionService ) ;
14+ injector . register ( "runController" , { } ) ;
1515
16- return injector . resolve ( "testExecutionService" ) ;
16+ return injector . resolve ( "testExecutionService" ) ;
1717}
1818
1919function getDependenciesObj ( deps : string [ ] ) : IDictionary < string > {
20- const depsObj : IDictionary < string > = { } ;
21- deps . forEach ( ( dep ) => {
22- depsObj [ dep ] = "1.0.0" ;
23- } ) ;
20+ const depsObj : IDictionary < string > = { } ;
21+ deps . forEach ( ( dep ) => {
22+ depsObj [ dep ] = "1.0.0" ;
23+ } ) ;
2424
25- return depsObj ;
25+ return depsObj ;
2626}
2727
2828describe ( "testExecutionService" , ( ) => {
29- const testCases = [
30- {
31- name :
32- "should return false when the project has no dependencies and dev dependencies" ,
33- expectedCanStartKarmaServer : false ,
34- projectData : { dependencies : { } , devDependencies : { } } ,
35- } ,
36- {
37- name : "should return false when the project has no karma" ,
38- expectedCanStartKarmaServer : false ,
39- projectData : {
40- dependencies : getDependenciesObj ( [ unitTestsPluginName ] ) ,
41- devDependencies : { } ,
42- } ,
43- } ,
44- {
45- name : "should return false when the project has no unit test runner" ,
46- expectedCanStartKarmaServer : false ,
47- projectData : {
48- dependencies : getDependenciesObj ( [ karmaPluginName ] ) ,
49- devDependencies : { } ,
50- } ,
51- } ,
52- {
53- name :
54- "should return true when the project has the required plugins as dependencies" ,
55- expectedCanStartKarmaServer : true ,
56- projectData : {
57- dependencies : getDependenciesObj ( [
58- karmaPluginName ,
59- unitTestsPluginName ,
60- ] ) ,
61- devDependencies : { } ,
62- } ,
63- } ,
64- {
65- name :
66- "should return true when the project has the required plugins as dev dependencies" ,
67- expectedCanStartKarmaServer : true ,
68- projectData : {
69- dependencies : { } ,
70- devDependencies : getDependenciesObj ( [
71- karmaPluginName ,
72- unitTestsPluginName ,
73- ] ) ,
74- } ,
75- } ,
76- {
77- name :
78- "should return true when the project has the required plugins as dev and normal dependencies" ,
79- expectedCanStartKarmaServer : true ,
80- projectData : {
81- dependencies : getDependenciesObj ( [ karmaPluginName ] ) ,
82- devDependencies : getDependenciesObj ( [ unitTestsPluginName ] ) ,
83- } ,
84- } ,
85- ] ;
29+ const testCases = [
30+ {
31+ name :
32+ "should return false when the project has no dependencies and dev dependencies" ,
33+ expectedCanStartKarmaServer : false ,
34+ projectData : { dependencies : { } , devDependencies : { } } ,
35+ } ,
36+ {
37+ name : "should return false when the project has no karma" ,
38+ expectedCanStartKarmaServer : false ,
39+ projectData : {
40+ dependencies : getDependenciesObj ( [ unitTestsPluginName ] ) ,
41+ devDependencies : { } ,
42+ } ,
43+ } ,
44+ {
45+ name : "should return false when the project has no unit test runner" ,
46+ expectedCanStartKarmaServer : false ,
47+ projectData : {
48+ dependencies : getDependenciesObj ( [ karmaPluginName ] ) ,
49+ devDependencies : { } ,
50+ } ,
51+ } ,
52+ {
53+ name :
54+ "should return true when the project has the required plugins as dependencies" ,
55+ expectedCanStartKarmaServer : true ,
56+ projectData : {
57+ dependencies : getDependenciesObj ( [
58+ karmaPluginName ,
59+ unitTestsPluginName ,
60+ ] ) ,
61+ devDependencies : { } ,
62+ } ,
63+ } ,
64+ {
65+ name :
66+ "should return true when the project has the required plugins as dev dependencies" ,
67+ expectedCanStartKarmaServer : true ,
68+ projectData : {
69+ dependencies : { } ,
70+ devDependencies : getDependenciesObj ( [
71+ karmaPluginName ,
72+ unitTestsPluginName ,
73+ ] ) ,
74+ } ,
75+ } ,
76+ {
77+ name :
78+ "should return true when the project has the required plugins as dev and normal dependencies" ,
79+ expectedCanStartKarmaServer : true ,
80+ projectData : {
81+ dependencies : getDependenciesObj ( [ karmaPluginName ] ) ,
82+ devDependencies : getDependenciesObj ( [ unitTestsPluginName ] ) ,
83+ } ,
84+ } ,
85+ ] ;
8686
87- describe ( "canStartKarmaServer" , ( ) => {
88- _ . each ( testCases , ( testCase : any ) => {
89- it ( `${ testCase . name } ` , async ( ) => {
90- const testExecutionService = getTestExecutionService ( ) ;
91- const canStartKarmaServer = await testExecutionService . canStartKarmaServer (
92- testCase . projectData
93- ) ;
94- assert . equal ( canStartKarmaServer , testCase . expectedCanStartKarmaServer ) ;
95- } ) ;
96- } ) ;
97- } ) ;
87+ describe ( "canStartKarmaServer" , ( ) => {
88+ _ . each ( testCases , ( testCase : any ) => {
89+ it ( `${ testCase . name } ` , async ( ) => {
90+ const testExecutionService = getTestExecutionService ( ) ;
91+
92+ // todo: cleanup monkey-patch with a friendlier syntax (util?)
93+ // MOCK require.resolve
94+ const Module = require ( 'module' ) ;
95+ const originalResolveFilename = Module . _resolveFilename
96+
97+ Module . _resolveFilename = function ( ...args : any ) {
98+ if ( args [ 0 ] . startsWith ( karmaPluginName )
99+ && ( testCase . projectData . dependencies [ karmaPluginName ]
100+ || testCase . projectData . devDependencies [ karmaPluginName ] )
101+ ) {
102+ // override with a "random" built-in module to
103+ // ensure the module can be resolved
104+ args [ 0 ] = 'fs'
105+ }
106+
107+ return originalResolveFilename . apply ( this , args )
108+ }
109+ // END MOCK
110+
111+ const canStartKarmaServer = await testExecutionService . canStartKarmaServer (
112+ testCase . projectData
113+ ) ;
114+ assert . equal ( canStartKarmaServer , testCase . expectedCanStartKarmaServer ) ;
115+
116+ // restore mock
117+ Module . _resolveFilename = originalResolveFilename ;
118+ } ) ;
119+ } ) ;
120+ } ) ;
98121} ) ;
0 commit comments