如何在没有框架的情况下测试JavaScript

6
如何在不使用额外框架(如Mocha)的情况下测试JavaScript代码?是否可以创建单元测试用例,手动编写测试函数,以此测试代码等?
我尝试编写测试用例,但即使它们在同一个文件夹中,我也无法链接它们。
假设这是位于main.js文件中的一个函数。
function calculate(a, b) {
    return a + b;
}

这是在testMain.js文件中的测试用例

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

testCalculate();

当我尝试在IntelliJ IDEA IDE中运行testMain.js时,我遇到了类似于以下错误的问题:

"ReferenceError: calculate is not defined"


2
就像在Java中一样:您仍然需要具有包括测试功能的执行上下文。并且就像在Java中一样,我基本上看不到不使用测试框架的任何好处。 - Dave Newton
4个回答

8

这取决于您是要测试Node.js代码还是前端代码。在两种情况下,您都必须将待测试的函数“公开”到测试框架中。

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 the sum of two numbers', () => {
  assert.strictEqual(main.sum(5, 10), 15);
});


当您运行node test.js时,应该能够看到测试结果。
前端
// app.js
self.myapp = myapp; // All the methods in 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>

在浏览器中打开test.html并打开浏览器控制台。您应该能够看到成功消息。

这样,您就可以为Node.js和前端JavaScript代码编写测试用例,而无需使用Mocha或任何其他框架。


小心使用'require'!如果你需要从模块中导入类,请使用'import'代替! - Andre Carneiro

1
为使你的代码正常运行,testMain.js文件需要以某种方式导入main.js代码。
在main.js文件中:
function calculate(a, b) {
    return a+b;
}

module.exports.calculate = calculate


在 testMain.js 文件中,导入 main.js:
var main = require('main.js')

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

注意:我知道这不一定展示良好的编码风格,只是为了演示最初的问题,并作出最小的更改。

话虽如此,重新发明轮子并构建自己的测试框架通常是不值得的。您能否澄清您想避免现有框架的原因?如果您正在寻找简单性,也许像jstinytest这样的东西就可以解决问题。


-1

如果这是一个Node.js应用程序,您可以简单地使用require关键字来引入其他文件和其中的函数。如果项目使用Babel,您可以使用ES6 import语法来从其他文件中导入函数。


-2
我也曾经为同样的问题困扰了一段时间。问题是如何在没有测试框架的情况下测试您的JavaScript代码,因为测试框架提供了很多功能,但大多数时候它们会妨碍您的工作。
  • 答案是使用断言库而不需要测试框架。例如,您可以只使用chai断言库而不需要mocha框架。

你可以简单地通过npm install chai来安装chai。
之后你就可以直接使用它了。
var should = require('chai').should() 

const log = console.log;

//log(should);

//const letters = "abcdef";

const letters = 555;


letters.should.be.a('string');

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接