tl;dr: the stubDate
helper can be copied from the bottom of the article.
It is a common case in unit testing to need a static date returned in your tests
so that you can either have a fixed expectations (e.g. in a JSON), or to prevent
random failures when tests are executed seconds later than when the expectations
where made.
For this reason, if you are using Jest, you want to stub the Date
object
inside your Jest tests so that when calling new Date()
or Date.now()
aways
returns the same result.
Jest does not provide a built in method for stubbing the JavaScript Date
object so we have to do this manually. This can be done by overriding the global
Date
object.
const fixedDate = new Date('2018-02-28T09:39:59');
Date = class extends Date {
constructor() {
super();
return fixedDate;
}
};
This is enough to get a test passing, which will always return the time
"2018-02-28 09:39:59", regardless which Date
methods are called. For example,
calling getTime()
on the date instance will always return "1519807199000".
beforeAll and afterAll
Since we are using Jest we can use the beforeAll
function to set the date
before all tests in a test file, so that this code does not have to be copied
into each test case.
const fixedDate = new Date('2018-02-28T09:39:59');
beforeAll(() => {
Date = class extends Date {
constructor() {
super();
return fixedDate;
}
};
});
It is also important to reset Date
back to the original "real" Date
object
after all tests have run to prevent confusing errors in future tests where you
expect to work with a non-stubbed date. We can do this with a Jest afterAll
function. This means we have to cache the original Date
object in the
beforeAll
.
const fixedDate = new Date('2018-02-28T09:39:59');
let _originalDate;
beforeAll(() => {
_originalDate = Date;
Date = class extends Date {
constructor() {
super();
return constantDate;
}
};
});
afterAll(() => {
Date = _originalDate;
});
stubDate
helper
Finally we can piece this all together into a handy stubDate
helper than can
be imported into any test file, leaving the test implementation clean of the
stubbing boilerplate code. I usually use a file called test-helper.js
where
helper functions like this live.
// test-helper.js
export const stubDate = fixedDate => {
let _originalDate;
beforeAll(() => {
_originalDate = Date;
Date = class extends Date {
constructor() {
super();
return fixedDate;
}
};
});
afterAll(() => {
Date = _originalDate;
});
};
This can then be imported into a test file and the before and after actions will
automatically be applied to your tests. For example:
// my-spec.js
import { stubDate } from './test-helper';
it('can stub the global date object', () => {
const myDate = new Date('2018-02-28T09:39:59');
stubDate(myDate);
// This expectation will always pass regardless of what time the test is run
expect(Date.now()).toEqual(1519807199000);
});
After searching and getting a lot of different answers on how to move all files in a project from one file pattern to another, I found this article had the simplest solution.
I was trying to move all files in a Jest project from having the suffix _spec.js
to have .spec.js
. The following line of bash got the job done:
for f in *_spec.js; do mv "$f" "${f/_spec/.spec}"; done
If you want to test this before actually moving the files and see what action will be done, add an echo before the mv
call in the loop, which will print the mv
calls that would be made:
for f in *_spec.js; do echo mv "$f" "${f/_spec/.spec}"; done
I also like the idea of using rename but this is not available by default on OS X and I do not want to install it since this is not a program I will be using often. It does have a much simpler syntax though and can be piped to find
, which would look like:
find -name "*_spec.js" -type d | rename 's/_spec/\.spec/g'
Like most developers I have large libraries of PDF books lying around in a
folder somewhere. In order to organise the books and get a better overview
I decided to work on a weekend project designed to investigate
Gatsby.js, a React / GraphQL based static site
generator. The project was to use Gatsby to build a PDF library that would allow me to
put my PDF books into the project and then build a simple static website that
would list the PDFs, give some information about each book, allow me to read
them online, and finally download them if I wanted.
Demo
My static Gatsby based PDF library ended up with the following features:
- List view of PDF books, read from the file system
- Metadata extraction from the PDF book (Author, Page count, etc.)
- Read a book online
- "Fullscreen" reading mode
- Download a book
- Remember your page number, for future reading
- Search for books
You can see a demo of the Library here:
https://blakesimpson.github.io/gatsby-library/
What is Gatsby?

