tntim96 / JSCover

JSCover is a JavaScript Code Coverage Tool that measures line, branch and function coverage
GNU General Public License v2.0
399 stars 84 forks source link

chrome console throw 'unsafe-eval' when I use File Mode generated js file replace web server js folder. #290

Closed MH1176 closed 4 years ago

MH1176 commented 4 years ago

Hi, Such as the title, my action as following:

run command java -Dfile.encoding=UTF-8 -jar /JSCover-all.jar -fs --local-storage ./js1 ./js2 in WebServer js folder.

In this time, use chrome open the web, is ok, look like no problem, I can run jscoverage_serializeCoverageToJSON(); in chrome console get jscoverage.json string when I run my test case.

but, if refresh the current page or use other browsers open it, such as chrome console throws an error, Refused to evaluate a string as JaveScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "default-src 'self'.

I don't understand, my usage is right?

If I hope to use File Mode compiled js file in webserver, what should I do?

thank you for your time.

tntim96 commented 4 years ago

There is an eval in JSCover which I've just removed.

I've released it in the 2.0.10 SNAPSHOT if you'd like to test.

<repositories>
    <repository>
        <id>sonatype-nexus-snapshots</id>
        <name>Sonatype Nexus Snapshots</name>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

Or build the latest JAR yourself or download from here.

Let me know if that fixes your issue and I'll build a release.

MH1176 commented 4 years ago

So sorry, I‘m tried to build the project, it has some errors thrown in console. that doesn't matter, I'm pythoner, java debug capability is not good. At the same time, your download link shows not found, so I hope to get a new after building download link. Cannot resolve plugin org.apache.maven.plugins:maven-checkstyle-plugin:<unknown> Cannot resolve plugin org.apache.maven.plugins:maven-gpg-plugin:<unknown> Cannot resolve plugin org.apache.maven.plugins:maven-source-plugin:<unknown> Cannot resolve plugin org.apache.maven.plugins:maven-javadoc-plugin:<unknown>

thanks!

tntim96 commented 4 years ago

At the same time, your download link shows not found

I've just updated the link. Please try again.

MH1176 commented 4 years ago

I've updated JSCover-all.js, chrome can useful by selenium WebDirver. however, from the desktop chrome icon open browser, still those errors. I’ve three js files, it throws three the same errors, just a different name.

tntim96 commented 4 years ago

from the desktop chrome icon open browser, still those errors

Maybe you need to clear the cache?

MH1176 commented 4 years ago

I cleaned the cache, and my browser is disabling cache mode. The same with other browsers(Firefox, Edge). I've tried to use chrome stealth mode, I can open the page, but can't refresh.

tntim96 commented 4 years ago

Well JSCover no longer serves the eval. Could it be coming from elsewhere?

MH1176 commented 4 years ago

Well JSCover no longer serves the eval. Could it be coming from elsewhere?

I following the error stack in the generated file. In the function jscoverage_parseCoverageJSON(data) , has var key, json, the value is eval(‘(’ + data + ')'); console error from here throw.

Is it helpful?

tntim96 commented 4 years ago

I think I uploaded an older version. Please download and try again - sorry about that. I've tested this download and it's the right version.

MH1176 commented 4 years ago

There is an eval in JSCover which I've just removed.

I've released it in the 2.0.10 SNAPSHOT if you'd like to test.

<repositories>
    <repository>
        <id>sonatype-nexus-snapshots</id>
        <name>Sonatype Nexus Snapshots</name>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

Or build the latest JAR yourself or download from here.

Let me know if that fixes your issue and I'll build a release.

I'm from here download jar again, this time, error 'unsafe-eval' is disappearing. But has a new issue, console shows Uncaught ReferenceError: angular is not defined and cannot read property 'init' of undefined Following the error stack is _$jscoverage['/lib.min.js'].branchDate['29'][451].init(4095,13);

tntim96 commented 4 years ago

Looks like you're trying to generate coverage on library files. Can you skip those?

http://tntim96.github.io/JSCover/manual/manual.xml#fileMode

--exclude=PATH ...or... --exclude-reg=PATH

MH1176 commented 4 years ago

I use java -Dfile.encoding=UTF-8 -jar JSCover-all.jar -fs path1 path2 generate js file, is work. If I addition argument --local-storage is thrown that errors.

and now, I want to know, if I do not add --local-storage, jscover will be count different page jscoverage?

tntim96 commented 4 years ago

I want to know, if I do not add --local-storage

...then the coverage data is lost on each page load

MH1176 commented 4 years ago

I want to know, if I do not add --local-storage

...then the coverage data is lost on each page load

so, if I wanna saved every page data, I only use --local-storage? or run jscoverage_serializeCoverageToJSON(); to save data when browser change address?

tntim96 commented 4 years ago

Correct

MH1176 commented 4 years ago

God, why do I make mistakes when I use --local-storage...

console shows Uncaught ReferenceError: angular is not defined and cannot read property 'init' of undefined Following the error stack is _$jscoverage['/lib.min.js'].branchDate['29'][451].init(4095,13);

tntim96 commented 4 years ago

Have you tried excluding with --exclude=PATH ...or... --exclude-reg=PATH

e.g. --exclude=/lib.min.js

MH1176 commented 4 years ago

Yap, I'm tried, the command is java -Dfile.encoding=UTF-8 -jar JSCover-all.jar -fs --local-storage --exclude=./path1/1.js --exclude=./path1/2.js --exclude=./path1/3.js ./path1 ./path2; still Uncaught ReferenceError: angular is not defined and cannot read property 'init' of undefined and Following the error stack is _$jscoverage['/lib.min.js'].branchDate['29'][451].init(4095,13);

tntim96 commented 4 years ago

Did you mean to use --exclude=./path1/1.js instead of --exclude=/path1/1.js?

The code _$jscoverage['/lib.min.js'].branchDate['29'][451].init(4095,13); shouldn't exist if it's been excluded.

tntim96 commented 4 years ago

Also, I've release v2.0.10 with the fix to the eval JavaScript.

MH1176 commented 4 years ago

fine, I tried so many different ways, absolute path, relative path, but still those errors, I almost to give up. maybe should use another way to record.