Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #20952

Re: require.js in a phantomjs test fails for code that works fine in production

Message-ID <2367596.uRJ2TEEu9r@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-08-14 11:24 +0200
Subject Re: require.js in a phantomjs test fails for code that works fine in production
Newsgroups comp.lang.javascript
References <1e4f5146-6d0d-4161-a2ec-7c302acfbe94@googlegroups.com>

Show all headers | View raw


Do not post to Usenet via Google Groups’ Web interface, for the latter is 
FUBAR.  Some of your lines contain more than 160 characters, whereas more 
than 80 are already inappropriate.

David Karr wrote:

> I have a small webapp whose javascript code I don't normally maintain.  I
> work on the back-end of the service, but that's irrelevant to the
> question.
> 
> I'm trying to implement javascript unit tests.

There is no javascript: <http://PointedEars.de/es-matrix>

> Someone helped me put together a configuration using maven, jstestrunner
> phantomjs, and qunit, along with one trivial unit test.
> 
> This configuration works, except I had to add one line of code to the CUT
> (code under test) that the code running in production doesn't need.  It
> still works in production with the added line, but I need to understand
> why the test requires the line.
> 
> In short, one of the several .js files begins like this:
> 
> -------------------
> Data.Daily = new Function();

The best current practice to create a Function instance dynamically is

  Data.Daily = function () {};

Unless “Data” also specifies a constructor, it should be “data”.
 
