I\'d like to unit test a service, however, when running the test, I get the following error:
Uncaught (in promise) SyntaxError: Unexpected token o in
No need to parse data.
Usually when you encounter this error, it means you do not need to parse your data.
Try using the 'token' directly.
For sanity, print your data in your console. (you can also Stringify it and print it see what it gives you)
SyntaxError: Unexpected token o in JSON at position 1
Reasons as per the error :
Content-Type as JSON as a response from the API but you are getting response as a String start with alphabet o. Hence, It throws an error that Unexpected token o in JSON at position 1.text/html you need to parse, and if the response header is application/json it is already parsed for you.If you parse it again it will give you unexpected token o.If response is already a JSON Object then no need to use response.json().
As it turned out I missed the import for Response:
Before: import { BaseRequestOptions, Http, ResponseOptions } from '@angular/http';
After: import { BaseRequestOptions, Http, Response, ResponseOptions } from '@angular/http';
Without importing Response from @angular/http, Response defaults to ECMAScript APIs.
The error Unexpected token ... in JSON at position 1 actually means that JSON.parse was applied to something that is not valid JSON - a random string or a value that is not a string at all which was coerced to string.
The message Unexpected token o ... implies that parsed value was most likely an object - which is coerced to [object ...] string.
The problem is clearly seen here:
Response
body: ReadableStream
locked: true
__proto__: Object
bodyUsed: true
...
Response object here is an instance of global Response constructor (a part of Fetch API), and the presence of ReadableStream clearly indicates this.
As it can be seen in Fetch polyfill, all that res.json() does is applying JSON.parse to some value
this.json = function() {
return this.text().then(JSON.parse)
}
which is likely new ResponseOptions object that was erroneously supplied to global Response constructor, hence the error.
Angular has similarly named Response class which derives its interface from Fetch Response but obviously isn't compatible. The problem is that it was never imported and thus global Response was used instead.
It should be
import { Response, ResponseOptions, ... } from '@angular/http';
Globals are declared in Typescript type definitions, they cannot be undeclared but can be re-declared in custom type definition:
custom.d.ts
declare var Headers: undefined;
declare var Request: undefined;
declare var Response: undefined;
declare var URLSearchParams: undefined;
Using globals instead of imported Http classes of the same name will result in type error:
TS2532: Object is possibly 'undefined'.
Which is a desirable behaviour as long as Fetch API isn't used in Angular app.
This is not an issue for HttpClient that replaced Http in Angular 4.3 and doesn't use classes of same names as the ones from Fetch API.