catamphetamine / libphonenumber-js

A simpler (and smaller) rewrite of Google Android's libphonenumber library in javascript
https://catamphetamine.gitlab.io/libphonenumber-js/
MIT License
2.79k stars 216 forks source link

Creating and testing own module using this module as a dependency #319

Closed apple-corps closed 5 years ago

apple-corps commented 5 years ago

Hi,

I'm new to Javascript but I'd like to make a module to be used in the browser, which uses this as a dependency. I have tried to make a simple project which uses similar tools to build and execute tests. However I'm getting import errors when trying to run my tests. Can you suggest what I need to fix in order to be able to fix my module which is dependent upon yours?

https://github.com/drocsid/validator_example

Thanks for the project! Hopefully I can put it to use.

catamphetamine commented 5 years ago

Hello. Provide more specifics.

apple-corps commented 5 years ago

Here's an error when I try to test

npm test

> phonenumvalidator@0.1.0 test /Users/drocsid/code/site/phonenumvalidator
> mocha --require @babel/register

/Users/drocsid/code/site/phonenumvalidator/test/units.js:3
import { validation } from '../src/validator.js';
^^^^^^

SyntaxError: Unexpected token import
    at new Script (vm.js:51:7)
    at createScript (vm.js:136:10)
    at Object.runInThisContext (vm.js:197:10)
    at Module._compile (internal/modules/cjs/loader.js:618:28)
    at Module._compile (/Users/drocsid/code/site/phonenumvalidator/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:665:10)
    at Object.newLoader [as .js] (/Users/drocsid/code/site/phonenumvalidator/node_modules/pirates/lib/index.js:104:7)
    at Module.load (internal/modules/cjs/loader.js:566:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:506:12)
    at Function.Module._load (internal/modules/cjs/loader.js:498:3)
    at Module.require (internal/modules/cjs/loader.js:598:17)
    at require (internal/modules/cjs/helpers.js:11:18)
    at /Users/drocsid/code/site/phonenumvalidator/node_modules/mocha/lib/mocha.js:330:36
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (/Users/drocsid/code/site/phonenumvalidator/node_modules/mocha/lib/mocha.js:327:14)
    at Mocha.run (/Users/drocsid/code/site/phonenumvalidator/node_modules/mocha/lib/mocha.js:804:10)
    at Object.exports.singleRun (/Users/drocsid/code/site/phonenumvalidator/node_modules/mocha/lib/cli/run-helpers.js:207:16)
    at exports.runMocha (/Users/drocsid/code/site/phonenumvalidator/node_modules/mocha/lib/cli/run-helpers.js:300:13)
    at Object.exports.handler.argv [as handler] (/Users/drocsid/code/site/phonenumvalidator/node_modules/mocha/lib/cli/run.js:296:3)
    at Object.runCommand (/Users/drocsid/code/site/phonenumvalidator/node_modules/yargs/lib/command.js:242:26)
    at Object.parseArgs [as _parseArgs] (/Users/drocsid/code/site/phonenumvalidator/node_modules/yargs/yargs.js:1104:24)
    at Object.parse (/Users/drocsid/code/site/phonenumvalidator/node_modules/yargs/yargs.js:566:25)
    at Object.exports.main (/Users/drocsid/code/site/phonenumvalidator/node_modules/mocha/lib/cli/cli.js:63:6)
    at Object.<anonymous> (/Users/drocsid/code/site/phonenumvalidator/node_modules/mocha/bin/_mocha:10:23)
    at Module._compile (internal/modules/cjs/loader.js:654:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10)
    at Module.load (internal/modules/cjs/loader.js:566:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:506:12)
    at Function.Module._load (internal/modules/cjs/loader.js:498:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:695:10)
    at startup (internal/bootstrap/node.js:201:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:516:3)
npm ERR! Test failed.  See above for more details.
catamphetamine commented 5 years ago

Well, this error means that the tests you're running don't support import because it's not a native Node.js syntax. Native Node.js syntax is require() and you could use that. But the better way is to use Babel for real-time transform. Go to this page: https://babeljs.io/setup Find "Mocha" button and click it and it will print the instructions. Mocha is a library for writing tests.

apple-corps commented 5 years ago

Hi @catamphetamine if you look at the project source you will see I tried adding Babel / Mocha as you listed above: https://github.com/drocsid/validator_example/blob/master/package.json

But I'm still getting the errors.

catamphetamine commented 5 years ago

Your babelrc filename is incorrect

apple-corps commented 5 years ago

Yes. It runs the test now. Not sure why it's not passing but I can probably get that part. Thank you and sorry I couldn't figure that out...

catamphetamine commented 5 years ago

It's ok