Testing AngularJS express node app - node.js

I have an issue with testing my application with both jasmine-node and mocha (with zombie). The point is angular doesn't work with this test environment - when browser.visit "http://localhost:3000/" it displays only layout, and don't render any templates link. I guess javascript doesn't run in browser (I mean zombie object) at all. My test Is there any way to make it works? Or problem is different?
In jasmine spec that i copied from another project spec occures an error:
/home/alder/Node/angular-express-coffee/spec/app_spec.coffee:12
throw error;
^
ReferenceError: angular is not defined
at /js/services.js:4:3
at /js/services.js:6:4
in /js/services.js
So I guess javascript doesn't work too.
Update:
I add debug option and here what it displays:
➜ angular-express-coffee git:(master) ✗ mocha --require should --compilers coffee:coffee-script --colors -R spec --ui bdd
Given I am a new user
When I visit the home page
Zombie: GET http://localhost:3000/newPost => 200
Zombie: GET http://localhost:3000/ => 200
Zombie: GET http://localhost:3000/js/lib/jquery-1.7.2.min.js => 200
Zombie: GET http://localhost:3000/js/lib/bootstrap.min.js => 200
Zombie: GET http://localhost:3000/js/lib/angular.min.js => 200
Zombie: GET http://localhost:3000/js/app.js => 200
Zombie: GET http://localhost:3000/js/controllers.js => 200
Zombie: Unexpected token ILLEGAL SyntaxError: Unexpected token ILLEGAL
in /js/controllers.js
Zombie: GET http://localhost:3000/js/lib/jquery-1.7.2.min.js => 200
Zombie: Unexpected token ILLEGAL SyntaxError: Unexpected token ILLEGAL
in /js/lib/jquery-1.7.2.min.js
Zombie: GET http://localhost:3000/js/filters.js => 200
Zombie: GET http://localhost:3000/js/directives.js => 200
Zombie: Unexpected token ILLEGAL SyntaxError: Unexpected token ILLEGAL
in /js/directives.js
Zombie: Unexpected token ILLEGAL SyntaxError: Unexpected token ILLEGAL
in /js/filters.js
Zombie: GET http://localhost:3000/js/lib/bootstrap.min.js => 200
Zombie: Unexpected token ILLEGAL SyntaxError: Unexpected token ILLEGAL
in /js/lib/bootstrap.min.js
Zombie: GET http://localhost:3000/js/lib/angular.min.js => 200
Zombie: Unexpected token ILLEGAL SyntaxError: Unexpected token ILLEGAL
in /js/lib/angular.min.js
Zombie: GET http://localhost:3000/js/services.js => 200
Zombie: GET http://localhost:3000/js/filters.js => 200
Zombie: GET http://localhost:3000/js/directives.js => 200
Zombie: GET http://localhost:3000/js/controllers.js => 200
Zombie: GET http://localhost:3000/js/app.js => 200
Zombie: Unexpected token ILLEGAL SyntaxError: Unexpected token ILLEGAL
in /js/app.js
Zombie: Unexpected token ILLEGAL SyntaxError: Unexpected token ILLEGAL
in /js/controllers.js
Zombie: Unexpected token ILLEGAL SyntaxError: Unexpected token ILLEGAL
in /js/directives.js
Zombie: Unexpected token ILLEGAL SyntaxError: Unexpected token ILLEGAL
in /js/filters.js
Zombie: GET http://localhost:3000/js/services.js => 200
Perhaps the problem in coffeescript but i had add option to mocha. And is there any method to test angular apps without JsTestDriver propagated by Angular team.
Updated.
I had changed errors assertions to browser.errors.should.be.empty and it had shown me an error:
1) Given I am a new user When I visit the home page Then browser status shold be ok:
AssertionError: expected [ [Error: Unexpected token ILLEGAL],
[Error: Unexpected token ILLEGAL],
[Error: Unexpected token ILLEGAL],
[Error: Unexpected token ILLEGAL],
[Error: Unexpected token ILLEGAL],
[Error: Unexpected token ILLEGAL],
[Error: Unexpected token ILLEGAL],
[Error: angular is not defined] ] to be empty
at Object.empty (/home/alder/Node/angular-express-coffee/node_modules/should/lib/should.js:229:10)
at Context.<anonymous> (/home/alder/Node/angular-express-coffee/test/app_test.coffee:31:40)
at Test.Runnable.run (/usr/lib/node_modules/mocha/lib/runnable.js:184:32)
at Runner.runTest (/usr/lib/node_modules/mocha/lib/runner.js:300:10)
at Runner.runTests.next (/usr/lib/node_modules/mocha/lib/runner.js:346:12)
at next (/usr/lib/node_modules/mocha/lib/runner.js:228:14)
at Runner.hooks (/usr/lib/node_modules/mocha/lib/runner.js:237:7)
at next (/usr/lib/node_modules/mocha/lib/runner.js:185:23)
at Runner.hook (/usr/lib/node_modules/mocha/lib/runner.js:205:5)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
Last update:
The problem was in assets pipeline, all js was disabled for Zombie browser. Now I change to js from coffee and it works. But I prefer coffee, So if anyone can solve it, i'll be grateful.

