theFinalArbiter's avatar

How can I test JS code when using Laravel Mix?

Hi guys, I have a JS file I would like to write tests for but I am not sure where to start. The files full path is resources/assets/js/ModelTransformer.js

I tried to setup a test class myself and run with node but then I am bypassing all the babel stuff so it wont work. I also thought about making a test page in addition to my SPA (react) but I rather not clutter the actual code. How would you do this?

0 likes
2 replies
martinbean's avatar

@theFinalArbiter JavaScript code is just like any other code: it has functions, so passing the same parameters should yield the same results. That’s what you test.

There are various JavaScript testing frameworks, such as Mocha and Jest.

theFinalArbiter's avatar

Thanks @martinbean! So I got mocha installed. Created test.js in root.

var assert = require('assert');

describe('Arithmetic', function() {
    describe('Plus', function() {
        it('1 + 1 equals 2', function() {
        assert.equal(1+1, 2);
        });
    });
});

All good! The thing I am unsure of is how to test my file as it includes alot of ES6 things. For example

import someDependency from 'resources/assets/js/ModelTransformerDependency';

How do I make it so mocha knows of my package.json/webpack process? Or should I just test the transpiled versions of my code?

Please or to participate in this conversation.