I have built a web app and I am trying to build out an automated unit testing suite so that I can more easily refactor the code.
I have javascript code that functions correctly on my website. Simple hypothetical code:
function timesTwo(x){x*=2;return x}
If I were writing my code for NodeJS I would add the tests to confirm that the code is working correctly. Below is an example using Wish and Mocha:
describe('timesTwo()', function() {
it('multiplies by two', function() {
var result = timesTwo(5);
wish(result === 10);
});
});
This code (including tests) works fine if I run it in node.JS to test it, but now it throws errors in my browser:
require is not defined
describe is not defined
wish is not defined
How can I create an automated test suite for my code in a way that doesn't throw errors in the browser?
question from:
https://stackoverflow.com/questions/65871322/how-can-i-unit-test-my-javascript-code-in-a-way-that-allows-it-to-be-used-in-a-w 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…