PDA

View Full Version : Ext.create is not a function



Xatenev
7 Apr 2017, 3:14 AM
Hi,

I've been following the docs exactly playing around with Sencha Test. When I try to do the following I get the error seen below. Is that a bug in the docs or a bug in the actual application? I'd love if anybody could help me out quick here so I can go on :).


It seems like its not injecting the Ext global object into the test.


node stc.js run --pool="Embedded"

http://termbin.com/5gdx

craigcomstock
10 Apr 2017, 6:56 AM
Can you include your project.json and some portion of your (or all) of your test?

I suspect that you might be running a WebDriver Scenario and trying to use Ext.create directly. This won't work as Ext is not present in the test code context for WebDriver tests. Instead you would need to use the execute() method to execute that code "over there" in the browser that webdriver launches.

Either that or the URL you specified in the Scenario points to a location which doesn't have Ext loaded.

vadimv
19 Apr 2017, 6:28 AM
So all the unit tests and/or the tests that need the App and Ext namespace have to be tested "In Browser" not in "WebDriver". I get for "In Browser" that the app namespace is not available:



describe("Unit testing MyApp.util.Utils", function () {
var util;


beforeAll(function (done) {
ST.documentReady().and(function () {
util = MyApp.util.Utils;
done();
})
});




it("should formatDocumentNumber equal", function () {
expect(util.formatDocumentNumber('34', 'U')).toEqual('34 U');
});


it("should formatDocumentNumber be undefined", function () {
expect(util.formatDocumentNumber()).toBe(undefined);
});
});


// Failed with error "MyApp is not defined"






What could be wrong here in the tests setup. I've seen the unit tests examples from sencha github, but they won't work either, I added ST.documentReady() assuming that we have to wait before the app code is available.

craigcomstock
19 Apr 2017, 7:19 AM
Do you have Sencha CMD integration enabled in Preferences? If so then check your scenario config and ensure "Launch application" is checked. This will launch your app and should make it available.

The alternative is to set "Launch application" to unchecked and instantiate the class directly. That might be a better option if you are unit testing your MyApp.util.Utils class in isolation. I would recommend this latter approach given the tests you have so far.

Also, best practice for jasmine is to use the "this" reference in beforeAll/etc and it().

You shouldn't need ST.documentReady() as Sencha Test takes care of waiting for Ext to be ready and generally should work.


beforeAll(function () {
this.util = new MyApp.util.Utils();
});

it('should formatDocumentNumber equal', function () {
expect(this.util.formatDocumentNumber('34','U')).toEqual('34 U');
});

vadimv
19 Apr 2017, 7:28 AM
So I unchecked the Launch and unchecked Sencha Cmd also, because yes I'm writing unit tests and better to be isolated. And I fixed the test according to your suggestion. But still I'm getting the error:



