Trying ES6 style import gives ‘Cannot use import statement outside a module’

The easiest way to run Mocha tests written in ES6 is compiling them on-the-fly using Mocha –require @babel/register option (see https://github.com/mochajs/mocha/wiki/compilers-deprecation#what-should-i-use-instead-then). Of course, you need to make sure to install the corresponding modules and set up the .babelrc accordingly package.json: “dependencies”: { “@babel/cli”: “^7.7.4”, “@babel/core”: “^7.7.4”, “@babel/preset-env”: “^7.7.4”, “@babel/register”: “^7.7.4”, … } .babelrc: { “presets”: … Read more

Testing requests that redirect with mocha/supertest in node

There is built-in assertion for this in supertest: should = require(‘should’) request = require(‘supertest’) app = require(‘../../app’) describe ‘authentication’, -> describe ‘POST /sessions’, -> describe ‘success’, -> it ‘redirects to the right path’, (done) -> request(app) .post(‘/sessions’) .send(user: ‘username’, password: ‘password’) .expect(302) .expect(‘Location’, ‘/home’) .end(done)

Catching gulp-mocha errors

You need to ignore ‘error’ and always emit ‘end’ to make ‘gulp.watch’ work. function handleError(err) { console.log(err.toString()); this.emit(‘end’); } gulp.task(“test”, function() { return gulp.src(paths.tests) .pipe(mocha({ reporter: “spec” }) .on(“error”, handleError)); }); This makes ‘gulp test’ to always return ‘0’ which is problematic for Continuous Integration, but I think we have no choice at this time.

Enzyme simulate an onChange event

You can simply spy to the method directly via the prototype. it(“responds to name change”, done => { const handleChangeSpy = sinon.spy(New.prototype, “handleChange”); const event = {target: {name: “pollName”, value: “spam”}}; const wrap = mount( <New /> ); wrap.ref(‘pollName’).simulate(‘change’, event); expect(handleChangeSpy.calledOnce).to.equal(true); }) Alternatively, you can use spy on the instance’s method, but you have to … Read more

How to await and return the result of a http.request(), so that multiple requests run serially?

async/await work with promises. They will only work if the async function your are awaiting returns a Promise. To solve your problem, you can either use a library like request-promise or return a promise from your doRequest function. Here is a solution using the latter. function doRequest(options) { return new Promise ((resolve, reject) => { … Read more

Testing React Select component

This is a recurrent question. I’m sharing my own code with 100% passing tests which cover 100% of my source code. My component looks like this MySelectComponent({ options, onChange }) { return <div data-testid=”my-select-component”> <Select className=”basic-single” classNamePrefix=”select” name=”myOptions” placeholder=”Select an option” options={options} onChange={e => onChange(e)} /> </div>; } The reason I’m adding a wrapper on … Read more

Mongoose & unique field

Use dropDups to ensure dropping duplicate records in your schemas like; var SimSchema = new Schema({ msisdn : { type : String , unique : true, required : true, dropDups: true }, imsi : { type : String , unique : true, required : true, dropDups: true }, status : { type : Boolean, default: … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)