How to test JavaScript without a framework

javascript unit testing without framework
javascript testing framework
simple javascript test
javascript writing test
javascript testing examples
testing javascript code
javascript test script
create javascript framework from scratch

How to test JavaScript code without using an additional framework such as Mocha. Is it possible to create a unit test case, write test functions manually, test the code..?

I've tried to write a test case but even tho they were in the same folder couldn't link them.

Let's say this is a function in the main.js file

function calculate(a, b) {
    return a+b;
}

And this is a test case in testMain.js file

function testCalculate(){
    if(calculate(1,1)==2)
       console.log('It Works!');
    else
       console.log('Test failed');
}

testCalculate();

When I try to run testMain.js on Intellij Idea IDE I get an error similar to

"ReferenceError: calculate is not defined"

If it is a NodeJS app you can simply require the other file and import the other function. If the project uses Babel you can use ES6 import to import the function from the other file.

Unit Test Your JavaScript Code Without a Framework, Lately, I am learning to write more and more of my javascript code without the help of any library or framework. I like to have a good unit test  Let’s first create a test.js file and write the it function as shown below The it function takes two parameters. First parameter desc is the description of the test case and second parameter fn is

To make your code work, your testMain.js file needs to import your main.js code somehow.

In the main.js file:

function calculate(a, b) {
    return a+b;
}

module.exports.calculate = calculate

in testMain.js file, import the main.js:

var main = require('main.js')

function testCalculate(){
    if(main.calculate(1+1)==2)
       console.log('It Works!');
    else
       console.log('Test failed');
}

Note: I'm aware this isn't necessarily showing good coding style, just aiming to demonstrate what the original issue was with minimal changes to the original snippets

That said, it's not usually worth reinventing the wheel and building your own test framework. Can you clarify the reason why you would like to avoid an existing framework? If you are looking for simplicity, maybe something like jstinytest would do the trick.

How to Unit Test HTML and Vanilla JavaScript Without a UI Framework, Recently I was curious about something: Is it possible to write unit tests for front end code that doesn't use any sort of UI framework or developer  How to Unit Test HTML and Vanilla JavaScript Without a UI Framework Prior Experience. In my professional life, I’ve done a good bit of testing. I’m primarily a front end software engineer, Initial Research. It’s very rare in the world of software engineering that you are the first person to

It depends whether you are trying to test node.js code or front end code. In both cases you have to "expose" the function under test to your test framework.

Node.js

//main.js

const obj = {};

obj.sum = (a, b) => {
  return a+b;
};

module.exports = obj; //Export obj so that it is visible from your test runner

//test.js
const main = require('main.js');
const assert = require('assert');

const it = (desc, fn) => {
  try {
    fn();
    console.log('\x1b[32m%s\x1b[0m', `\u2714 ${desc}`);
  } catch (error) {
    console.log('\n');
    console.log('\x1b[31m%s\x1b[0m', `\u2718 ${desc}`);
    console.error(error);
  }
};

it('should return sum of two numbers', ()=>{
  assert.strictEqual(main.sum(5, 10), 15);
});

When you run node test.js you should be able to see the test result.

Front End

// app.js
self.myapp = myapp; // All the methods on myapp will be exposed globally

myapp.sum = function(a, b) {
  return a + b;
}

// test.js
function it(desc, fn) {
  try {
    fn();
    console.log('\x1b[32m%s\x1b[0m', '\u2714 ' + desc);
  } catch (error) {
    console.log('\n');
    console.log('\x1b[31m%s\x1b[0m', '\u2718 ' + desc);
    console.error(error);
  }
}

function assert(condition) {
  if (!condition) {
    throw new Error();
  }
}

it('should return a sum of two integers', function(){
  assert(myapp.sum(5, 10) === 15);
});


// test.html - This is your test runner for the front end
<html>
...
<body>
...
<script src="app.js"></script>
<script src="test.js"></script>
</body>
</html>

Open test.html in a browser and open the browser console. You should be able to see the success message.

This way you can write test cases for node.js and front end javascript code without using Mocha or any other framework.

Making a Testing Framework in Node.js (Without any External , Recently I was curious about something: Is it possible to write unit tests for front end code that do Tagged with javascript, testing, webdev,  Learn the history of and decompose modern JavaScript frameworks like Angular, React, and Vue by learning about dependency injection, declarative syntax, and data-binding. The one feature that is not fully supported natively by the latest JavaScript versions is databinding .

How to test JavaScript without a framework, Do you want to test JavaScript code that doesn't use any frameworks, or do you want to not use a testing framework? Testing code that does not use a framework​  Michael shows how to build an SPA from scratch, without using a client-side JavaScript framework — helping you evaluate what these frameworks actually do.

A Simple JavaScript test framework from scratch, In this article, I build a simple JavaScript framework from scratch. The framework will then be used to unit test itself. I am exporting the methods at the bottom of the file, so I can require and test them. Testing the testing framework. As promised, we will test the framework using itself. I will write the tests in a file called index.test.js, which can be run using node index.test.js. First, a test on describe. Testing describe. describe… actually doesn’t

JavaScript Unit Testing for Beginners, When first starting just focus on writing a test without worrying so much This JavaScript unit testing framework was created by the jQuery team  Jest is a JavaScript testing framework designed to ensure correctness of any JavaScript codebase. It allows you to write tests with an approachable, familiar and feature-rich API that gives you results quickly.

Comments
  • Just like you would in Java: you still need to have an execution context that includes the functionality under test. And just like in Java I see essentially zero benefit to not using a test framework.