Mocha with Grunt: Cannot find module that exists - json

I am having issues with Mocha finding a module that exists. I have a data folder that contains all my test data. This folder exists and is at the correct directory (confirmed with pwd). When I run the test using mocha test filename the tests pass, but when I use grunt test I get the following error.
Running "mochaTest:tests" (mochaTest) task
>> Mocha exploded!
>> Error: Cannot find module '/Users/user/Documents/project/project_dispatcher/tests/data'
>> at Function.Module._resolveFilename (module.js:339:15)
>> at Function.Module._load (module.js:290:25)
>> at Module.require (module.js:367:17)
>> at require (internal/module.js:16:19)
>> at /Users/user/Documents/project/project_dispatcher/node_modules/mocha/lib/mocha.js:219:27
>> at Array.forEach (native)
>> at Mocha.loadFiles (/Users/user/Documents/project/project_dispatcher/node_modules/mocha/lib/mocha.js:216:14)
>> at MochaWrapper.run (/Users/user/Documents/project/project_dispatcher/node_modules/grunt-mocha-test/tasks/lib/MochaWrapper.js:51:15)
>> at /Users/user/Documents/project/project_dispatcher/node_modules/grunt-mocha-test/tasks/mocha-test.js:86:20
>> at capture (/Users/user/Documents/project/project_dispatcher/node_modules/grunt-mocha-test/tasks/mocha-test.js:33:5)
Warning: Task "mochaTest:tests" failed. Use --force to continue.
I am assuming this is a config issue, but I am lost as to what is causing it. I have the following structure:
->test
-->testfile.js
-->data
---->datafile.json
And I include the json file using:
var requestObj = require('./data/workflowRequest.json');
Is the ./ no longer valid because I am using Grunt which is located above the test folder? I am also confused because the stack trace only contains mocha files, and no test files that I wrote.
Edit Grunt code may be useful too
mochaTest: {
tests: {
src:['tests/**/*'],
options: {
reporter: 'spec'
}
},
watch: {
src:['tests/**/*'],
options: {
reporter: 'nyan'
}
},
tap: {
src:['tests/**/*'],
options: {
reporter: 'tap'
}
}
},

Related

Can we change the cypress.env.json file name

