Is it possible to use ES6 modules in Mocha tests?

Mocha has support for ESM from version 7.1.0 onward (release: Feb. 26, 2020). This requires Node 12.11.0 or higher, and is subject to the current restrictions/limitations of using modules in Node: Either you must use .mjs file extensions for source files that use ES modules, or you must have “type”: “module” in your package.json You … Read more

Nested components testing with Enzyme inside of React & Redux

Enzyme’s mount takes optional parameters. The two that are necessary for what you need are options.context: (Object [optional]): Context to be passed into the component options.childContextTypes: (Object [optional]): Merged contextTypes for all children of the wrapper You would mount SampleComponent with an options object like so: const store = { subscribe: () => {}, dispatch: … Read more

What’s the best way to unit test an event being emitted in Nodejs?

If you can guarantee that the event should fire within a certain amount of time, then simply set a timeout. it(‘should emit an some_event’, function(done){ this.timeout(1000); //timeout with an error if done() isn’t called within one second myObj.on(‘some_event’,function(){ // perform any other assertions you want here done(); }); // execute some code which should trigger … Read more

Mocha, Chai: Assert that Object is included in an Array of Objects

Here is an alternative and non order dependent approach for collections: array expect([1, 2, 3]).to.include.members([3, 2, 1]) You can also use this with a deep flag for comparison of objects: array of objects expect([{ id: 1 }]).to.deep.include.members([{ id: 1 }]); object expect({foo: ‘bar’, width: 190, height: 90}).to.include({ height: 90, width: 190 })

Mocha tests don’t run with Webpack and mocha-loader

Mocha loader won’t run tests while building, it’s used to create a bundle specifically containing your tests which you can then run from your browser. I would recommend creating a separate webpack config file for your tests, which you can then host on a webpack-dev-server that uses a different port from your application. Here’s an … Read more

How can mocha recursively search my `src` folder for a specific filename pattern?

The –recursive flag is meant to operate on directories. If you were to pass a glob that matches directories, then these directories would be examined recursively but if you pass a glob that matches files, like you are doing, then –recursive is ineffective. I would suggest not using –recursive with a glob because globs already … Read more

tech