AVA test setup: "Unexpected token export" - node.js

Problem
I'm trying to add AVA tests to a project and my tests are not parsing ES2015 modules correctly with my Babel setup. What is odd to me is the imports are working fine and if I run my npm run build and run the output from the REPL it works...so it seems that AVA is not transpiling export statements correctly or something is wrong with my config in my package.json? I've tried reading through the docs and looking at other examples without success.
Steps to reproduce
Clone https://github.com/trevordmiller/utility-functions
checkout the tests branch
npm install
npm test
Get an error (screenshot below) - ES2015 module export error on line 7 of getURLSlug:

Add "require": ["babel-register"] to the AVA configuration in your package.json

Related

Jest not recognizing Yarn workspace module in CircleCI

My tests run fine locally, but in CircleCI I get this error “Cannot find module ‘shared-data-model’” which is one of my Yarn workspaces.
The stack trace of the error points to Resolver.resolveModule (../node_modules/#jest/core/node_modules/jest-resolve/build/index.js:276:11)
I am running
Jest 25.1.0,
Yarn 1.22,
Node.js 12.16
Any ideas what could be causing this? Thank you
I figured out the problem. I am using typescript CLI tsc to transpile my module from /src to /dist, and I wasn't running the transpile step on CI to generate the /dist folder, so when it tried to find the entrypoint of the module dist/index.js there was nothing there. Once I ran the build tsc step in CI it worked as expected.

Running mocha on various packages with lerna

I have just started with a mono repo with multiple packages using lerna.
On all my packages I will have unit tests using mocha. But as it stands, only one of my packages is ready for unit tests.
From my understanding, shouldn't I be able to only install mocha on the root package.json and then run lerna run test in order to run it only in the packages that have available test scripts in their num packages, correct?
Unfortunately as it stands this is not working.
I have mocha installed as a devDependency in the root. However, if I do run lerna run test --scope=myPackage I get the following message:
mocha: command not found
Because it is in fact installed in the root, shouldn't it work for all packages? If it isn't the case, what am I missing here? DO I have to install mocha in all of them after all?
Just remove the test script from package.json wherever the unit tests are not ready and lerna wont call it.

test input of jest test is not converting on to the json file

jest --json --outputFile=tests.json is not producing tests.json file and test runs fine so no problem at that front. I have 21.2.1 version of the jest.
I am getting this in terminal:
Please run 'npm install' to use the version of Jest intended for this project.
Accroding to Jest changelog --json --outputFile command is supported from version 18.0.0 so I don't think version is problem.

Testing against multiple node module versions

I'm developing several node modules which extend the functionality of the Sequelize database ORM module. I'm using Travis to run tests before pushing new versions of my modules to NPM.
Travis allows you to run tests against different versions of node (v0.10, v0.12 etc). Similarly, I'd like to be able to run my tests using various different versions of the Sequelize module. i.e. run the tests once using Sequelize 2.0.0, again using 2.0.1, 2.0.2 etc.
Travis loads modules from NPM automatically according the the versions set in the package.json file, and so only runs tests using the latest module version available according to what's specified in the package.json of my module (in this case ^2.0.0).
Is there any way to interfere with this process and get Travis to run the tests multiple times using a different Sequelize version each time?
I've searched for advice on this on Stack Overflow and elsewhere but not had any luck. But I assume I can't be the only person who wants to do this...
In the end the solution I came up with is to put the following in the travis.yml file:
before_script:
- 'if [ "$SEQ_VERSION" ]; then npm install sequelize#^$SEQ_VERSION.0.0; fi'
env:
- SEQ_VERSION=2
- SEQ_VERSION=3
(with thanks to Sandro Munda for his answer which put me on the right track)
In your package.json, you could have some something like this:
...
"scripts": {
"test-sequelize-2.0.0": "npm install sequelize#2.0.0 && ./node_modules/mocha/bin/mocha",
"test-sequelize-2.1.0": "npm install sequelize#2.1.0 && ./node_modules/mocha/bin/mocha",
"test": "npm run test-sequelize-2.0.0 && npm run test-sequelize-2.1.0"
}
...
Then, run npm test.
test-all-versions is a package that will run a command (you want your test script) for a whole range of semantic versions. so if you want to run your tests against sequelize you might write a config file like this.
#.tav.yml
name: sequelize
versions: ^2.0.0
command: npm run test
This will run your npm test script against all published versions of sequelize that match ^2.0.0 (ie 2.0.0, 2.0.1, etc)
Then in your travis config run the tav command to test against all versions in the given range.

How do you install and run Mocha, the Node.js testing module? Getting "mocha: command not found" after install

I'm having trouble getting Mocha to work as expected, and I'd love to say as documented, but there (appears) to not be much documentation on actually getting the thing running.
I've installed it using npm (both globally and locally), and each time when I run it I get:
$ mocha
mocha: command not found
Ok, so I figured it's not in my PATH, so I tried running it directly,
$ ./node_modules/mocha/bin/mocha
execvp(): No such file or directory
Finally, I tried hitting the other bin file, and got,
$ ./node_modules/mocha/bin/_mocha
path.existsSync is deprecated. It is now called `fs.existsSync`.
.
✔ 1 tests complete (1ms)
How can I just execute my tests with a single command? Vows seems to let you, but I've heard Mocha is the better choice, I just can't seem to get it working correctly.
And any thoughts on the error I got above in my third attempt?
Edit:
I'm running,
Ubuntu 11.10 64-bit
Node.js 0.7.5
npm 1.1.8
mocha 0.14.1
should 0.6.0
since npm 5.2.0, there's a new command "npx" included with npm that makes this much simpler, if you run:
npx mocha <args>
Note: the optional args are forwarded to the command being executed (mocha in this case)
this will automatically pick the executable "mocha" command from your locally installed mocha (always add it as a dev dependency to ensure the correct one is always used by you and everyone else).
Be careful though that if you didn't install mocha, this command will automatically fetch and use latest version, which is great for some tools (like scaffolders for example), but might not be the most recommendable for certain dependencies where you might want to pin to a specific version.
You can read more on npx here
Now, if instead of invoking mocha directly, you want to define a custom npm script, an alias that might invoke other npm binaries...
you don't want your library tests to fail depending on the machine setup (mocha as global, global mocha version, etc), the way to use the local mocha that works cross-platform is:
node node_modules/.bin/mocha
npm puts aliases to all the binaries in your dependencies on that special folder.
Finally, npm will add node_modules/.bin to the PATH automatically when running an npm script, so in your package.json you can do just:
"scripts": {
"test": "mocha"
}
and invoke it with
npm test
After further reading, and confirmation from Linus G Thiel above, I found I simply had to,
Downgrade to Node.js 0.6.12
And either,
Install Mocha as global
Add ./node_modules/.bin to my PATH
To run Mocha with mocha command from your terminal you need to install mocha globally on this machine:
npm install --global mocha
Then cd to your projectFolder/test and run mocha yourTestFileName.js
If you want to make mocha available inside your package.json as a development dependency:
npm install --save-dev mocha
Then add mocha to your scripts inside package.json.
"scripts": {
"test": "mocha"
},
Then run npm test inside your terminal.
While installing the node modules for mocha I had tried the below commands
npm install
npm install mocha
npm install --save-dev mocha
npm install mocha -g # to install it globally also
and on running or executing the mocha test I was trying
mocha test
npm run test
mocha test test\index.test.js
npm test
but I was getting the below error as:
'Mocha' is not recognized as internal or external command
So , after trying everything it came out to be just set the path to environment variables under the System Variables as:
C:\Program Files\nodejs\
and it worked :)
For windows :
Package.json
"scripts": {
"start": "nodemon app.js",
"test": "mocha"
},
then run the command
npm run test
Late answer but I think will work.
Install mocha globally
npm install --global mocha
If you have already installed mocha then set the path to bin

Resources