Node-qunit does not include my source files in the area

I installed node-qunit (stable) from npm, but it may not seem like any tests are working. My source files do not seem to be included in scope.

./source/myscript.js:

var myObj = { a : true } 

./test/tests.js:

 test("that a is true", function () { ok(myObj.a); }); 

./test/runner.js:

 var runner = require('qunit'); runner.run({ code : './source/myscript.js', tests : './test/tests.js' }); 

./Makefile:

 test : <tab>node ./test/testrunner.js .PHONY: install test 

If I run make test , I get the error 'ReferenceError: myObj is not defined' . The source file is launched because it can cause errors. It seems that it is simply not included in the global scope. This does not work if I do it from the command line according to the instructions in node-qunit readme . Does anyone know how to do this?

+4
source share
1 answer

You are not exporting anything. Behind the scenes, node-qunit uses require to load the specified modules. To expose variables when the require d module, you must add them to the exports object (or assign your own exports object)

(There's also a syntax error - ; in object literature)

This works for me:

./source/myscript.js:

 exports.myObj = { a: true } 

./test/tests.js:

 QUnit.module('tests') test("that a is true", function () { ok(myObj.a) }) 

./test/runner.js:

 var runner = require('qunit') runner.run({ code : './source/myscript.js' , tests : './test/tests.js' }) 
+5
source

Source: https://habr.com/ru/post/1390988/


All Articles