Switch between angular test/dev module and production module - javascript

I am implementing and an Angular app. In e2e tests, I want to mock some of the request to the server and pass through some i.e I want to use e2e httpBackend.
Here is Vijittas example of how to use the HttpBackend.
` http://jsfiddle.net/vojtajina/DQHdk/ `
Now, here is the dilemma: When I am testing I want my application to boostrap with development module i.e.
<html ng-app="AppDevModule">
and when I run the server I want the production module to be included.
<html ng-app="AppCoreModule">
but I don't find it reasonable to change the HTML whenever I want to change between development mode and production mode.
The documentation of e2e httpBackend
provides a code snippet for including a development module, but they didn't mentioned anything about the problem and the inclusion of the dev app.
I am using angular testacular. I tried to configure it in the e2e tests like this:
describe("DHCP Client e2e. ", function () {
beforeEach(function () {
var fakeAppModule = angular.module('AppCoreModule', ['AppCoreModule', 'ngMockE2E']);
fakeAppModule.run(function ($httpBackEnd) {
var networkInterface = [
{
'secondarySubnets':[
{"dhcpOfferOptions":{"dnsServers":["8.8.8.8"], "offerTime":"400", "leaseTime":"600"}, "rangesLimits":[],
"network":"192.168.0.0", "slash":"24", "gateway":"192.168.0.1",
"isDynamic":"dynamic", "description":"asdsadsa"}
]
},
{
'secondarySubnets':[
{"dhcpOfferOptions":{"dnsServers":["8.8.8.8"], "offerTime":"400", "leaseTime":"600"}, "rangesLimits":[],
"network":"192.168.0.0", "slash":"24", "gateway":"192.168.0.1",
"isDynamic":"dynamic", "description":"asdsadsa"}
]
}
];
$httpBackEnd.whenGET('/^\/view\//').respond(200, '<div></div>');
$httpBackEnd.whenGET('/r/networkInterface').respond(200, networkInterface);
$httpBackEnd.whenGET('./../main/webapp/r/networkInterface').respond(200, networkInterface);
});
fakeAppModule.config(function ($provide) {
$provide.decorator('$httpBackend', angular.mock.e2e.$httpBackendDecorator);
});
});
but, things do not go as I expect.

Based on the following thread:
https://groups.google.com/forum/?fromgroups=#!topic/angular/3Xm7ZOmhNp0
it looks like mock backend is ment to be used when the real one is not yet implemented. After this is in place the real one is recommended to be used as maintaining two versions may lead to tests not really proving the production application working correctly.
To sum up switching between two application modules for testing and production should be avoided if possible.

Related

differentiate between make and package in Electron Forge config

I have an Electron Forge config file set up with many options and it all works automagically and beautifully (thanks Forge team!!). But I have found certain situations where I might want to handle a bare npm run package differently than a full npm run make (which as I understand it runs the package script as part of its process). Is there any way to programmatically detect whether the package action was run direct from the command line rather than as part of the make process, so that I can invoke different Forge configuration options depending? For example sometimes I just want to do a quick build for local testing and skip certain unnecessary time-consuming steps such as notarizing on macOS and some prePackage/postPackage hook functions. Ideally I'm looking for a way to do something like this in my Forge config file:
//const isMake = ???
module.exports = {
packagerConfig: {
osxNotarize: isMake ? {appleId: "...", appleIdPassword: "..."} : undefined
},
hooks: {
prePackage: isMake ? someFunction : differentFunction
}
}
You can do it by process.argv[1]:
let isMake;
if (process.argv[1].endsWith('electron-forge-make.js') {
isMake = true;
} else {
isMake = false;
}
module.exports = {
// ...
}
When calling process.argv, it returns an array with two strings: The first one with node.js directory and the second one with electron forge module directory.
The make module ends with electron-forge-make.js and package module ends with electron-forge-package.js. So you can look at the end of it and determine whether it's package or make.

protractor 3.0.0 and cucumber automated testing

I am currently using protractor, cucumber and chai/chai-as-promised for my automated tests. My current code is using protractor 1.8.0 and I would like to update it to the most recent version. The problem is that the most recent version of protractor doesn't support cucumber.
To use cucumber as your framework, protractor (http://angular.github.io/protractor/#/frameworks) points you to using protractor-cucumber-framework (https://github.com/mattfritz/protractor-cucumber-framework). I have tried integrating this with my current code and some smaller example projects with no luck at getting them working. The main error I get is:
Error: Step timed out after 5000 milliseconds at Timer.listOnTimeout
(timers.js:92:15)
I have tried changing the default timeout globally as cucumber suggests by:// features/support/env.js
var configure = function () {
this.setDefaultTimeout(60 * 1000);
};
module.exports = configure;
But I seem to be missing something with my setup.
So, does anyone know of a good example that can show me the proper setup for the new protractor/cucumber framework? If not, does anyone know of an example that shows how to change the default timeout globally?
You should add
this.setDefaultTimeout(60000);
to one of your step_def files. For example:
module.exports = function () {
this.setDefaultTimeout(60000);
this.After(function (callback) { ... }
}
Or you should add //features/support/env.js to
cucumberOpts:{require: ['//features/support/env.js']}
to array with your stepDefinition files
thx to #Ivan,
with cucumber-protractor-framework and typescript:
in protractor.conf.js
cucumberOpts: {
compiler: "ts:ts-node/register",
require: [
'./src/env.ts', //<- added
'./src/**/*.steps.ts'
]
},
in src/env.ts:
import {setDefaultTimeout} from 'cucumber';
setDefaultTimeout(9001);

Why does using Mongo in package tests fail when the package has api.use('mongo')?

I'm developing a custom package. Its package.js is :
Package.describe({
name: 'adigiovanni:one-way-accounts',
version: '0.0.1',
summary: 'One Way Accounts',
git: '',
documentation: 'README.md',
});
Package.onUse(function (api) {
api.versionsFrom('1.2.0.2');
api.use('ecmascript');
api.use('mongo');
// api.imply('mongo');
api.addFiles([
'lib/collections/Accounts.js',
'lib/methods.js',
'lib/OneWayAccounts.js',
]);
api.export('OneWayAccounts');
});
Package.onTest(function (api) {
api.use([
'ecmascript',
'sanjo:jasmine#0.20.2',
'velocity:html-reporter',
]);
api.use('adigiovanni:one-way-accounts');
api.addFiles('tests/client/OneWayAccounts.js', 'client');
api.addFiles('tests/server/OneWayAccounts.js', 'server');
});
As you can see, package makes use of 'mongo'.
Tests fail with :
Reference error: Mongo is not defined
But if I uncomment the line api.imply('mongo') then tests succeed.
Same odd behavior applies to ecmascript dependency, if I don't api.use('ecmascript') in Package.onTest, tests fail.
Meteor version is 1.2.0.2.
Test runner is velocity.
Test framework is jasmine.
I am using Mongo and ES6 syntax and features in my tests.
What is happening and how can I fix it?
Using a package with api.use('other-package') in Package.onUse does not make 'other-package' available in your test codes in the same way it doesn't make it available for other packages that use('my-package') or in applications that meteor add my-package. To solve this issue there is two solutions depending on the need for other-package :
Allowing users of the package (including your tests) to access 'other-package' with api.imply
Package.onUse(function (api) {
//...
api.imply('other-package')
//...
})
This makes sense if and only if the package you imply is necessary to use your own package. Do not imply everything willy-nilly for scope convenience. See more in this question.
If it does not fall into that category,
Simply use the package in your tests
Package.onTest(function (api) {
//...
api.use('my-package')
api.use('other-package')
//...
})
This will allow you to use other-package in your tests too, without polluting scopes.

Different settings for debug/local ("grunt serve") vs. dist/build ("grunt")?

I want to define some application settings, but I want to provide different values depending on whether I'm running in 'debug' mode (e.g. grunt serve), or whether the final compiled app is running (e.g. the output of grunt). That is, something like:
angular.module('myApp').factory('AppSettings', function() {
if (DebugMode()) { // ??
return { apiPort: 12345 };
} else {
return { apiPort: 8008 };
}
});
How can I accomplish this?
The way I handle it in my apps:
move all your config data for one environment to a file: config.js, config.json,... whatever your app finds easy to read.
now modify your config file to turn it into a template using grunt config values, and generate the file with grunt-template as part of your build - for example: app.constant('myAppConfig', {bananaHammocks: <%= banana.hammocks %>});
finally, add grunt-stage to switch grunt config values depending on environment: create your different config/secret/(env).json files, update your template (app.constant('myAppConfig', {bananaHammocks: <%= stg.banana.hammocks %>});), and then grunt stage:local:build or grunt stage:prod:build
I find this the good balance between complexity and features (separation between environments, runtime code not concerned with building options,...)

Exclude folders from builds in Brocfile

Is there a way to exclude a folder from a build in a Brocfile (or any other place).
The use case is packaging, where I have an app made of sub-apps within pods. eg.
/app/modules/components
/app/modules/app1
/app/modules/app2
/app/modules/app3
I'd like to build them all when environment is set to 'development' or only eg. 'app1' when environment is 'app1'. Any suggestions?
I have tried different combinations of broccoli-file-remover, broccoli-funnel and broccoli-merge-trees to no avail.
var removeFile = require('broccoli-file-remover');
module.exports = removeFile(app.toTree(), {
paths: ['app/modules/pod1/', 'app/modules/pod2/']
});
Ah, so after actually thinking about this clearly, everything is actually working exactly as expected in my previous example.
I clearly wasn't paying enough attention. app.toTree() is far too late to perform this operation, as everything has already been built and concated.
Luckily, ember-cli does enable addons to modify the appropriate trees at various life cycle milestones.
See: https://github.com/ember-cli/ember-cli/blob/master/ADDON_HOOKS.md for more details on which hooks are currently available.
The hook that should do the trick is Addon.prototype.postprocessTree. Now we have two choices, we can build a standalone addon, via ember addon or we can create a light-weight in-repo addon via ember g in-repo-addon. Typically for these types of situations, I prefer in-repo-addons as they don't require a second project, but otherwise they are the same.
ember g in-repo-addon remove
we need to install broccoli-stew via npm install --save broccoli-stew
include it var stew = require('broccoli-stew');
add hook postprocessTree to the add-on
when the postprocessTree is for the type we care about, use broccoli-stew to remove the directories we no longer care care.
The resulting pull request: https://github.com/WooDzu/ember-exclude-pod/pull/1
Note: I noticed template wasn't one of the types available in postprocess, so I added it: https://github.com/ember-cli/ember-cli/pull/4263 (should be part of the next ember-cli release)
Note: we really do want an additional hook
Addon.prototype.preprocessTree, as to ignore the files before we
even build them. I have opened a related issue:
https://github.com/ember-cli/ember-cli/issues/4262
output of the above steps
var stew = require('broccoli-stew');
module.exports = {
name: 'remove',
isDevelopingAddon: function() {
return true;
},
postprocessTree: function(type, tree){
if (type === 'js' || type === 'template') {
return stew.rm(tree, '*/modules/pod{1,2}/**/*');
} else {
return tree;
}
}
};
I am pretty confident broccoli-stew's rm will handle this correctly.
https://github.com/stefanpenner/broccoli-stew/blob/master/lib/rm.js#L4-L40 there are even tests that test a very similar scenario: https://github.com/stefanpenner/broccoli-stew/blob/master/tests/rm-test.js#L48-L57
var stew = require('broccoli-stew');
module.exports = stew.rm(app.tree(), 'app/modules/{pod1,pod2}');
If this doesn't work, feel free to open an issue on broccoli-stew. Be sure to provide a running example though
This is really late, but I created a Broccoli plugin to do just this. It's available at https://www.npmjs.com/package/broccoli-rm.
(The trick is to detect whether an excluded path is a folder, and then use a glob match to make sure that none of the children of the folder get symlinked during copying.)
var rm = require('broccoli-rm');
var input = app.toTree();
module.exports = output = rm([input], {
paths: ['app/modules/pod1', 'app/modules/pod2']
});

Categories

Resources