Saturday, 27 May 2017

Jest Testing with require modules: ejs-loader

I am playing with the idea of having large static html bundles just loaded into a react component vice typing them all out in jsx. I am currently just experimenting with ejs-loader and html-react-parser to evaluate the feasibility of this. Everything actually renders fine but I cannot get any tests to work with jest for this. I receive: 'Cannot find module 'ejs-loader!./AboutPage.view.ejs' from 'AboutPage.js'' errors and I am unsure of what to do.

I am currently just working off of react-slingshot as my base for experimenting with this.

The component itself is simple:

import React from 'react';
import Parser from 'html-react-parser';
import '../styles/about-page.css';

const view = require('ejs-loader!./AboutPage.view.ejs')();

// Since this component is simple and static, there's no parent container for it.
const AboutPage = () => {
  return (
    <div>
      {Parser(view)}
    </div>
  );
};

export default AboutPage;

And the test is:

import React from 'react';
import {shallow} from 'enzyme';
import AboutPage from './AboutPage';

describe('<AboutPage />', () => {
  
  it('should have a header called \'About\'', () => {
    const wrapper = shallow(<AboutPage />);
    const actual = component.find('h2').text();
    const expected = 'About';

    expect(actual).toEqual(expected);
  });
  
});

I have read through the docs and similar questions like this. I attempted to use a custom transformer, but I may be misunderstanding something as it doesn't appear to be even called.

Package.json

"jest": {
    "moduleNameMapper": {
      "\\.(css|scss)$": "identity-obj-proxy",
      "^.+\\.(gif|ttf|eot|svg|woff|woff2|ico)$": "<rootDir>/tools/fileMock.js"
    },
    "transform": {
      "^.+\\.js$": "babel-jest",
      "\\.(ejs|ejx)$": "<rootDir>/tools/ejx-loader/jest.transformer.js"
    }
  },

and the transformer itself:

module.exports = {
  process(src, filename, config, options){
    console.log('????');
    return 'module.exports = ' +  require(`ejs-loader!./${filename}`);
    //return require(`ejs-loader!./${filename}`);
  }
};


via AndrewHunter

Control multiple BLE peripherals with one central

I am pretty new to IoT and BLE. What I want to achieve is to write on GATT characteristic of at least 100 peripheral devices simultaneously or in a very short period of time(i.e. less than 100ms). Currently my peripherals are Ble nano (nRF51822) with custom GATT service running on them. I am using my Mac as central BLE. I am using Node.js and noble to implement the central. I want to know if such thing is possible at all and if possible, please link me to an example code. :)



via Goris

NodeJS and robots.txt that returns ContentType -> text/html

I try to set robots.txt file in ExpressJS. I found that to make all fine needed to add middleware function into app.js . But all time when i test my robots.txt i get ContentType = text/html . What i do wrong?

// app.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var index = require('./routes/index');
var users = require('./routes/users');
var test = require('./routes/test');
var test1 = require('./routes/test1');
var test2 = require('./routes/test2');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.get('/robots.txt', function (req, res) {
    res.type('text/plain');
    res.send("User-agent: *\nDisallow: /engine/go.php\nDisallow: /engine/download.php\nDisallow: /user/\nDisallow: /newposts/\nDisallow: /statistics.html\nDisallow: /*subaction=userinfo\nDisallow: /*subaction=newposts\nDisallow: /*do=lastcomments #comments into text\nDisallow: /*do=feedback\nDisallow: /*do=register\nDisallow: /*do=lostpassword\nDisallow: /*do=addnews\nDisallow: /*do=stats\nDisallow: /*do=pm\nDisallow: /*do=search\n\n# comment in text\n\nUser-agent: Yandex\nDisallow: /*sid= # comment\nDisallow: /*do=addnews\nDisallow: /*do=stats\nDisallow: /*do=pm\nDisallow: /*do=search\n\nUser-agent: Googlebot\nDisallow: /newposts/\nDisallow: /statistics.html\nDisallow: /*subaction=userinfo\n\n# some more comments\n# and one more comment");
});

app.use('/', index);
app.use('/users', users);
app.use('/test', test);
app.use('/test1', test1);
app.use('/test2', test2);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;



via Nikita M

Can't Parse Data node.js correctly

I have tried to send a post request to an api in node.js yet got the response

{ Error: 'Internal server error' }

I used this code:

var request = require('request');
var config = require("./config.json");


var request = require('request');
request.post({
  url:     'https://api.mymonero.com:8443/get_address_txs',
  json:    {method: "post", address:config.address, viewkey:config.viewkey }
}, function(error, response, body){
  console.log(body);
});

This isn't the server as I can do the exact same thing in python really easily and get the expected response

import requests
address = "*Monero Address*"
viewkey = "*Monero Viewkey*"
data = {"address":address,"view_key":viewkey}
response = requests.post("https://api.mymonero.com:8443/get_address_txs",json=data)
print(response.text)

I'm sorry if i'm being dumb I'm new to node.js

Expected Response:

{
  "total_received": "0",
  "scanned_height": 2586375,
  "scanned_block_height": 1319524,
  "start_height": 2586052,
  "transaction_height": 2586375,
  "blockchain_height": 1319524
}

Thanks :)



via Joss Bird

How to add Redis configuration in express-request-proxy node package

I'm using "express-request-proxy" node package for adding proxy for the HTTP requests. I have enabled the Redis cache which works fine in my local server.

Here is how I am loading the Redis packages in my local as suggested in the documentation (https://github.com/4front/express-request-proxy#caching):

var redis = require('redis');
require('redis-streams')(redis);

However when I'm pushing the app to cloud, I'm seeing the following error:

Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379

I have the Redis server details of the cloud but I'm not quite sure how to configure the host, port and password here.

Could someone suggest on this please?



via Ansuman

React Native: Cannot read property 'RNFSFileTypeRegular' of undefined

I'm using https://github.com/itinance/react-native-fs in my app. But on running I'm getting the following error message.

Cannot read property 'RNFSFileTypeRegular' of undefined.

But the same repo cloned and ran in another laptop works fine.

Using Mac OS Sierra.

I suspect this has something to do with `node_modules'.

Tried uninstalling node and npm, still the same error. What am I doing wrong here ?



via Sooraj Chandran

In gitlab ci : is it possible to run a command before building the artifact and just for the artifact?

In gitlab-ci, I would like to produce a npm prune --production just before producing my artifact.

I don't want to alter my cached node_modules folder and I want to keep it as it is for the next stage.

Is this possible ?



via BigDong