> Data.Daily.prototype = {

Be aware that you reset the “constructor” property to the value of “Object” 
if you use this inheritance pattern.

> ...
> -----------------
> 
> When I run my test, I see this failure:
> -------------
>     at http://localhost:9080/js/require.js:1703: Can't find variable:
>     Data, source: ReferenceError: Can't find variable: Data
> -------------
> 
> I then added the following line before the first line:
> -------------
> var Data	= window.Data || {};

The object referred to by “window” *maybe* does not have a built-in “Data” 
property.  Do not use “window” as reference for the Global Object; in 
particular do not use the referred object as data container.  It has or 
inherits host-defined properties that you cannot know about; some of them 
may hold references to host objects.  To prevent the value of an initialized 
“data” variable from being overwritten, use

  if (typeof data == "undefined")
  {
    var data = …
  }

Those issues notwithstanding, in general your approach should work.  It does 
not even matter where in the same execution context the declaration is 
located, as (per ES 3) “variable instantiation” or (per ES 5.x) “declaration 
binding instantiation” happens before execution.  As a result, the “data” 
variable declared above will be available everywhere in that context even 
though the declaration is located in the Block statement.  [“let” in Mozilla 
JavaScript, which is planned for a future Edition of ECMAScript, (will) also 
allow for block scoping.]

> -------------
> 
> This fixes the test.  Running the app in my browser works fine both with
> or without this line.
> 
> I know I haven't provided much code here, but is there anything obvious
> here that I'm missing?

Yes.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

require.js in a phantomjs test fails for code that works fine in production David Karr <davidmichaelkarr@gmail.com> - 2013-08-13 16:29 -0700
  Re: require.js in a phantomjs test fails for code that works fine in production "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2013-08-13 16:52 -0700
    Re: require.js in a phantomjs test fails for code that works fine in production David Karr <davidmichaelkarr@gmail.com> - 2013-08-13 17:27 -0700
  Re: require.js in a phantomjs test fails for code that works fine in production Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-08-14 11:24 +0200
    Re: require.js in a phantomjs test fails for code that works fine in production John Harris <niam@jghnorth.org.uk.invalid> - 2013-08-14 14:48 +0100
      Re: require.js in a phantomjs test fails for code that works fine in production Jim T. <x@y.z> - 2013-08-14 14:32 -0400
        Re: require.js in a phantomjs test fails for code that works fine in production Tim Streater <timstreater@greenbee.net> - 2013-08-14 19:43 +0100
      Re: require.js in a phantomjs test fails for code that works fine in production Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2013-08-15 18:51 -0300
      Re: require.js in a phantomjs test fails for code that works fine in production Christoph Michael Becker <cmbecker69@arcor.de> - 2013-08-16 02:28 +0200
        Re: require.js in a phantomjs test fails for code that works fine in production Martin Leese <please@see.Web.for.e-mail.INVALID> - 2013-08-15 20:12 -0600
        Re: require.js in a phantomjs test fails for code that works fine in production John Harris <niam@jghnorth.org.uk.invalid> - 2013-08-16 11:29 +0100
          c.l.js (was: require.js in a phantomjs test fails for code that works fine in production) Christoph Michael Becker <cmbecker69@arcor.de> - 2013-08-16 23:57 +0200
            Re: c.l.js Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-08-18 10:17 +0200
              Re: c.l.js John Harris <niam@jghnorth.org.uk.invalid> - 2013-08-18 15:14 +0100
            Re: c.l.js (was: require.js in a phantomjs test fails for code that works fine in production) John Harris <niam@jghnorth.org.uk.invalid> - 2013-08-18 15:51 +0100
              Re: c.l.js Christoph Michael Becker <cmbecker69@arcor.de> - 2013-08-18 17:14 +0200
                Re: c.l.js Tim Streater <timstreater@greenbee.net> - 2013-08-18 16:44 +0100
                Re: c.l.js Christoph Michael Becker <cmbecker69@arcor.de> - 2013-08-18 17:52 +0200
                Re: c.l.js Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-08-18 19:28 +0200
                Re: c.l.js John Harris <niam@jghnorth.org.uk.invalid> - 2013-08-19 15:42 +0100
                Re: c.l.js Andrew Poulos <ap_prog@hotmail.com> - 2013-08-20 06:58 +1000
                Re: c.l.js John Harris <niam@jghnorth.org.uk.invalid> - 2013-08-20 16:10 +0100
                Re: c.l.js Andrew Poulos <ap_prog@hotmail.com> - 2013-08-21 07:55 +1000
                [OT] How to find messages by ID (was: c.l.js) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-08-21 06:47 +0200
                Re: [OT] How to find messages by ID (was: c.l.js) John Harris <niam@jghnorth.org.uk.invalid> - 2013-08-21 15:50 +0100
                Re: [OT] How to find messages by ID (was: c.l.js) Christoph Michael Becker <cmbecker69@arcor.de> - 2013-08-21 23:13 +0200
                Re: [OT] How to find messages by ID (was: c.l.js) John Harris <niam@jghnorth.org.uk.invalid> - 2013-08-22 11:05 +0100
                Quoting in c.l.js (was: [OT] How to find messages by ID) Christoph Michael Becker <cmbecker69@arcor.de> - 2013-08-22 15:06 +0200
                Re: Quoting in c.l.js (was: [OT] How to find messages by ID) John Harris <niam@jghnorth.org.uk.invalid> - 2013-08-24 10:05 +0100
                Re: Quoting in c.l.js Christoph Michael Becker <cmbecker69@arcor.de> - 2013-09-01 22:32 +0200
                Re: Quoting in c.l.js Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-09-02 00:08 +0200
                Re: Quoting in c.l.js John Harris <niam@jghnorth.org.uk.invalid> - 2013-09-02 15:52 +0100
                Re: c.l.js Christoph Michael Becker <cmbecker69@arcor.de> - 2013-08-20 01:06 +0200
                Re: c.l.js John Harris <niam@jghnorth.org.uk.invalid> - 2013-08-20 16:14 +0100
                Re: c.l.js John Harris <niam@jghnorth.org.uk.invalid> - 2013-08-19 16:07 +0100
                Re: c.l.js Andrew Poulos <ap_prog@hotmail.com> - 2013-08-20 07:01 +1000
                Re: c.l.js John Harris <niam@jghnorth.org.uk.invalid> - 2013-08-20 16:20 +0100
                Re: c.l.js Andrew Poulos <ap_prog@hotmail.com> - 2013-08-21 08:02 +1000
                Re: c.l.js Tim Streater <timstreater@greenbee.net> - 2013-08-20 23:29 +0100
                Re: c.l.js Andrew Poulos <ap_prog@hotmail.com> - 2013-08-21 14:59 +1000
                Re: c.l.js John Harris <niam@jghnorth.org.uk.invalid> - 2013-08-21 16:05 +0100
                Re: c.l.js Andrew Poulos <ap_prog@hotmail.com> - 2013-08-22 07:43 +1000
        [FAQ] “javascript” (was: require.js in a phantomjs test fails for code that works fine in production) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-08-18 11:36 +0200
  Re: require.js in a phantomjs test fails for code that works fine in production Carlos Pedro <carlospedr@gmail.com> - 2013-08-15 04:28 -0700
  Re: require.js in a phantomjs test fails for code that works fine in production richarduie@yahoo.com - 2013-08-18 08:21 -0700

csiph-web