Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
mocha async/await テスト
const fs = require('fs');
const assert = require("assert");
const pReadFile = (path) => new Promise((resolve, reject) => fs.readFile(path, (err, files) => {
if (err) {
reject(err);
}
else {
resolve(files);
}
}));
describe("test", function () {
it('case1', async function () {
let files = await pReadFile('./test/await-test.js');
assert.ok(files.length > 0);
});
it('case2', async function () {
let files = await pReadFile('./test/nofile');
assert.ok(files.length > 0);
});
it('case3', async function () {
assert.throws(async function () {
let files = await pReadFile('./test/nofile');
assert.ok(files.length > 0);
},
Error);
});
it('case4', async function () {
let err = null;
try {
let files = await pReadFile('./test/nofile');
assert.ok(files.length > 0);
} catch (e) {
err = e;
}
assert.equal(err && err.code, 'ENOENT');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment