1
votes

I am working on a database module and now I'm working on trying to get the unit tests to pass. Originally we used browserify and we were able to require(sqlite3) or require(better-sqlite3) without an issue in the tests. We have changed to webpack and webpack does not handle requiring native node modules so we had to switch to non_webpack_require to get webpack to load the database modules.

This works good except mocha has no idea what __non_webpack_require__ is and fails the tests. So we found out we have to run our unit tests through webpack in order to get this all to work.

Sounds good and I think I have it working however mocha does not recognize any of the tests now. The command I run webpack test/*.test.js -o test/testBundle.js --module-bind js=node-loader -target node && mocha test/testBundle.js --file ../../systemjs/dist/system.js --require @babel/register.

The output

   Asset      Size  Chunks             Chunk Names
testBundle.js  3.58 KiB       0  [emitted]  main
Entrypoint main = testBundle.js
[0] (webpack)/buildin/global.js 279 bytes {0} [built]
[1] (webpack)/buildin/module.js 279 bytes {0} [built]
[2] multi ./test/better-sqlite3.test.js ./test/catena-database.test.js ./test/idatabase.test.js ./test/index.test.js ./test/nedb.test.js ./test/sqlite-sync.test.js 88 bytes {0} [built]
[3] ./test/better-sqlite3.test.js 325 bytes {0} [built]
[4] ./test/catena-database.test.js 327 bytes {0} [built]
[5] ./test/idatabase.test.js 315 bytes {0} [built]
[6] ./test/index.test.js 307 bytes {0} [built]
[7] ./test/nedb.test.js 305 bytes {0} [built]
[8] ./test/sqlite-sync.test.js 319 bytes {0} [built]

WARNING in configuration
The 'mode' option has not been set, webpack will fallback to
'production' for this value. Set 'mode' option to 'development' or
'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn
more: https://webpack.js.org/concepts/mode/


0 passing (1ms

No passing tests and no record of any failed tests. How can I get mocha to see the unit tests? Or is there another way I can go about this?

EDIT

I have also tried renaming all the tests to something.a.js and then running webpack test/*.a.js -o test/testBundle.test.js --config test/webpack.config.js && mocha --file ../../systemjs/dist/system.js --require @babel/register so the bundle is the only test.js file and so mocha does the default of looking in the test folder for .test.js files. I still have the same issue.

1

1 Answers

1
votes

After installing mocha in your project you can point to mocha path in node modules with your file name as arg, Hope it helps

Example:

$../node_modules/.bin/mocha apiTest.js