Open benjlin opened 6 years ago
Hi @benjlin, Thank you for contacting us. We will check reported case and will verify if there is a real issue, which could lead to a crash on the application. You can keep track on the issue for further info.
I also experience this issue within my project and recreated it by following the testing documentation for the downloaded playground application. It seems when invoking the tns test
I get the following errors in the console for the browser:
@pekevski Did you discover the cause of your "uncaught ReferenceError: exports is not defined"
Same behavior while running "tns test android" for template app (https://github.com/NativeScript/template-drawer-navigation-ng). Any info about this issue?
Here's what I learned:
Use an emulator. When you start your app, (for me it became 'npm start') and Karma asked to open a browser,I was doing so...only to get the exports not defined error.
Having an emulator set up within the AVD management portion of Android studio helped. After lots of requests for me to open a browser, karma finally just opens the emulator.
However, I was still unable to run tests. I ran into "Zone is undefined" afterwards...which is likely related to the karma.config file set up, although I'm unsure why just yet.
Most recommendations I've seen advise not to use Karma on mobile apps, which has been in response to a lack of solution being found.
Either way, look at your karma.config file and try using an emulator.
The initial error that I got was on an emulator, I tried again on the simulator and also found that to be broken, I posted the results from the ios emulator dump and it seems that when my post was edited to read cleaner that was lost.
I will try again in the next few days to see if anything has changed with the 4.1.X release as well as downgrade to 3.4.X to see if it is present there
I ran a clean install of nativescript on a new computer, and when i downloaded the helloWorld template
tns create HelloWorld --template nativescript-template-ng-tutorial
I found it still broken by default, however after upgrading the typescript dev definition from "~2.7.2" to "2.9.2" i found that the tests work, after successfully running the test, the app crashes but it does successfully run the tests
19 07 2018 13:45:28.905:WARN [karma]: No captured browser, open http://localhost:9876/ Skipping prepare. Building project... Gradle build...
@pekevski , @CapellaBlue ,
I had the same problem. I've solved it by doing "tns test init", and picking the mocha framework. Then add the following files to /test folder:
test-config.ts . With content:
(
import * as chai from 'chai';
export let assert: typeof chai.assert = (
test-main.ts . With content:
import 'nativescript-angular/zone-js/testing.mocha'; import {nsTestBedInit} from 'nativescript-angular/testing'; nsTestBedInit();
example.ts . With content:
import {assert} from './test-config';
describe('A suite', function() { it('contains spec with an expectation', function() { assert.equal(true, true); }); });
Result:
JS: NSUTR: eval script /base/node_modules/chai/chai.js?40e7aa72e9665366bfd82579520de4fb0754dfae JS: NSUTR: eval script /base/node_modules/karma-chai/adapter.js?bab484a2111791847997ed88414622bccfbd2749 JS: NSUTR: eval script /base/node_modules/mocha/mocha.js?8bf1d1adf34e719cca6e8f6915b9cd6eabf83d6b JS: NSUTR: eval script /base/node_modules/karma-mocha/lib/adapter.js?e8826109cf50b958ee4bd667517729ffdd4de363 JS: NSUTR: require script /base/app/tests/example.js?5f2953ee9642c9402922f1858f6c6daeb474e644 from ../../tests/example.js JS: NSUTR: require script /base/app/tests/test-config.js?0feba9db7ae5fc2c47ffef99cf2314ec89d56428 from ../../tests/test-config.js JS: NSUTR: require script /base/app/tests/test-main.js?20993e7c421b970122391ecef7a52ed3b0866208 from ../../tests/test-main.js JS: NSUTR: beginning test run NativeScript / 27 (8.1.0; Android SDK built for x86): Executed 0 of 1 SUCCESS (0 secs / 0 secs) NativeScript / 27 (8.1.0; Android SDK built for x86): Executed 1 of 1 SUCCESS (0.141 secs / 0.002 secs) TOTAL: 1 SUCCESS
I ran into this too.
I created a new project with tns create <NAME> --ng
and configured testing with tns test init --framework jasmine
.
This gave the same error as @benjlin reported.
After a while I tried renaming src
to app
and updated my tsconfig.json
and nsconfig.json
to match that and that solved the problem.
I don't understand why it worked, but it did.
@m-abs I believe it works because the hooks didn't ran when you are using the new structure but when you changed to the old structure then the hooks will ran and the tests will success
I've managed to resolve this (exports is not defined
) by degrading jasmine-core
to ^2.9.0
.
Hey folks. I also managed to get the test running by renaming src
-> app
as @m-abs mentioned.
I had to change src
to app
in karma.config.js
, nsconfig.json
and tsconfig.json
as well.
After more digging around I found the reason why tests aren't working with src
folder.
The unit-test-runner app has a hardcoded app folder here. However files are in base/src
instead of base/app
. So the test files are not treated as "local" and are eval
-ed instead of required.
Maybe the solution is to pass the actual app
folder to the unit-test-app trough the config.js
options, so that it can correctly determine which files are tests.
@rosen-vladimirov - what do you think? I can do a PR if you think this is the correct way to go?
Even with a new project and latest versions, I had to rename src
=> app
to run my tests without getting ReferenceError: exports is not defined
.
I tried switching between mocha
and jasmine
same result.
I also needed to add android:usesCleartextTraffic="true"
to my AndroidManifest.xml
because I ran the test on an emulator with Android API 28.
My package.json
:
{
"nativescript": {
"id": "org.nativescript.demo",
"tns-android": {
"version": "5.1.0"
},
"tns-ios": {
"version": "5.1.0"
}
},
"description": "NativeScript Application",
"license": "SEE LICENSE IN <your-license-filename>",
"repository": "<fill-your-repository-here>",
"dependencies": {
"@angular/animations": "~7.2.0",
"@angular/common": "~7.2.0",
"@angular/compiler": "~7.2.0",
"@angular/core": "~7.2.0",
"@angular/forms": "~7.2.0",
"@angular/http": "~7.2.0",
"@angular/platform-browser": "~7.2.0",
"@angular/platform-browser-dynamic": "~7.2.0",
"@angular/router": "~7.2.0",
"@nota/nativescript-ngx-tools": "file:../src",
"nativescript-angular": "~7.2.0",
"nativescript-theme-core": "~1.0.4",
"nativescript-unit-test-runner": "^0.4.1",
"reflect-metadata": "~0.1.13",
"rxjs": "^6.4.0",
"tns-core-modules": "~5.1.2",
"zone.js": "^0.8.29",
"typescript": "~3.1.6"
},
"devDependencies": {
"@angular/compiler-cli": "~7.2.0",
"@nativescript/schematics": "~0.5.0",
"@ngtools/webpack": "~7.2.0",
"@types/jasmine": "^3.3.8",
"karma": "4.0.0",
"karma-jasmine": "2.0.1",
"karma-nativescript-launcher": "0.4.0",
"nativescript-dev-typescript": "~0.7.9",
"nativescript-dev-webpack": "~0.19.1",
"prettier": "^1.16.4",
"prettier-check": "^2.0.0",
"tns-platform-declarations": "^5.1.2"
},
"readme": "NativeScript Application"
}
Project was created with tns create --ng
followed by tns test init --framework jasmine
.
@m-abs,
This should be fixed in next version of {N} CLI, so you need
npm i -g nativescript@next
I am getting the following error when running karma unit tests:
Chrome 81.0.4044.138 (Mac OS 10.14.6) ERROR An error was thrown in afterAll Uncaught ReferenceError: global is not defined ReferenceError: global is not defined at http://127.0.0.1:9876src/tests/example.bundle.js:1:1
I am running tns cli v6.5.0 and its failing on the example unit test on a fresh angular NS projection
Reproduced by:
tns create <test> --ng
tns test init
tns test ios --emulator
I added (window as any).global = window;
in my main.ts and it seems to have gone away based on this
@pekevski I have added (window as any).global = window;
in my main.ts but still I am getting error. Do I need more changes to resolve this error?
I have the same issue. Running the auto generated example.ts results in an error:
An error was thrown in afterAll
Uncaught ReferenceError: global is not defined
Adding (window as any).global = window;
to main.ts does not solve the issue either.
When attempting to run unit testing on the default template app it crashes with
Steps to replicate
the same error occurs running both android and ios
I suspect that it is something to do with the default Karma configuration file generated