We will now create our first integration tests. Each of our tests will run separately, meaning they should not depend on any other test and should follow a predictable workflow. First, we need to change our run.js file to run all test files. For that, we'll use mocha and add all files found in the integration folder:
const fs = require("fs");
const path = require("path");
const mocha = require("mocha");
const suite = new mocha();
fs.readdir(path.join(__dirname, "integration"), (err, files) => {
if (err) throw err;
files.filter((filename) =>
(filename.match(/.js$/))).map((filename) => {
suite.addFile(path.join(__dirname, "integration", filename));
});
suite.run((failures) => {
process.exit(failures);
});
});
Then, let's create the integration folder inside our test folder, and let's create our first test file, called image-upload.js. Add this content to the file:
describe("Uploading image", () => {
it("should accept only images");
});
If we now run the tests again, we should see the default mocha response with no tests passing and no tests failing:
npm test
> [email protected] test /Users/dresende/imagini
> node test/run
0 passing (2ms)
To avoid repeating code, let's create a tools.js file inside the test folder, so we can export common tasks that every test file can use. Out of the box, I'm thinking about our microservice location and a sample image:
const fs = require("fs");
const path = require("path");
exports.service = require("../imagini.js");
exports.sample = fs.readFileSync(path.join(__dirname, "sample.png"));
Create a sample.png image in the test folder. When a test needs to upload an image, it will use that sample. In the future, we could have different kinds of samples, such as huge images, to test performance and limitations.