I'm trying to set up testing for my Next.js project. I want to test it with RITEway which is based on tape. I want a test command that finds all files in my src/ folder that end with .test.js.
Here is the commend I came up with:
"test": "NODE_ENV=test node -r #babel/register src/**/*.test.js",
I get the error:
Error: Cannot find module '/path/to/project/src/**/*.test.js'
How can I tell node to find all files ending in .test.js in my src/ folder?
Extra context:
My testing files live in src/features/<feature>/<feature.test.js>, e.g.:
"test": "NODE_ENV=test node -r #babel/register src/features/home/home-page-component.test.js",
Works to find a single file and run it.
"test": "NODE_ENV=test node -r #babel/register src/**/**/*.test.js",
Works to find all folders in features, but ignores files like src/<file>.test.js, which I also want to run.
I had to install #babel/register and #babel/core for Node to process absolute imports and newer syntax.
My .babelrc is:
{
"env": {
"test": {
"plugins": [
[
"module-resolver",
{
"root": [
"."
],
"alias": {
"features": "./src/features"
}
}
]
]
}
},
"presets": [
[
"next/babel"
]
],
"plugins": []
}
As Jon Sharpe said, you have to feed the regex into riteway.
"test": "NODE_ENV=test riteway -r #babel/register 'src/**/*.test.js'",
Related
I am trying to run Jest tests as part of a script before I then run webpack, like this.
npm run test
webpack --progress --profile --watch --mode development
Jest only works with compiled code, so I had set my .babelrc to the following, which worked, however then it transpiled all my code in webpack which I didn't want, in development mode I want to leave the JavaScript un-transpiled so I can work with it without it being obfuscated.
{
"presets": [ "#babel/preset-env" ]
}
Instead I want to run Jest by calling 'npm run test' which then I can specify only that script transpiles the code and then webpack runs without transpiling, I was hoping something like this in my .babelrc file
{
"env": {
"test": {
"presets": [ "#babel/preset-env" ]
}
}
}
Then in my package.json I could set the env to test which then would leave webpack alone.
"scripts": {
"test": "SET env=test&& jest --config jest.config.js"
}
With this setup I still get the following message appearing when 'npm run test' runs which shows the babelrc file isn't being hit.
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
Can anyone help?
So turns out my test was ok in the .babelrc file
{
"env": {
"test": {
"presets": [ "#babel/preset-env" ]
}
}
}
And the script needed in my package.json was this without setting any node env
"scripts": {
"test": "jest --config jest.config.js"
}
It was actually my webpack script that wasn't configured correctly, I needed to add '--env.NODE_ENV=development' at the end
webpack --progress --profile --watch --mode development --env.NODE_ENV=development
Which could then be checked within my webpack.config file.
module.exports = (env) => {
const isDevelopment = env && env.NODE_ENV === 'development';
...
then in my rules test for isDevelopment
rules: [{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: isDevelopment ? {} : { presets: ['#babel/preset-env'] }
}
},
I need to include the aliases from webpack into AVA when it runs.
I used webpack's resolve.alias to access all the files under src folder:
webpack.config.js
resolve: {
alias: {
'#': path.resolve(__dirname, 'src/'),
},
},
and then that # special prefix for my own modules like this:
my-module.js
import main from '#/view/main'
This is my AVA configuration:
package.json
"scripts": {
"test-unit": "ava test/unit"
},
"ava": {
"require": ["esm"]
},
Is possible to add something to package.json like in this mocha solution?
https://stackoverflow.com/a/42989394/12361834
Thank you so much for your time and help!
If I understood you well you can do it with link-module-alias npm package.
Add this to your package.json:
"scripts": {
"postinstall": "link-module-alias",
"preinstall": "command -v link-module-alias && link-module-alias clean || true"
"test": "ava"
},
"_moduleAliases": {
"#": "src"
}
npm i && npm test
If you need further details you can download a working example here.
In my package.json I added "test": "set NODE_ENV=test && jest --watch" to set up my unit tests.
After troubleshooting for a long time to find out why I get this error message:
{import { configure } from 'enzyme';
^
SyntaxError: Unexpected token {
I found that by removing set NODE_ENV=test && I resolve the issue. I am now thinking that it must have something to do with the test config instead:
"babel": {
"env": {
"test": {
"presets": [
[
"next/babel",
{
"preset-env": {
"modules": "commonjs"
}
}
],
"#babel/preset-env"
],
"plugins": [
"transform-es2015-modules-commonjs"
]
}
}
}
Adding "transform-es2015-modules-commonjs" and #babel/preset-env has been suggested in similar posts, but I am not confident it is related to the issue.
I installed cross-env and added it: "test": "cross-env NODE_ENV=test jest --watch" which works perfectly. I am on a windows machine, by the way.
How is cross-env compiling my commands differently than set NODE_ENV=test && jest --watch ??
I'm trying to run some test over my JS files.
mocha runs them without any problem, though I have a definition for aliases in my .babelrc file as follows:
"presets": ["es2015", "react", "stage-0"],
"env": {
"test": {
"plugins": [
[ "babel-plugin-webpack-alias", {
"config": "./webpack.config.js"
} ]
]
}
}
for some reason, the code coverage of istanbul covers only the webpack.config.js file(??)
the npm test command which I run is:
istanbul cover --handle-siginit --hook-run-in-context ./node_modules/mocha/bin/_mocha -- test/test.js --compilers js:babel-core/register --require test/setup.js -R spec
(in the file test.js I define NODE_ENV: process.env.NODE_ENV = "test";)
in case relevant,
My code is written in ES6 + JSX and I run just one test function
Cheers!
I'm trying to get started with a simple Grunt example, but I'm running into an issue with grunt-contrib-concat.
Here's my Gruntfile.js:
$ cat Gruntfile.js
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
concat: {
options: {
separator: ';'
},
dist: {
src: ['src/**/*.js'],
dest: 'dist/<%= pkg.name %>.js'
}
}
});
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.registerTask('default', ['concat']);
and my package.json:
$ cat package.json
{
"name": "Linker",
"version": "0.0.0",
"description": "A test project that is meant to be a dependency",
"repository": {
"type": "git",
"url": "git://github.com/jonbri/Linker.git"
},
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"grunt-contrib-concat": "*"
},
"author": "",
"license": "ISC"
}
Running npm install doesn't show any obvious errors:
$ npm install
$ ls node_modules/
grunt grunt-contrib-concat
Here is what my directory structure looks like:
$ ls
Gruntfile.js node_modules package.json README.md src
$ ls src
index.js
When I run grunt concat I get this:
$ grunt concat
Loading "concat.js" tasks...ERROR
>> Error: Cannot find module 'ansi-styles'
Warning: Task "concat" not found. Use --force to continue.
Aborted due to warnings.
My setup:
Lubuntu 12.10, node: v0.10.25, npm: 1.4.21, grunt-cli: v0.1.13, grunt: v0.4.5
Am I missing something?
You should run it as: grunt without concat as the task is the default one. No arguments are needed to run it. So the command to launch becomes simply:
grunt
Use this to install grunt-contrib-concat:
npm install grunt-contrib-concat --save-dev
Package.json should have something like this:
"devDependencies": {
"grunt-contrib-concat": "^0.5.1"
},