Main /
MochaWe intend to use Mocha for testing Node.js and JavaScript. How to run mocha tests with the Test accessor:The Test accessor can be used to read a mocha test file and execute the tests. Browser host
Node host
node ../../nodeHostShell.js < ./testCommon.js How to run mocha and test the Node.js facilities in Ptolemy II:If you don't have Node.js, then see https://nodejs.org/en/download/ After Node.js is installed: cd $PTII svn update ./configure sudo npm install -g mocha sudo npm install -g mocha-junit-reporter ant test.mocha How to run mocha and test in the browser:Please see the example in the accessors repo at To run this example:
For more information, please see the mocha website, https://mochajs.org/#running-mocha-in-the-browser How to write mocha testsAn example test is in The code uses the var assert = require('assert'); Mocha FunctionsBelow are the functions common mocha functions:
Use assert.equal(a.latestOutput('output'), 50); Add-ons
Resources
Looks like Mocha is a place to start for Node.js JavaScript testing frameworks. Accessors Test Bed DesignCurrently To run the script cd accessors/web/hosts/node/test/ node ../nodeHost.js < testNodeHost.js Features we need:
"this" rears its ugly head.
// This is just a simple smoke test for the Node.js host. // To use it, run the node host and copy and paste the following into it. var a = this.instantiate('TestComposite', 'test/TestComposite'); a.initialize(); a.provideInput('input', 10); a.react(); a.latestOutput('output'); // Should return 50 a.wrapup(); quit; However, if we try to run Mocha on a similar file that uses
Then we get: bash-3.2$ mocha testNodeHost.js Welcome to the Node swarmlet host (nsh). Type exit to exit, help for help. nsh> AccessorTests test/TestComposite() 1) Create a test composite and run it 0 passing (6ms) 1 failing 1) AccessorTests test/TestComposite() Create a test composite and run it: TypeError: undefined is not a function at Context.<anonymous> (testNodeHost.js:12:26) bash-3.2$ Line 12 is: var a = this.instantiate('TestComposite', 'test/TestComposite'); If we change it to: var a = instantiate('TestComposite', 'test/TestComposite'); Then the test passes bash-3.2$ mocha testNodeHost.js Welcome to the Node swarmlet host (nsh). Type exit to exit, help for help. nsh> AccessorTests test/TestComposite() Reading accessor at: /Users/cxh/ptII/org/terraswarm/accessor/accessors/web/test/TestComposite.js Reading accessor at: /Users/cxh/ptII/org/terraswarm/accessor/accessors/web/test/TestGain.js Reading accessor at: /Users/cxh/ptII/org/terraswarm/accessor/accessors/web/test/TestAdder.js Instantiated accessor TestComposite with class test/TestComposite ✓ Create a test composite and run it 1 passing (11ms) bash-3.2$ The question is: Should the function in Mocha have Indeed, the answer is that the script should not have (However, we should probably not have instantiate as a global so that we can use accessors with other modules) JUnit reporting for MochaWe want to include the results of running the Mocha tests in the nightly build. https://www.npmjs.com/package/mocha-junit-reporter seems useful
The output looks like: <?xml version="1.0" encoding="UTF-8"?> <testsuites name="Mocha Tests" time="0.004" tests="1" failures="0"> <testsuite name="Root Suite" timestamp="2016-02-11T02:24:44" tests="0" failures="0" time="0"> </testsuite> <testsuite name="AccessorTests" timestamp="2016-02-11T02:24:44" tests="0" failures="0" time="0"> </testsuite> <testsuite name="test/TestComposite()" timestamp="2016-02-11T02:24:44" tests="1" failures="0" time="0.004"> <testcase name="Root Suite AccessorTests test/TestComposite() Create a test composite and run it" time="0.004" classname="Create a test composite and run it"> </testcase> </testsuite> </testsuites> The solution is that our top level ant script looks like: <target name="test.mocha.xml" description="Use mocha to test node.js and generate JUnit compatible xml output."> <echo> ==test.mocha.xml== This target uses mocha to test Node.js tests in **/mocha/test*.js files. This target requires setup: sudo npm install -g mocha sudo npm install -g mocha-junit-reporter See https://chess.eecs.berkeley.edu/ptexternal/wiki/Main/JSMocha The output will appear in reports/junit/mochaJUnit.xml </echo> <fileset id="test.mocha.files" dir="${basedir}"> <include name="**/mocha/test*.js"/> </fileset> <pathconvert refid="test.mocha.files" property="converted"/> <exec executable="mocha"> <arg value="--reporter"/> <arg value="mocha-junit-reporter"/> <arg value="--reporter-options"/> <arg value="mochaFile=reports/junit/mochaJUnit.xml"/> <arg line="${converted}"/> </exec> </target> The Ptolemy II Continuous Integration build runs the mocha tests by invoking Mocha/Accessors issuesRelative paths in testsIf a test uses relative paths with The mocha tests are in
TypeError: Converting circular structure to JSONbash-4.1$ mocha hosts/common/test/mocha/testCommon.js CommonTests ^[[2K^[[0G 1) Load and run the accessor common host tests 0 passing (12ms) 1 failing 1) CommonTests Load and run the accessor common host tests: TypeError: Converting circular structure to JSON at Object.stringify (native) at util.js:41:30 at String.replace (native) at Console.exports.format (util.js:35:23) at Console.log (console.js:53:34) at Object.<anonymous> (hosts/common/test/testCommon.js:63:9) at Context.<anonymous> (hosts/common/test/mocha/testCommon.js:10:26) bash-4.1$ The issue here is that we are running under RHEL 6 and bash-4.1$ node --version v0.10.36 bash-4.1$ The fix is to install a more recent version of NOde Mocha Output is truncated in AntWhen running bash-3.2$ ant tests.mocha Buildfile: /Users/cxh/ptII/org/terraswarm/accessor/accessors/web/build.xml -check-mocha: -check-chai: chai-install: chai-update: mocha-install: mocha-update: -check-terraswarm-gdp: terraswarm-gdp-install: terraswarm-gdp-update: tests.mocha: [echo] [echo] ==tests.mocha== [echo] This target uses mocha to test Node.js tests in **/mocha/test*.js files. [echo] To run just the composites, use "ant tests.mocha.composites" [echo] If necessary, istanbul and mocha are installed in node_modules/ [echo] See https://chess.eecs.berkeley.edu/ptexternal/wiki/Main/JSMocha [echo] target timeout = 1200000 ms. [echo] [echo] tests.mocha: running on /Users/cxh/ptII/org/terraswarm/accessor/accessors/web/hosts/common/test/moc\ ha/testCommon.js /Users/cxh/ptII/org/terraswarm/accessor/accessors/web/hosts/node/test/mocha/testCommon.js /User\ s/cxh/ptII/org/terraswarm/accessor/accessors/web/hosts/node/test/mocha/testMain.js /Users/cxh/ptII/org/terraswar\ m/accessor/accessors/web/hosts/node/test/mocha/testNodeAllAuto.js /Users/cxh/ptII/org/terraswarm/accessor/access\ ors/web/hosts/node/test/mocha/testNodeHost.js /Users/cxh/ptII/org/terraswarm/accessor/accessors/web/hosts/node/t\ est/mocha/testNodeHostInvoke.js /Users/cxh/ptII/org/terraswarm/accessor/accessors/web/net/test/auto/mocha/testRE\ ST.js [exec] Usage: [-help] [-echo] [-js filename] [-timeout milliseconds] [-version] [accessorClassName] [access\ orClassName ...]testNodeAllAuto.js: Skipping gdp/test/auto [exec] [exec] Argument 0 was -timeout but there is no following milliseconds argument. Args were: -timeouttestNo\ deAllAuto.js: Skipping hosts/browser/test/auto [exec] [exec] testNodeAuto.js: testNodeAuto(./net/test/auto) [exec] testNodeAuto.js: testNodeAuto(./test/auto) [exec] [exec] [exec] hosts/browser/common/test/mocha/testCommon.js or hosts/common/test/mocha/testCommon.js (symlink): \ CommonTests [exec] 1) Load and run the accessor common host tests [exec] [exec] hosts/node/test/mocha/testCommon.js: testCommon. [exec] To replicate: (cd hosts/node/test; ../../../node_modules/.bin/mocha testCommon.js) [exec] 2) load testCommon [exec] [exec] hosts/node/test/mocha/testMain.js: nodeHost.js processCommandLineArguments(). [exec] To replicate: (cd hosts/node/test; ../../../node_modules/.bin/mocha testMain.js) [exec] 3) nodeHost.processCommandLineArguments(). Should have generated a usage message. [exec] Usage: [-help] [-echo] [-js filename] [-timeout milliseconds] [-version] [accessorClassName] [access\ orClassName ...] [exec] ✓ nodeHost.processCommandLineArguments(-h). Should have generated a usage message. [exec] 4) nodeHost.processCommandLineArguments(-timeout) with no timeout. Should have generated an erro\ r message. [exec] Accessors 1.0, commonHost.js: $Id: commonHost.js 1225 2016-12-31 00:11:10Z eal $ [exec] ✓ nodeHost.processCommandLineArguments(-v). Should have generated a version message. [exec] commonHost.js: processCommandLineArguments(): Setting timout to stop after 5500 ms. [exec] Instantiated accessor RampJSTest with class test/auto/RampJSTest [exec] 5) nodeHost.processCommandLineArguments(test/auto/RampJSTest) [exec] commonHost.js: processCommandLineArguments(): Setting timout to stop after 5500 ms. [exec] Instantiated accessor RampJSTestDisplay with class test/auto/RampJSTestDisplay [exec] 6) nodeHost.processCommandLineArguments(test/auto/RampJSTestDisplay) [exec] [exec] NodeHost [exec] 7) run accessors/web/./net/test/auto/TestRESTGet.js [exec] . To replicate: (cd hosts/node; node nodeHostInvoke ./net/test/auto/TestRESTGet.js) [exec] [exec] 8) run accessors/web/./net/test/auto/TestRESTPost.js [exec] . To replicate: (cd hosts/node; node nodeHostInvoke ./net/test/auto/TestRESTPost.js) [exec] [exec] 9) run accessors/web/./net/test/auto/TestRESTPut.js [exec] . To replicate: (cd hosts/node; node nodeHostInvoke ./net/test/auto/TestRESTPut.js) [exec] [exec] 10) run accessors/web/./net/test/auto/TestUdpSocketSelf.js [exec] . To replicate: (cd hosts/node; node nodeHostInvoke ./net/test/auto/TestUdpSocketSelf.js) [exec] [exec] 11) run accessors/web/./net/test/auto/WebSocketClient2JS.js [exec] . To replicate: (cd hosts/node; node nodeHostInvoke ./net/test/auto/WebSocketClient2JS.js) [exec] [exec] 12) run accessors/web/./net/test/auto/WebSocketClient3JS.js [exec] . To replicate: (cd hosts/node; node nodeHostInvoke ./net/test/auto/WebSocketClient3JS.js) [exec] [exec] 13) run accessors/web/./net/test/auto/WebSocketClientJS.js [exec] . To replicate: (cd hosts/node; node nodeHostInvoke ./net/test/auto/WebSocketClientJS.js) [exec] [exec] 14) run accessors/web/./net/test/auto/WebSocketSendReceive.js [exec] . To replicate: (cd hosts/node; node nodeHostInvoke ./net/test/auto/WebSocketSendReceive.js) [exec] [exec] [exec] NodeHost [exec] 15) run accessors/web/./test/auto/RampJSDisplay.js [exec] . To replicate: (cd hosts/node; node nodeHostInvoke ./test/auto/RampJSDisplay.js) [exec] [exec] 16) run accessors/web/./test/auto/RampJSTest.js [exec] . To replicate: (cd hosts/node; node nodeHostInvoke ./test/auto/RampJSTest.js) [exec] [exec] 17) run accessors/web/./test/auto/RampJSTestDisplay.js [exec] . To replicate: (cd hosts/node; node nodeHostInvoke ./test/auto/RampJSTestDisplay.js) [exec] [exec] 18) run accessors/web/./test/auto/Stop.js [exec] . To replicate: (cd hosts/node; node nodeHostInvoke ./test/auto/Stop.js) [exec] [exec] 19) run accessors/web/./test/auto/TestComposite.js [exec] . To replicate: (cd hosts/node; node nodeHostInvoke ./test/auto/TestComposite.js) [exec] [exec] [exec] hosts/node/test/mocha/testNodeHost.js: testComposite [exec] test/TestComposite() [exec] Instantiated accessor TestComposite with class test/TestComposite [exec] TestGain: inputHandler: input: 10 gain: 4 [exec] TestAdder: fire(): inputLeft: 10 inputRight: 40 [exec] ✓ Create a test composite and run it [exec] [exec] hosts/node/test/mocha/testNodeHostInvoke.js: nodeHost instantiateAndInitialize() [exec] 20) instantiateAndInitialize(["test/TestComposite"]) [exec] [exec] net/test/auto/mocha/testREST.js. [exec] To replicate: (cd net/test/auto/mocha; ../../../../node_modules/.bin/mocha testREST.js) [exec] Instantiated accessor REST with class net/REST [exec] 21) Should GET values from a Cross-Origin Resource Sharing (CORS) site [exec] 22) Should GET values using the JSON with padding technique [exec] [exec] [exec] 3 passing (4s) [exec] 22 failing [exec] [exec] 1) hosts/browser/common/test/mocha/testCommon.js or hosts/common/test/mocha/testCommon.js (symlink\ ): CommonTests Load and run the accessor common host tests: [exec] [exec] AssertionError: Temporarily failing while we figure out commonHost. [exec] + expected - actual [exec] [exec] -true [exec] +false [exec] [exec] at Context.<anonymous> (hosts/common/test/mocha/testCommon.js:12:9) [exec] [exec] 2) hosts/node/test/mocha/testCommon.js: testCommon. [exec] To replicate: (cd hosts/node/test; ../../../node_modules/.bin/mocha testCommon.js) load testComm\ on: [exec] [exec] AssertionError: Temporarily failing while we figure out commonHost. [exec] + expected - actual [exec] [exec] -true [exec] +false [exec] [exec] at Context.<anonymous> (hosts/node/test/mocha/testCommon.js:12:9) [exec] [exec] 3) hosts/node/test/mocha/testMain.js: nodeHost.js processCommandLineArguments(). [exec] To replicate: (cd hosts/node/test; ../../../node_modules/.bin/mocha testMain.js) nodeHost.processComman\ dLineArguments(). Should have generated a usage message.: [exec] AssertionError: false == 3 [exec] at Context.<anonymous> (hosts/node/test/mocha/testMain.js:14:16) [exec] [exec] 4) hosts/node/test/mocha/testMain.js: nodeHost.js processCommandLineArguments(). [exec] To replicate: (cd hosts/node/test; ../../../node_modules/.bin/mocha testMain.js) nodeHost.processComman\ dLineArguments(-timeout) with no timeout. Should have generated an error message.: [exec] AssertionError: false == 3 [exec] at Context.<anonymous> (hosts/node/test/mocha/testMain.js:25:16) [exec] [exec] 5) hosts/node/test/mocha/testMain.js: nodeHost.js processCommandLineArguments(). [exec] To replicate: (cd hosts/node/test; ../../../node_modules/.bin/mocha testMain.js) nodeHost.processComman\ dLineArguments(test/auto/RampJSTest): [exec] AssertionError: true == 0 [exec] at Context.<anonymous> (hosts/node/test/mocha/testMain.js:102:20) [exec] [exec] 6) hosts/node/test/mocha/testMain.js: nodeHost.js processCommandLineArguments(). [exec] To replicate: (cd hosts/node/test; ../../../node_modules/.bin/mocha testMain.js) nodeHost.processComman\ dLineArguments(test/auto/RampJSTestDisplay): [exec] AssertionError: true == 0 [exec] at Context.<anonymous> (hosts/node/test/mocha/testMain.js:102:20) [exec] [exec] 7) NodeHost run accessors/web/./net/test/auto/TestRESTGet.js [exec] . To replicate: (cd hosts/node; node nodeHostInvoke ./net/test/auto/TestRESTGet.js) [exec] : [exec] ReferenceError: instantiateAndInitialize is not defined [exec] at Context.<anonymous> (hosts/node/test/testNodeAuto.js:62:95) [exec] [exec] 8) NodeHost run accessors/web/./net/test/auto/TestRESTPost.js [exec] . To replicate: (cd hosts/node; node nodeHostInvoke ./net/test/auto/TestRESTPost.js) [exec] : [exec] ReferenceError: instantiateAndInitialize is not defined [exec] at Context.<anonymous> (hosts/node/test/testNodeAuto.js:62:95) [exec] [exec] 9) NodeHost run accessors/web/./net/test/auto/TestRESTPut.js [exec] . To replicate: (cd hosts/node; node nodeHostInvoke ./net/test/auto/TestRESTPut.js) [exec] : [exec] ReferenceError: instantiateAndInitialize is not defined [exec] at Context.<anonymous> (hosts/node/test/testNodeAuto.js:62:95) [exec] [exec] 10) NodeHost run accessors/web/./net/test/auto/TestUdpSocketSelf.js [exec] . To replicate: (cd hosts/node; node nodeHostInvoke ./net/test/auto/TestUdpSocketSelf.js) [exec] : [exec] ReferenceError: instantiateAndInitialize is not defined [exec] at Context.<anonymous> (hosts/node/test/testNodeAuto.js:62:95) [exec] [exec] 11) NodeHost run accessors/web/./net/test/auto/WebSocketClient2JS.js [exec] . To replicate [exec] Result: 22 The contents of build.xml: <target name="tests.mocha" description="Use mocha to test the Node host and generate output on stdout." depends="mocha-update, terraswarm-gdp-update"> <echo> ==tests.mocha== This target uses mocha to test Node.js tests in **/mocha/test*.js files. To run just the composites, use "ant tests.mocha.composites" If necessary, istanbul and mocha are installed in node_modules/ See https://chess.eecs.berkeley.edu/ptexternal/wiki/Main/JSMocha target timeout = ${timeout.longer} ms. </echo> <pathconvert refid="test.mocha.files" pathsep=" " property="converted"/> <echo> tests.mocha: running on ${converted}</echo> <exec executable="${mocha.command}"> <arg line="${converted}"/> </exec> </target> The mocha command that is run is node_modules/.bin/mocha /Users/cxh/ptII/org/terraswarm/accessor/accessors/web/hosts/common/test/mocha/testCommon.js /Us\ ers/cxh/ptII/org/terraswarm/accessor/accessors/web/hosts/node/test/mocha/testCommon.js /Users/cxh/ptII/org/terraswarm/a\ ccessor/accessors/web/hosts/node/test/mocha/testMain.js /Users/cxh/ptII/org/terraswarm/accessor/accessors/web/hosts/nod\ e/test/mocha/testNodeAllAuto.js /Users/cxh/ptII/org/terraswarm/accessor/accessors/web/hosts/node/test/mocha/testNodeHos\ t.js /Users/cxh/ptII/org/terraswarm/accessor/accessors/web/hosts/node/test/mocha/testNodeHostInvoke.js /Users/cxh/ptII/\ org/terraswarm/accessor/accessors/web/net/test/auto/mocha/testREST.js If put that in a file called bash-3.2$ cat doit #!/bin/sh node_modules/.bin/mocha /Users/cxh/ptII/org/terraswarm/accessor/accessors/web/hosts/common/test/mocha/testCommon.js /Us\ ers/cxh/ptII/org/terraswarm/accessor/accessors/web/hosts/node/test/mocha/testCommon.js /Users/cxh/ptII/org/terraswarm/a\ ccessor/accessors/web/hosts/node/test/mocha/testMain.js /Users/cxh/ptII/org/terraswarm/accessor/accessors/web/hosts/nod\ e/test/mocha/testNodeAllAuto.js /Users/cxh/ptII/org/terraswarm/accessor/accessors/web/hosts/node/test/mocha/testNodeHos\ t.js /Users/cxh/ptII/org/terraswarm/accessor/accessors/web/hosts/node/test/mocha/testNodeHostInvoke.js /Users/cxh/ptII/\ org/terraswarm/accessor/accessors/web/net/test/auto/mocha/testREST.js bash-3.2$ cat mybuild.xml <?xml version="1.0" encoding="UTF-8" standalone="no"?> <project> <target name="doit"> <exec executable="./doit"/> </target> </project> bash-3.2$ ant -f mybuild.xml doit Buildfile: /Users/cxh/ptII/org/terraswarm/accessor/accessors/web/mybuild.xml doit: ... One thing is that mocha is returning a non-zero return code. See Issue 333: Output gets cut off if piped into another application It looks like the problem is that I was running inside an emacs shell. I created doit2, which just echos the what running mocha does: bash-3.2$ ./doit > doit.out Usage: [-help] [-echo] [-js filename] [-timeout milliseconds] [-version] [accessorClassName] [accessorClassName ...] Argument 0 was -timeout but there is no following milliseconds argument. Args were: -timeout bash-3.2$ chmod a+x doit2 #!/bin/sh echo "Usage: [-help] [-echo] [-js filename] [-timeout milliseconds] [-version] [accessorClassName] [accessorClassName ...]" > 2 echo "Argument 0 was -timeout but there is no following milliseconds argument. Args were: -timeout" > 2 cat doit.out exit 22 bash-3.2$ Here's a run bash-3.2$ ./doit2 testNodeAllAuto.js: Skipping gdp/test/auto testNodeAllAuto.js: Skipping hosts/browser/test/auto testNodeAuto.js: testNodeAuto(./net/test/auto) testNodeAuto.js: testNodeAuto(./test/auto) ... bash-3.2$ cat mybuild.xml <?xml version="1.0" encoding="UTF-8" standalone="no"?> <project> <target name="doit"> <exec executable="./doit"/> </target> <target name="doit2"> <exec executable="./doit2"/> </target> </project> In an emacs shell, running the target has the same truncated output bash-3.2$ ant -f mybuild.xml doit2 ... [exec] hosts/node/test/mocha/testNodeHost.js: testComposite [exec] test/TestComposite() [exec] Instantiated accessor TestComposite with class test/TestComposite [exec] TestGain: inputHandler: input: 10 gain: 4 [exec] TestAdder: fire(): inputLeft: 10 inputRight: 40 [exec] ✓ Create a test composite and run it [exec] [exec] hosts/node/test/mocha/testNodeHostInvoke.js: nodeHost instantiateAndInitialize() [exec] 20) instantiateAndInitialize(["test/TestComposite"]) [exec] [exec] net/test/auto/mocha/testREST.js. [exec] To replicate: (cd net/test/auto/mocha; ../../../../node_modules/.bin/mocha testREST.js) [exec] Instantiated accessor REST with class net/REST [exec] 2 [exec] Result: 22 BUILD SUCCESSFUL Total time: 0 seconds bash-3.2$ However, in a regular MacOS terminal, it ends properly: [exec] 22) net/test/auto/mocha/testREST.js. [exec] To replicate: (cd net/test/auto/mocha; ../../../../node_modules/.bin/mocha testREST.js) Should GET values using the JSON with padding technique: [exec] AssertionError: expected '/**/ typeof === \'function\' && ({\n "userId": 1,\n "id": 1,\n "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",\n "body": "quia et suscipit\\nsuscipit recusandae consequuntur expedita et cum\\nreprehenderit molestiae ut ut quas totam\\nnostrum rerum est autem sunt rem eveniet architecto"\n});' to deeply equal { Object (userId, id, ...) } [exec] at Assertion.assertEqual (node_modules/chai/lib/chai/core/assertions.js:485:19) [exec] at Assertion.ctx.(anonymous function) [as equal] (node_modules/chai/lib/chai/utils/addMethod.js:41:25) [exec] at Timeout._onTimeout (net/test/auto/mocha/testREST.js:86:42) [exec] [exec] [exec] [exec] Result: 22 BUILD SUCCESSFUL Total time: 0 seconds ealmac23:web cxh$ Solution: Ant output gets truncated in an emacs shell. The workaround is to run This is true when running
ealmac23:web cxh$ emacs --version GNU Emacs 25.1.1 Copyright (C) 2016 Free Software Foundation, Inc. GNU Emacs comes with ABSOLUTELY NO WARRANTY. You may redistribute copies of GNU Emacs under the terms of the GNU General Public License. For more information about these matters, see the file named COPYING. Using emacs from Macports. Running Searching the Ant bugs for emacs did not show much. Searching the Ant bugs for truncated
See AlsoBack to JS |