I want to use different environment variable file for prod environment and non prod environments. Currently I'm maintaining a single file for all environment and going forward each env file content will be get different according the environment. Hence is there a possibility to rename the file according the environment and pass it at run time or define the respective env file at a configuration file (cypress.json)
Sample env file names:
cypress.env.nonprod.json
cypress.env.prod.json
You can add scripts into project.json file:
{
"scripts": {
"setEnvDev": "cp cypress.env.dev.json cypress.env.json",
"setEnvStaging": "cp cypress.env.staging.json cypress.env.json",
"setEnvSproduction": "cp cypress.env.production.json cypress.env.json"
}
}
And run
$ npm run setEnvDev
$ npx cypress run
You can create cypress configuration files in your project root. Like for example if you have three config files:
production.json
staging.json
dev.json
Then depending on what configuration file you want to use, you can directly run the command:
npx cypress run --config-file staging.json
Set up cypress.env.nonprod.json and cypress.env.prod.json for envrionment-specific, and cypress.env.json for common variables.
In cypress/support/index.js add
const env = Cypress.env() // configured env from common cypress.env.json + command line
if (env.nonprod) { // has nonprod environment been set?
const addEnv = require('cypress.env.nonprod.json')
const merged = {...env, ...addEnv}
Cypress.env(merged)
}
if (env.prod) { // has prod environment been set?
const addEnv = require('cypress.env.prod.json')
const merged = {...env, ...addEnv}
Cypress.env(merged)
}
Run from command line (or set up script)
npx cypress run --env nonprod=true
This allows "stacking" where multiple files can be merged
npx cypress run --env nonprod=true,prod=true
Using cypress/plugins.index.js to merge named environment variable file
// plugins/index.js
module.exports = (on, config) => {
if (config.env.environment) {
const envars = require(`cypress.env.${config.env.environment}.json`
config.env = {
...config.env,
...envars
}
}
return config
}
To add cypress.env.nonprod.json to base config (cypress.json)
npx cypress run --env environment=nonprod

How to correct error in Bitbucket Pipeline running md-to-pdf

I am getting the below error when trying to run md-to-pdf (see https://www.npmjs.com/package/md-to-pdf) in a Bitbucket Pipeline script (see script below). 
Error
ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported.
See https://crbug.com/638180.
bitbucket-pipelines.yaml file
image: buildkite/puppeteer:v1.15.0
pipelines:
default:
- step:
caches:
- node
script:
- npm install -g md-to-pdf
- doc="appendix"
- md-to-pdf --config-file config.json ${doc}.md
config.json file
I tried to follow instructions for this. Is this config.json malformed?
{
"launch_options": {
"args": ["no-sandbox"]
}
}
The correct syntax is:
{
"launch_options": {
"args": ["--no-sandbox"]
}
}

Gulp Build command is failing with error " EISDIR: Illegal operation on directory"

I am trying to run the gulp build task for the dev environment on the server but its failing. However, The same gulp build is working on my local machine. The function and error are given below.
Function:
// S3 Upload for dev
gulp.task('s3sync:dev', function () {
var config = {
accessKeyId: "-Key-",
secretAccessKey: "-Key-"
};
var s3 = require('gulp-s3-upload')(config);
return gulp.src("./dist/**")
.pipe(s3({
Bucket: 'example',
ACL: 'public-read'
}, {
maxRetries: 5
}))
});
Command:
Gulp build:development
Error:
[09:01:04] Starting 's3sync:dev'...
events.js:160
throw er; // Unhandled 'error' event
^
Error: EISDIR: illegal operation on a directory, read
at Error (native)
Any idea?
Finally, This problem has been solved by removing a system symlink which was created after the deployment from the capistrano which is also running below npm commands.
npm run clean && npm run build
After removing the system file. I have run the below command and it works fine.
gulp build:development

Is there a way to switch cwd by changing environment in PM2 - node.js

I am using PM2 to manage the execution of a couple of micro-apps on node.
Goal:
However I would like to be able to automatically switch settings and the cwd value based on the environment the app is executing in.
For example: on my local machine CWD should be ~/user/pm2, while on the server it needs to be E:\Programs\PM2.
Is there any way to do this using JSON config options with PM2? Is there a better way to manage the variables for different environments?
you can save a shell script, say pm2_dev.sh containing the cd command as first line.
#!/bin/bash
cd /foo/bar
pm2-dev run my-app.js
OR you can add input to your script:
# pm2_dev.sh ~/user/pm2
file should be:
#!/bin/bash
cd $1
pm2-dev run my-app.js
If you do not want to change environment by shell script, you can follow documentation way:
{ "apps" : [{
"script" : "worker.js",
"watch" : true,
"env": {
"NODE_ENV": "development",
},
"env_production" : {
"NODE_ENV": "production"
} },{
"name" : "api-app",
"script" : "api.js",
"instances" : 4,
"exec_mode" : "cluster" }] }
When running your application you should use --env option as it is written here:
--env specify environment to get
specific env variables (for JSON declaration)
Finally you can wrap configuration in a js object that conditionally returns parameters basing on current environment:
module.exports = (function(env){
if( env === 'development' )
return { folder: '~/user/pm2' };
else if( env === 'production' )
return { folder: 'E:\Programs\PM2' };
}(process.env.NODE_ENV));
Then you can require the config file and access it being sure that it returns always the correct config.

No tests executed with grunt-qunit-junit

I am following this guide to generate junit output from my js tests:
https://github.com/sbrandwoo/grunt-qunit-junit
I have installed grunt-qunit-junit into my local test project:
npm install grunt-contrib-qunit --save-dev
And this is my Gruntfile.js:
module.exports = function(grunt) {
"use:strict";
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
qunit_junit: {
options: {
},
all: ["all_tests.html"]
},
})
grunt.loadNpmTasks('grunt-qunit-junit');
};
where all_tests.html is located in the same dir and lists all my *test.js files. But when I run:
user#ubuntu:~/Test$ grunt qunit_junit
Running "qunit_junit" task
>> XML reports will be written to _build/test-reports
Done, without errors.
Why are the tests not executed (the folder _build/test-reports is not created)?
The README states that you should execute both the qunit_junit and qunit tasks: http://github.com/sbrandwoo/grunt-qunit-junit#usage-examples
For example: grunt.registerTask('test', ['qunit_junit', 'qunit']);