As mentioned in the introduction, Gatsby.js is a
static site generator built with JavaScript. Gatsby uses React for building the views and a
GraphQL API that is queried from your views to read
information about your static files.
This means you can add, for example, a bunch of Markdown files to your project
and Gatsby will run through each file (what it calls a "node") to index these
and put them into the GraphQL index. You can then build a list of your Markdown
articles to the home page and link to each one and render it as a web page.
With this setup, Gatsby is very good for building static websites that do not
rely on a database at runtime, for example, a blog.
However, Gatsby can not only read Markdown files from your system, it can read
any file type such as an Excel spreadsheet, or as I discovered, a PDF.
Before going on to show how I extracted PDF information and put it into the
Gatsby index you might want to try out some
Gatsby starter examples such
as the gatsby-blog
.
Also, if you have never worked with Gatsby before, it is a good idea to work
through the tutorial which helps to
understand the Gatsby concepts such as configuration, the node server, and how
the plugins work.
I recently had the issue where I needed to proxy traffic from my IP address to a different server, in order to perform mobile testing.
The setup I have is a "local" server running on my development machine with a local hostname, since my phone can not access this hostname,
I decided to use a proxy that allows me to visit the IP address of my development machine on my phone and all traffic would forward to
the local domain.
I decided to use Express.JS as it is an easy to use server. Express also has large amount of community
contributed middlewares, such as express-http-proxy which fills all of my requirements.
Additionally I use the package "ip" that will discover the local IP address of my development machine.
These dependencies can be installed with:
yarn add express express-http-proxy ip
The proxy server is quite small, and looks as follows:
Update 14/03/2018:
Thanks to a pull request to my example repository by Francisco Presencia, the creator of Server.js, he explains that
my original example can actually be cut down to a single line of code.
Server.js already contains the static
middleware from Express so there is no need to include and configure the server manually. Server.js assumes that you want to serve static files from a directory called public/
in your project, in which case you can simply create the server as:
require('server')();
If you would like to serve the static files from a different directory, such as the root directory, as in my case you can simply pass a public
configuration option when initializing the server. For example:
require('server')({ public: '.' });
This highlights how Server.js is even simpler to use than I assumed.
Server.js is a node based server that is based on Express.js but provides you with an even easier interface to write your server with.
As server.js was only recently released, I was trying to find a tutorial on how to serve a directory of static files but could not find one. After reading the documentation, I noticed that since Server.js is built on express, you can use express middlewares out of the box. Since Express already has a express.static()
method, I noitced that I could use this to serve static files as I wanted.
The example server is very basic, it simply serves all files in the current directory.
First of all, I import server
and express
:
const server = require('server');
const express = require('express');
Next I import the modern
method from server.utils
that allows us to attach Express middleware to our server:
const { modern } = server.utils;
I then call the express.static
middleware, and pass it to modern
:
const middleware = modern(express.static('./'));
Finally, I start the server and pass the static middleware:
server(middleware);
The final server is only 7 lines long:
const server = require('server');
const express = require('express');
const { modern } = server.utils;
const middleware = modern(express.static('./'));
server(middleware);
You can test this by writing this to a file called server.js
and calling it with node server.js
. The server will then be running at: http://localhost:3000/
.
If you then write an index.html
file in the same directory, it will be served when visiting this address in your browser.
For full example code, please visit this repo: https://github.com/BlakeSimpson/serverjs-static-files
React Native allows you to automatically truncate text that will not fit within its <View>
container.
There is a property numberOfLines
that can be passed to the <Text>
node. For example:
<View style={styles.container}>
<Text numberOfLines={1} style={styles.text}>This is a very long text that will overflow on a small device</Text>
</View>
In most cases this is enough for the device to truncate the text, automatically adding ellipsis to the end of the string (…) after however many lines of text you have specified (In this case, we only want 1 line).
If you however find that you have added the numberOfLines
property but the text is still not truncated, as so:

Then make sure your text node styles have the flex: 1
property. For the example above, the StyleSheet
would look like:
var styles = StyleSheet.create({
container: {
flexDirection: 'row',
padding: 10
},
text: {
flex: 1
}
});
Which will allow the <Text>
node to apply the truncation properly:

The MySQL version that will be installed through Homebrew for El Capitan is currently version 5.7.9. This is an upgrade from the 5.6.x versions that brew would install on previous OS X versions.
This new version of MySQL has some security features and syntax changes that may be confusing. Personally after installing through Homebrew I did not know the default password and wanted to set up the server to accept the root user with no password, for my development environment. Read on to see how I achieved this.
Getting a root user with no password
Run the following highlighted steps in the Terminal.
- Lines prefixed with "$" are a terminal command.
- Lines prefixed with "mysql >" are entered in the MySQL command line client.
Stop the server
$ mysql.server stop
Also unload the launchctl
file, if you have one, to prevent the server restarting:
$ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Starting Safe Server
Now that MySQL has stopped running, you can start the mysqld_safe
process. We use the --skip-grant-tables
option to prevent permission errors when trying to update the password later.
$ mysqld_safe --skip-grant-tables &
Reseting the password: Part 1
Next we will enter the MySQL command line and ask the server to change the remove the password for the "root" user.
$ mysql -u root
mysql> FLUSH PRIVILEGES;
mysql> use mysql;
mysql> UPDATE user SET authentication_string=PASSWORD('') WHERE User = 'root';
mysql> exit
You may notice that in MySQL 5.7.6+ the password
field has been changed to authentication_string
.
Reseting the password: Part 2
Now that the login password has been set, we can kill the safe server:
$ killall mysqld
Next restart the "real" MySQL server. This will take a few moments.
$ mysql.server start
Now we can log in to MySQL successfully as the root user with no password. Login with the following command (when prompted for the password, don't type anything, just hit enter):
$ mysql -u root -p
Enter password:
Now the process gets confusing. You are logged in as root successfully but try to run use mysql;
, you see the following error:
mysql> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
Now you must set the password again using the new MySQL 5.7.6 method (ALTER USER):
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '';
Done!
Finally we are done. You can exit the MySQL CLI using the exit
command and are back to the terminal. You can try to log in again and access your tables and not experience any errors.
If you wish you can now reload the launchctl
plist for MySQL so that the server starts automatically when your computer does.
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
For some reason since the El Capitan upgrade the /usr/local/bin
and /usr/local/share
directories are not owned by the current logged in user. This means that Homebrew is not able to access these locations and add the files it needs to install software for you on OS X.
To fix this you will need to use chown
in the Terminal application to reclaim permissions for these directories. Open up a terminal and run:
sudo chown -R `whoami`:admin /usr/local/bin
You will be prompted for your password when using the sudo
command.
Then run:
sudo chown -R `whoami`:admin /usr/local/share
Now you can rerun your brew install command.
Note: If you have already installed the homebrew package, you will simply need to link it instead. If you see an error similar to:
brew install the_silver_searcher
Warning: the_silver_searcher-0.31.0 already installed, it's just not linked
Then simply run:
brew link the_silver_searcher
Do you run Sophos Antivirus?
I have heard that currently there is a problem with Sophos on El Capitan. When its scans your computer it will touch the /usr/local/
directory and affect the permissions. Sophos are working on a fix for this but currently you may discover permission problems even without a log out or system reboot.
If your Mac audio controls are locked (greyed out in the control menu and the keyboard buttons don't work), you can open up the Terminal
application and run the following command:
sudo killall coreaudiod
You will be prompted for your password and then your audio controls will restart, brining them back to life.
When building an iOS project with Cordova the setting that allows web views to bounce will be turned on by default, resulting in an effect while scrolling that can look ugly, like so:

Since Cordova versions 1.5+ the cordova.plist
file has been replaced with the config.xml
that you find in the root directory of your Cordova project. This is where you set the configuration property to inform iOS to disable web view bouncing.
Instead of editing a plist file in XCode, instead we will add the configuration to the /your-project/config.xml
file. Open this file with a text editor and add the line to the end of the <widget>
block, before the final </widget>
.
<preference name="DisallowOverscroll" value="true" />
Now rebuild the iOS application from the terminal.
cordova build ios
When you next run the application in XCode on a simulator or device, the bouncing problem will be solved.