On my build step I'm using babel to transpile the code to es5 (from src to dist). How do I make it exclude files ending in .test.js?
package.json
"scripts": {
"build": "babel src --out-dir dist",
.babelrc
{
"presets": [ "es2015" ],
"ignore": "\\.test\\.js"
}
Based on the documentation, you should be able to write .babelrc
{
"ignore": [
"**/*.test.js"
]
}
However, I was able to verify that this does not seem to work. I tried it with version 6.5.1 (babel-core 6.5.2).
At the same time, the following does work:
babel src --out-dir build --ignore '**/*.test.js'
That is the same glob pattern as written in the .babelrc file. If you install any glob library from npm you'll find that this glob pattern would work (that is how I came up with it...I do not currently use babel).
As of today, the following works in .babelrc
(babel-core: v6.26.3)
"ignore": [
"**/__tests__", // ignore the whole test directory
"**/*.test.js" // ignore test files only
]
Related
I have a TypeScript project, where the test are written in TypeScript. I want to use mocha to directly test TS files. I'm using ts-node for that, as described in ts-node#mocha.
In the project I'm using a JS library which doesn't have TS type definitions. So I created a d.ts file for that. Everything works well when compiling and running the projects. However mocha fails with:
% yarn mocha --require ts-node/register --require source-map-support/register ./test/*.ts
src/client.ts:3:26 - error TS7016: Could not find a declaration file for module 'algosdk'. '/home/robert/projects/algorand/ts-mocha/node_modules/algosdk/index.js' implicitly has an 'any' type.
Try `npm install #types/algosdk` if it exists or add a new declaration (.d.ts) file containing `declare module 'algosdk';`
3 import * as algosdk from "algosdk";
It seams that ts-node doesn't recognize the d.ts files.
The only solution which works is to use require instead of import statement, but this will not link the types to algosdk.
How to use mocha with TS files and type definition file?
This is the project structure (also in github):
├── build <-- JS from compiled TS go here
├── node_modules
├── package.json
├── src
├── #types <-- d.ts files
├── test
├── tsconfig.json
The tsconfig.json:
{
"compilerOptions": {
"target": "es2017",
"lib": ["esnext"],
"module": "commonjs",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"outDir": "./build",
"typeRoots" : ["./#types", "node_modules/#types"]
},
"exclude": ["**/node_modules"],
"include": [
"src",
"test",
"#types"
]
}
UPDATE
I confirm that the problem is related to ts-node, not to mocha. Running:
yarn ts-node src/client.ts
returns the same error.
References:
Mocha + ts-node
TypeScript: Could not find a declaration file for module in unit tests, only
you can add the following configuration to tsconfig.json file.
"ts-node": {
"files": true
},
it works well on my side.
I am using this npm module (module-alias)
https://www.npmjs.com/package/module-alias in my Node project.
To make use of this package you have to set path aliases in the package.json file.
However, using this package comes with the disadvantage, that intellisense doesn't work anymore.
My question is how to enable intellisense with those path aliases?
The problem is that you did not register those aliases anywhere with your linter. I would gerenally suggest to use ESLint here (even if you use TypeScript as TSLint will be discontinued in favour of ESLint). My examples will include the TypeScript endings as well. If you definately want to make it work for JavaScript only you can skip the .ts .tsx extentensions in the eslint) So to make intellisense work do this in
.eslintrc.js
settings: {
"import/resolver": {
alias : {
map: [
["#","./src"]
],
extensions: [".js", ".jsx", ".ts", ".tsx"],
},
}
},
Note that in this case you will need the import Plugin for ESLint. If you don't already have it install it.
If you are using TypeScript you will also have to make that alias known to your compiler. So add this to your
tsconfig.json
"compilerOptions": {
"baseUrl": ".",
"paths": [
"#/*" : [
"src/*"
]
]
}
Is there any WORKING way how to bundle node project into one single file (including dependencies) and how?
I am using babel (.babelrc)
{
"presets": ["#babel/preset-env"],
"plugins": [
[
"module-resolver",
{
"root": [
"./src"
],
"alias": {
"test": "./test",
"underscore": "lodash"
}
}
]
]
}
the answer is no. babel can not do what you want by itself. It is a tool for transforming one dialect of Javascript into another, based on rules defined in your .babelrc file. It is a compiler, not a linker (to borrow terms from the C world).
Using babel-plugin-module-resolver will not cause babel to transpile dependencies as if they were source files. It is simply a babel rule which modifies the paths passed to require() or import.
To include dependencies as well as source files you need to use both a compiler like babel and a bundler such as webpack or rollup.
I have a es2017 code with async/await, I want to transform it to es5 so that it'll be supported by most Node.js versions.
My current .babelrc file looks like this:
{
"presets": ["es2015", "es2016", "es2017"]
}
So I'm transforming es2017 to es2016, from es2016 to es2015 and from es2015 to es5.
When I'm trying to run the code after I built it with babel src -d dist -s I'm getting error saying that: ReferenceError: regeneratorRuntime is not defined
How can I transform the es2017 code to es5? I wanna publish the code later and make it usable by node.js v4 and up.
Thanks to #Bergi I found a way how to solve this ReferenceError: regeneratorRuntime is not defined error.
I added the transform-runtime plugin to my .babelrc
Now my .babelrc is:
{
"presets": ["es2015", "es2016", "es2017"],
"plugins": ["transform-runtime"]
}
There aren't any errors now and it works fine.
I'm having some issues while running Istanbul with Mocha and the Babel compiler.
All my tests are running just fine, but after all the tests done it shows me this message:
No coverage information was collected, exit without writing coverage information
And it is not producing any coverage report.
The command that I am running is:
NODE_ENV=test istanbul cover _mocha -- --require babel-core/register --recursive
The project is hosted on GitHub:
https://github.com/weslleyaraujo/react-flux-puzzle/tree/feat/unit-tests-24
Any ideas what it could be?
Using Babel 6.x, let's say we have file test/pad.spec.js:
import pad from '../src/assets/js/helpers/pad';
import assert from 'assert';
describe('pad', () => {
it('should pad a string', () => {
assert.equal(pad('foo', 4), '0foo');
});
});
Install a bunch of crap:
$ npm install babel-istanbul babel-cli babel-preset-es2015 mocha
Create a .babelrc:
{
"presets": ["es2015"]
}
Run the tests:
$ node_modules/.bin/babel-node node_modules/.bin/babel-istanbul cover \
node_modules/.bin/_mocha -- test/pad.spec.js
pad
✓ should pad a string
1 passing (8ms)
=============================================================================
Writing coverage object [/Volumes/alien/projects/forked/react-flux-puzzle/coverage/coverage.json]
Writing coverage reports at [/Volumes/alien/projects/forked/react-flux-puzzle/coverage]
=============================================================================
=============================== Coverage summary ===============================
Statements : 100% ( 4/4 )
Branches : 66.67% ( 4/6 ), 1 ignored
Functions : 100% ( 1/1 )
Lines : 100% ( 3/3 )
================================================================================
UPDATE: I've had success using nyc (which consumes istanbul) instead of istanbul/babel-istanbul. This is somewhat less complicated. To try it:
Install stuff (you can remove babel-istanbul and babel-cli):
$ npm install babel-core babel-preset-es2015 mocha nyc
Create .babelrc as above.
Execute this:
$ node_modules/.bin/nyc --require babel-core/register node_modules/.bin/mocha \
test/pad.spec.js
...which should give you similar results. By default, it puts coverage info into .nyc-output/, and prints a nice text summary in the console.
Note: You can remove node_modules/.bin/ from any of these commands when placing the command in package.json's scripts field.
PS: I now recommend to use single jest instead of mocha/instanbul/nyc/chai/etc.
Solution A: Using nyc and babel-plugin-istanbul
Setup (don't forget #next for nyc):
npm install --save-dev nyc babel-plugin-istanbul babel-register
Add an env to babel config:
{
"env": {
"nyc": { "plugins": ["istanbul"] }
}
}
nyc config:
{
"reporter" : ["text", "text-summary", "lcov", "html"],
"include" : ["src/**/*.js"],
"require" : ["babel-register"],
"sourceMap" : false,
"instrument" : false,
"all" : true
}
PS: include field needs to be specified in .nycrc of in package.json, if specified in command line, coverage will not works
Running the tests:
# 1. Build
NODE_ENV=nyc babel src --out-dir lib
# 2. Coverage
nyc mocha
Solution B: No extra packages : Only the basic ones
Work has been done recently on istanbul (1.0.0-alpha.2) to support Babel generated code with sourcemaps (see #212 and this for an example).
There are 2 ways:
A. Tests written against previously transpiled code
B. Tests written against original code and transpiled all together in memory at runtime
B1. Tests that exports (previously) transpiled code
This is done in 2 steps: Firstly, build your source with babel (e.g. from ./src to ./out) and write your tests against transpiled source (export foo from "./out/foo";).
Then you will be able to run the tests using istanbul 1.0.0-alpha.2 :
istanbul cover _mocha -- ./test --compilers js:babel-register
Now if you want code coverage to follow the original code you've written (not the transpiled one), make sure to build with babel source-maps options set to both :
babel ./src --out-dir ./out --source-maps both
PS: If needed you can also do :
istanbul cover _mocha -- ./test --compilers js:babel-register \
--require babel-polyfill \
--require should \
--require sinon
B2. Tests that directly exports original code
In this case you write your tests against original source (export foo from "./src/foo";), and with no further step, you directly run istanbul 1.0.0-alpha.2 using babel-node against cli.js :
babel-node ./node_modules/istanbul/lib/cli.js cover _mocha -- ./test
PS: If needed you can also do :
babel-node ./node_modules/istanbul/lib/cli.js cover _mocha -- ./test
--require babel-polyfill \
--require should \
--require sinon
As of now 17.4.2016 this coverage reporting stuff is still a bit messy and with my React-project that has .jsx files and a helper file the coverage reporting script looks like this:
istanbul cover node_modules/mocha/bin/_mocha -- \
--compilers js:babel-core/register \
--require ./test/testhelper.js \
\"test/**/*#(.js|.jsx)\"
So it wouldn't be too easy the current version 0.4.3 of Istanbul doesn't work with Babel so you have to use the experimental alpha-version:
npm install istanbul#1.0.0-alpha.2 --save-dev
And then you need .istanbul.yml -file so that Istanbul recognizes the .jsx-files with these lines:
instrumentation:
root: .
extensions: ['.js', '.jsx']
And now it should work. Also as a small bonus if you want to add coverage reporting with Travis and Coveralls you should:
enable the project in https://coveralls.io
add coveralls npm i coveralls --save-dev
add this to your .travis.yml:
script:
- npm --silent test
- cat ./c
coverage/lcov.info | coveralls
And now you can put that cool badge to your README. Neato!