I am using Karma and Mocha for running integration tests on my codebase. For validating the codebase in Node.js, I have a folder of test cases.
repository/
├── test/
│ ├── cases/
│ │ ├── case1.json
│ │ ├── case2.json
│ │ ├── case3.json
│ │ └── case4.json
│ ├── browser/
│ │ └── testFactory.spec.ts
│ └── node/
└── testFactory.spec.ts
For my Node.js tests, I just use fs.readdirSync(testCaseFolder, "utf-8") to get the filenames of all the test cases, and then loop through them.
for (const filename of filenames) {
const data = fs.readFileSync(testCaseFolder + filename, "utf-8");
const testCase: ModelTestCase = JSON.parse(data.toString());
const testName = getTestName(filename);
describe(testName, function() {
it(getTestDescription(filename), async function() {
/* do some testing here */
});
});
}
Now I need to rewrite this testing design to work in the browser for testing via Karma. Karma has a way to specify a path to static files that can be served up during testing in the configuration: Karma - Files. So in this case I added to my Karma Configuration:
files: [
{pattern: 'test/cases/*.json', watched: false, included: false, served: true, nocache: false}
],
and set up a proxy to serve the test cases endpoint at a convenient URL:
proxies: {
"/cases/": "http://localhost:8080/base/test/cases/"
},
Now I could theoretically access test case 1 with the following code:
fetch("/cases/case1.json");
How can I write a loop to fetch every test case located in the /cases/ endpoint that is browser compatible JavaScript?