Can it be because of Mocha? I hear it can cause problems because of its name...

Have you tried the Angular Scenario Runner?

Related

Error on see nestjs version and start new pro ject

enter image description here
Error on see NestJS Version
if (er?.code !== 'ENOENT') {
^
SyntaxError: Unexpected token '.'

SyntaxError Unexpected token at test-script

I have a Collection that has POST requests in it.
This collections needs bearer token for auth.
The Pre-request script and Tests assertion is written at Collection level.
Problem: The collection works fine when ran in Postman, but when I run it in Azure… it throws “Syntax Error Unexpected token at test-script inside…”. And none of the assertions written at collection level is validated as indicated by 0 in report.
How can I resolve Unexpected token issue and have the assertions run. Thanks.
PRE-Request Script:
postman.setEnvironmentVariable("guid", genHexString(32));
const parse =require('csv-parse/lib/sync');
const holiday = parse(pm.environment.get("holiday"),{
});
Tests:
const responseData = pm.response.json();
const csv_data = pm.environment.get("test_data");
const parse = require('csv-parse/lib/sync');
const csv_parsed_json_data = parse(csv_data, {
});
pm.test("Response has a body", () => {
pm.response.to.be.withBody;
});
pm.test("response body is json", () => {
pm.response.to.have.jsonBody()
});
Error message in Azure
# failure detail
01. SyntaxError
Unexpected token .
at test-script
inside "Set library"
02. SyntaxError
Unexpected token .
at test-script
inside "Obtain Token"
03. SyntaxError
Unexpected token .
at test-script
inside "Introspection"
04. SyntaxError
Unexpected token .
at test-script
inside "test case for so and so "

Getting error 500 instead of 401 on token expire in Hapi-auth-jwt2

I am using Hapi-auth-jwt2 and facing an issue after my JWT expire, instead of getting a 401 error I am getting 500 server error,
Here is my code:
const prepare = async () => {
if(prepared){
return;
}
await server.register(documentor as any);
server.auth.strategy('token','jwt',{
key:JWT_TOKEN,
validate: AuthService.verify,
verifyOptions:{
algorithms:['HS256']
}
});
server.auth.default('token');
server.validator(Joi);
server.route(routes);
prepared = true;}
it is working fine if token is valid, but in case of invalid token it suppose to return 401, but i am getting 500, if i am using ignoreExpiration: true in verifyOptions it is again working.
this is related to #328, since i am using v10.2.0 it is suppose to be fixed, still not working in my case.
Also in case of invalid token or expired token validate: AuthService.verify is not getting called.
I don't know what is wrong with it. Please help.
Here is a debug error message if that helps
Debug: auth, unauthenticated, error, token
Error: Expired token
at Object.raiseError (/Users/tinkeshwar/Sites/proj/tool/node_modules/hapi-auth-jwt2/lib/index.js:302:45)
at Object.internals.authenticate (/Users/tinkeshwar/Sites/proj/tool/node_modules/hapi-auth-jwt2/lib/index.js:171:26)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at Object.authenticate (/Users/tinkeshwar/Sites/proj/tool/node_modules/hapi-auth-jwt2/lib/index.js:353:22)
at exports.Manager.execute (/Users/tinkeshwar/Sites/proj/tool/node_modules/#hapi/hapi/lib/toolkit.js:60:28)
at module.exports.internals.Auth._authenticate (/Users/tinkeshwar/Sites/proj/tool/node_modules/#hapi/hapi/lib/auth.js:258:30)
at Request._lifecycle (/Users/tinkeshwar/Sites/proj/tool/node_modules/#hapi/hapi/lib/request.js:372:32)
at Request._execute (/Users/tinkeshwar/Sites/proj/tool/node_modules/#hapi/hapi/lib/request.js:280:9)
Debug: internal, error
ValidationError: "attributes" is not allowed
statusCode: Joi.any().optional().example(401),
error: Joi.any().example("Unauthorized"),
message: Joi.any().example("Missing authentication"),
attributes: {error: "Expired token"}
}).label("Unauthorized Error");
you should probably compare your schema for the unauthorized responses.

How to handle DiscordHTTPError: 401 Unauthorized on GET /api/v7/users/#me

I want to redirect users to login page if the access_token is invalid
I'm using discord-oauth2.
if (key != '0' && key != null && key != undefined) {
oauthclient.getUser(key).then((data)=>{
console.log(data)
});}
but if the token is invalid, like abcdefg the I get 401 error
(node:12732) UnhandledPromiseRejectionWarning: DiscordHTTPError: 401 Unauthorized on GET /api/v7/users/#me
how I can redirect to /login if I get 401 error

SyntaxError: Unexpected token u in JSON at position 0 Nodejs hue api

I try to make an API call with Node Js.
My Goal is to get the current state of my Phillips hue light.
But i always get an error message: SyntaxError: Unexpected token u in JSON at position 0
I searched the web but couldn't find a solution to solve the problem and i don't understand what this error message means.
here is the code i use to make the call( don't worry "username" is just a place holder).
const request = require('request');
request('https://192.168.1.10/api/<username>/lights/', function (error, response, body) {
var huelights = JSON.parse(body)
console.log(huelieghts);
});

Resources