Leaving out index.js for unit testing

I have an app with extensive unit tests (using Jasmine 2.4.1). Under Enyo 2.5, I put new App().renderInto(document.body); in a separate JS file, which was included in index.html for the app, but was not included in SpecRunner.html, which runs the unit tests.

Under Enyo 2.7, that file becomes index.js, and is folded into the app JS file in dist. Is there a way to exclude it, or prevent it from running, so my Jasmine unit tests can pull in all the app code, without instantiating my app?

Comments

  • I think you can follow a similar path in 2.7. You'd need to include a custom HTML template (enyo pack -t path/to/template.jade) that executing the rendering and update your app's main key in package.json to point to your definition of App rather than index.js.
  • I think you are right that changing the value of "main" in package.json (or replacing index.js) is the essential mechanism. It's unfortunate that there's no command line flag for that.
  • For anyone interested in how I ended up doing this:

    1. In package.json, in the devAssets array, I include SpecRunner.html (the base file for running unit tests)

    2. SpecRunner.html pulls in Jasmine using script tags, then the rest is copied from the generated index.html

    3. index.js contains
    
    var
    	ready = require('enyo/ready');
    
    if ("jasmine" in window) {
    	var
    		testErrorToMessage = require('./test/test-errorToMessage'),
    		testHumanDate = require('./test/test-humanDate'),
    		testCleanHtml = require('./test/test-cleanHtml'),
    		testIdb = require('./test/test-idb'),
    		testNoteService = require('./test/test-NoteService'),
    		testFileSelectionService = require('./test/test-FileSelectionService'),
    		testAlarmService = require('./test/test-AlarmService'),
    		testPrefHelpPanel = require('./test/test-PrefHelpPanel'),
    		testListPane = require('./test/test-ListPane'),
    		testDetailPanel = require('./test/test-DetailPanel'),
    		testBanner = require('./test/test-Banner'),
    		testApp = require('./test/test-app');
    } else {
    	var App = require('./src/App');
    }
    
    ready(function () {
    	if ("jasmine" in window) {
    		console.log("running Jasmine tests");
    	} else {
    		new App().renderInto(document.body);
    	}
    });
    
  • To allow production builds, I've written a Bash script to replace index.js, depending on the build. Production builds use a streamlined index.js, which does not mention the test files (so enyo pack won't pull them in).
    
    #!/usr/bin/env bash
    
    ORIGINALDIR=${PWD}
    DIST=dist
    
    while test $# -gt 0; do
        case "$1" in
        	-t | --test )
        		TEST="true"
        		shift
        		;;
    
            -p | --production )
            	PRODUCTION="true"
            	shift
            	;;
    
    		-w | --webos )
    			WEBOS="true"
            	shift
     			;;
    
    		-i | --install )
    			INSTALL="true"
            	shift
     			;;
    
    		-c | --chrome-app )
    			CHROMEAPP="true"
            	shift
    			;;
    
            *)
            	EXTRA="$EXTRA $1"
            	shift
            	;;
        esac
    done
    
    
    if [ "$PRODUCTION" = "true" ]; then
    	if [ "$TEST" = "true" ]; then
    		echo Including tests
    		cp -p index-test.js index.js
    	else
    		cp -p index-production.js index.js
    	fi
    	echo enyo pack --reset-cache --clean --production --no-inline-js $EXTRA
    	enyo pack --reset-cache --clean --production --no-inline-js $EXTRA
    else
    	echo Including tests
    	cp -p index-test.js index.js
    	echo enyo pack --reset-cache --clean --dev-mode $EXTRA
    	enyo pack --reset-cache --clean --dev-mode $EXTRA
    fi
    
    
    if [ "$WEBOS" = "true" ]; then
    	# package it up for Lune OS
    	palm-package "$DIST"
    fi
    
    if [ "$INSTALL" = "true" ]; then
    	# re-install to attached Lune OS device
    	adb push "$DIST" /media/cryptofs/apps/usr/palm/applications/com.hominidsoftware.searchablenotes
    	adb shell systemctl restart luna-next
    
    	# enable inspection of web views
    	adb forward tcp:1122 tcp:1122
    fi
    
    if [ "$CHROMEAPP" = "true" ]; then
    	# Chrome app: make sure deploy.json contains "assets": [... "/manifest.json", "./chrome-background.js"]
    
    	# package it up
    	rm -f SereneNotes.zip
    	cd $DIST
    	zip -r --quiet "../SereneNotes" *
    	cd $ORIGINALDIR
    	echo "packaged for Chrome: SereneNotes.zip"
    fi
    
Sign In or Register to comment.