TypeError: Cannot read property 'formatDocumentNumber' of undefined at Object.(http://127.0.0.1:8700/~orion/workspace/test/unit/util/Utils.js?_dc=1492615371227:15:20) at ST.Block.call (http://127.0.0.1:8700/~orion/files/orion.js?_dc=1492615371227:993:30) at ST.Block.invoke (http://127.0.0.1:8700/~orion/files/orion.js?_dc=1492615371227:1098:16) at Object.me.wrapperFn (http://127.0.0.1:8700/~orion/files/orion.js?_dc=1492615371227:920:16) at attemptAsync (http://127.0.0.1:8700/~orion/files/jasmine/jasmine.js?_dc=1492615371227:2942:26) at QueueRunner.run (http://127.0.0.1:8700/~orion/files/jasmine/jasmine.js?_dc=1492615371227:2890:9) at QueueRunner.execute (http://127.0.0.1:8700/~orion/files/jasmine/jasmine.js?_dc=1492615371227:2878:10) at Spec.queueRunnerFactory (http://127.0.0.1:8700/~orion/files/jasmine/jasmine.js?_dc=1492615371227:715:35) at Spec.execute (http://127.0.0.1:8700/~orion/files/jasmine/jasmine.js?_dc=1492615371227:362:10) at Object.fn (http://127.0.0.1:8700/~orion/files/jasmine/jasmine.js?_dc=1492615371227:3617:37)





ReferenceError: MyApp is not defined at Object.<anonymous> (http://127.0.0.1:8700/~orion/workspace/test/unit/util/Utils.js?_dc=1492615764513:3:21) at ST.Block.call (http://127.0.0.1:8700/~orion/files/orion.js?_dc=1492615764513:993:30) at ST.Block.invoke (http://127.0.0.1:8700/~orion/files/orion.js?_dc=1492615764513:1098:16) at Object.me.wrapperFn (http://127.0.0.1:8700/~orion/files/orion.js?_dc=1492615764513:920:16) at attemptAsync (http://127.0.0.1:8700/~orion/files/jasmine/jasmine.js?_dc=1492615764513:2942:26) at QueueRunner.run (http://127.0.0.1:8700/~orion/files/jasmine/jasmine.js?_dc=1492615764513:2890:9) at QueueRunner.execute (http://127.0.0.1:8700/~orion/files/jasmine/jasmine.js?_dc=1492615764513:2878:10) at queueRunnerFactory (http://127.0.0.1:8700/~orion/files/jasmine/jasmine.js?_dc=1492615764513:715:35) at Object.fn (http://127.0.0.1:8700/~orion/files/jasmine/jasmine.js?_dc=1492615764513:3602:13) at attemptAsync (http://127.0.0.1:8700/~orion/files/jasmine/jasmine.js?_dc=1492615764513:2942:26)




56182


56181



{
"libs": [],
"coverageFilters": [],
"directory": "unit",
"name": "unit",
"profile": "classic",
"sandbox": false,
"globals": "*",
"framework": "jasmine",
"launch": false,
"page": "http://localhost/build/testing/MyApp/index.html"
},

vadimv
19 Apr 2017, 7:39 AM
I even checked back the Sencha Cmd in prefs, but getting same errors.

craigcomstock
19 Apr 2017, 7:55 AM
Maybe can you check in your workspace for a .sencha/temp/MyApp directory and delete it completely? That is a location in which we make a special "Development Build" which should pull in all your classes and make them available. What does your Utils definition look like? You might instantiate your Utils class in a different way depending on how it is defined. I would expect something like


Ext.define('MyApp.util.Utils', {
...
});

and given that you should be able to


this.util = new MyApp.util.Utils()

vadimv
19 Apr 2017, 8:24 AM
That Utils is a singleton, but even if wouldn't be, the missing namespace error wouldn't be thrown. The tests/.sencha/temp is empty, has only the .ide folder within it, nothing more.

craigcomstock
19 Apr 2017, 10:45 AM
When selecting a browser in the Scenario Runner a "Development Build" dialog should be shown. Agree to make that build and your classes should be available. What creates the singleton? If it is the app then you will likely need to select "Launch application" in your scenario.

56190

vadimv
19 Apr 2017, 11:52 AM
I don't see your attachment. I don't see any Dev Build dialog. I've noticed that after I checked Launch app checkbox, the app is loaded in browser then the tests pass, then when running again they fail because the app is not loading again, checking and unchecking again may open it again....in conclusion it's a random behavior, I can't really understand how this app should be used so every time when I run the tests they pass.

I have a gif, but this forum doesn't display it as a gif but as an image (jpg), so it's useless.

vadimv
19 Apr 2017, 11:58 AM
Your attachment:

56189

craigcomstock
19 Apr 2017, 12:27 PM
There may be some state problem with your Scenario Runner and the browser. Try closing the Running and re-opening it when problems occur.

In your workspace opened in Studio do you have your Application? I notice that the URL for your test seems to be an locally hosted app. If your workspace doesn't include your App then you will have to use "Launch application" since Studio can't make a build with no app sources.

When your test completes the web browser should be taken to a "parked" page with the text "Sencha Test Session" and "This browser window is available for work from Sencha Studio."

Normally for application unit tests the workspace opened by Studio will contain the Application code and Studio can make a special build which does not require the app to load but rather makes available all classes in the app for instantiation in tests for unit testing.

Sorry about the attachment. I'll try again.

craigcomstock
19 Apr 2017, 12:29 PM
I think I fixed up that previous attachment about a "Development Build" dialog. Take a look.

vadimv
19 Apr 2017, 12:38 PM
There may be some state problem with your Scenario Runner and the browser. Try closing the Running and re-opening it when problems occur.


Yes I've been doing this.



In your workspace opened in Studio do you have your Application? I notice that the URL for your test seems to be an locally hosted app. If your workspace doesn't include your App then you will have to use "Launch application" since Studio can't make a build with no app sources.


I don't have the app. Yes the url comes from localhost which points to testing build.




When your test completes the web browser should be taken to a "parked" page with the text "Sencha Test Session" and "This browser window is available for work from Sencha Studio."

Yep I can see that page. So here's how I found it how to run it with less problems. So I check first them the browser, I wait for the app to be loaded, once is loaded I run the tests,
they pass, then the browser page is redirected to parking. Then:

- If I hit run, the app loads for 2 seconds, the load is not finished, tests fail, and then redirect to parking
- If I uncheck and check again, then wait for app to load, and hit run, the tests pass, then redirect to parking.

That's a weird behavior of running.




Normally for application unit tests the workspace opened by Studio will contain the Application code and Studio can make a special build which does not require the app to load but rather makes available all classes in the app for instantiation in tests for unit testing.

Sorry about the attachment. I'll try again.

Ok, got it, I'm going to add the login (login from other page, it's a multi page app) scenario into unit test also, so then try to run the app as you say. Since the ST1 the login data was hardcoded into the bulding/index.html page, with migration to ST2 I haven't changed that for unit tests, just for functional.

vadimv
19 Apr 2017, 12:48 PM
Ok, I can see your attachment now, thanks.
So how to add the App code in the Studio ? Through the Files Tab ? Also what folders to add (see attachments)

Btw, I've looked through the doc guides but couldn't find anything about how to setup and run Unit Tests, unless I missed something.

56191

vadimv
25 Apr 2017, 1:45 AM
I really would need an answer to last comment. Thanks.

craigcomstock
25 Apr 2017, 7:12 AM
Ah. If you wish to test against the app source and do unit testing you should create a workspace that includes the application code. According to your directory listing the workspace (workspace.json) is at the level of the tests directory and so won't include the application source code above it. Do you already have a workspace.json file in the directory above tests? If you do open that with Open Project menu item.

There are some good getting started docs at

http://docs.sencha.com/sencha_test/2.0.2/guides/getting_started.html

which include a section on unit tests:

http://docs.sencha.com/sencha_test/2.0.2/guides/unit_functional_tests.html

Some of the docs are a bit out of date but the basic principles remain the same.

Let me know how it goes. Thanks.

vadimv
2 May 2017, 4:33 AM
Thanks man, went well.