我尝试了解如何测试js文件。 看,我有一个带有功能的emotify.js文件:
function emotify(string) {
return string + '' + ' :)';
}
然后我创建了另一个文件-index.js,内容如下:
var emotify = require('./emotify.js');
console.log(emotify('just testing'));
但是控制台向我推送了一个错误
TypeError: emotify is not a function
怎么了?
答案 0 :(得分:3)
当您需要一个模块时,结果就是该模块已导出的内容。在这种情况下,您将需要导出函数:
emotify.js代码:
#include <algorithm>
#include <initializer_list>
#include <iostream>
#include <cassert>
#include <ostream>
#include "Vector.h"
int main() {
///////////////////////////////////////////////////////////////////////
///////////////////////////// VECTOR //////////////////////////////////
///////////////////////////////////////////////////////////////////////
Vector<int> nullVector; // Declare an empty Vector
assert(nullVector.getSize() == 0); // Make sure its size is 0
assert(nullVector.empty()); // Make sure the vector is empty
assert(nullVector.getCapacity() == 100); // Make sure its capacity is greater than 0
Vector<int> source(20, 0); // Declare a 20-element zero Vector
assert(source.getSize() == 20); // Make sure its size is 20
for (int i = 0; i < source.getSize(); i++) {
source.set(i, i);
assert(source.get(i) == i); // Make sure the i-th element has value i
}
source.remove(15); // Remove the 15th element
assert(source[15] == 16); // Make sure the 15th element has value 16
source.insert(15, 15); // Insert value 15 at the index 15
assert(source[15] == 15); // Make sure the 15th element has value 15
source.pop_back(); // Remove the last element
assert(source.getSize() == 19); // Make sure its size is 19
source.push_back(19); // Insert value 20 at the bottom
assert(source.getSize() == 20); // Make sure its size is 20
assert(source.get(19) == 19); // Make sure the 19th element has value 19
Vector<int> copyVector(source); // Declare a Vector equal to source
for (int i = 0; i < source.getSize(); i++) {
assert(copyVector[i] == source[i]); // Make sure copyVector equal to source
}
// Vector<int> newSource = copyVector + source;
std::cout << "source: \n" << source; // Print out source
std::cout << "copyVector: \n" << copyVector; // Print out copyVector
Vector<int> newSource = source + copyVector; // Concatenate source and copyVector
std::cout << "newSource: \n" << newSource; // Print out source + copyVector
source.clear(); // Clear source
assert(source.getSize() == 0); // Make sure its size is 0
std::cout << "Vector unit test succeeded." << std::endl;
return 0;
}
答案 1 :(得分:1)
emotify.js:
module.exports = function emotify(string) { // Named function, good for call stack at debugging. You are pro, right ?
return string + '' + ' :)';
}
test.js:
const emotify = require('./emotify.js'); // const instead of var, cause you are pro :)
console.log(emotify('just testing'));
mylib.js:
function emotify(string) {
return string + '' + ' :)';
}
function anotherFunc(string) {
return string + '' + ' :)';
}
module.exports = {
emotify,
anotherFunc,
};
test.js:
const mylib = require('./mylib.js');
console.log(mylib.emotify('just testing'));
console.log(mylib.anotherFunc('just testing'));
================
有用的链接: