如何使用 ava 的 t.throws 来测试 returns 一个 Promise 的函数的错误消息?
How can I use ava's t.throws to test the error message of a function that returns a Promise?
我有一个 returns Promise 函数。我想为它创建一个测试,要求它用特定的错误消息拒绝它才能通过。
我创建了一些示例测试来证明我的意思:
import test from 'ava';
const returnsPromise = () => Promise.reject(new Error('foo'));
const awaitsPromise = async () => {
await Promise.reject(new Error('foo'));
};
test('for comparison, t.throws works with this syntax (this passes)', async t => {
const error = await t.throws(Promise.reject(new Error('foo')));
t.is(error.message, 'foo');
});
test('throws works with functions that return promises (this fails)', async t => {
const error = await t.throws(returnsPromise);
t.is(error.message, 'foo');
});
test('throws works with async functions that await promises (this fails)', async t => {
const error = await t.throws(awaitsPromise);
t.is(error.message, 'foo');
});
作为参考,第一个测试的代码是从这个 GitHub 评论中复制的:https://github.com/avajs/ava/issues/1120#issuecomment-261783315
当 运行 与 ava --verbose
(版本 0.17.0
)时,它给出以下输出:
❯ ava --verbose test/promise.js
√ for comparison, t.throws works with this syntax (this passes)
× throws works with functions that return promises (this fails) Missing expected exception..
× throws works with async functions that await promises (this fails) Missing expected exception..
Unhandled Rejection: test\promise.js
Error: foo
returnsPromise (test/promise.js:3:45)
_tryBlock (node_modules/core-assert/index.js:311:5)
_throws (node_modules/core-assert/index.js:330:12)
Function.assert.throws (node_modules/core-assert/index.js:360:3)
Test.<anonymous> (test/promise.js:15:25)
Test.__dirname [as fn] (test/promise.js:14:1)
Unhandled Rejection: test\promise.js
Error: foo
test/promise.js:6:24
awaitsPromise (test/promise.js:5:7)
_tryBlock (node_modules/core-assert/index.js:311:5)
_throws (node_modules/core-assert/index.js:330:12)
Function.assert.throws (node_modules/core-assert/index.js:360:3)
Test.<anonymous> (test/promise.js:20:25)
Test.__dirname [as fn] (test/promise.js:19:1)
2 tests failed [16:55:55]
2 unhandled rejections
1. throws works with functions that return promises (this fails)
AssertionError: Missing expected exception..
Test.<anonymous> (test/promise.js:15:25)
Test.__dirname [as fn] (test/promise.js:14:1)
2. throws works with async functions that await promises (this fails)
AssertionError: Missing expected exception..
Test.<anonymous> (test/promise.js:20:25)
Test.__dirname [as fn] (test/promise.js:19:1)
如您所见,第一个测试通过,但其他两个测试失败。
如何使用 t.throws 来测试 returns Promise 函数的错误消息?
通过的测试格式为:
test('...', async t => {
const error = await t.throws(<Promise>);
t.is(error.message, '...');
});
失败的两个测试的形式为:
test('...', async t => {
const error = await t.throws(<Function>);
t.is(error.message, '...');
});
尝试调用函数并将返回的任何内容传递给 t.throws()
。
例如。 :
test('throws works with functions that return promises (this fails)', async t => {
const error = await t.throws(returnsPromise());
t.is(error.message, 'foo');
});
你可以向 t.throws
传递一个 promise 并立即检查错误消息。
const foo = Promise.reject(new Error('hello world'));
test(async t => {
await t.throws(foo, 'hello world');
});
您的示例将如下所示。
test('throws works with functions that return promises', async t => {
await t.throws(returnsPromise(), 'foo');
});
我知道这已经很老了,但是如果有人像我一样偶然发现这个,Ava 现在有一个 built in method throwsAsync
来处理这个:
test('throws', async t => {
await t.throwsAsync(async () => {
throw new TypeError('');
}, {instanceOf: TypeError, message: ''});
});
我有一个 returns Promise 函数。我想为它创建一个测试,要求它用特定的错误消息拒绝它才能通过。
我创建了一些示例测试来证明我的意思:
import test from 'ava';
const returnsPromise = () => Promise.reject(new Error('foo'));
const awaitsPromise = async () => {
await Promise.reject(new Error('foo'));
};
test('for comparison, t.throws works with this syntax (this passes)', async t => {
const error = await t.throws(Promise.reject(new Error('foo')));
t.is(error.message, 'foo');
});
test('throws works with functions that return promises (this fails)', async t => {
const error = await t.throws(returnsPromise);
t.is(error.message, 'foo');
});
test('throws works with async functions that await promises (this fails)', async t => {
const error = await t.throws(awaitsPromise);
t.is(error.message, 'foo');
});
作为参考,第一个测试的代码是从这个 GitHub 评论中复制的:https://github.com/avajs/ava/issues/1120#issuecomment-261783315
当 运行 与 ava --verbose
(版本 0.17.0
)时,它给出以下输出:
❯ ava --verbose test/promise.js
√ for comparison, t.throws works with this syntax (this passes)
× throws works with functions that return promises (this fails) Missing expected exception..
× throws works with async functions that await promises (this fails) Missing expected exception..
Unhandled Rejection: test\promise.js
Error: foo
returnsPromise (test/promise.js:3:45)
_tryBlock (node_modules/core-assert/index.js:311:5)
_throws (node_modules/core-assert/index.js:330:12)
Function.assert.throws (node_modules/core-assert/index.js:360:3)
Test.<anonymous> (test/promise.js:15:25)
Test.__dirname [as fn] (test/promise.js:14:1)
Unhandled Rejection: test\promise.js
Error: foo
test/promise.js:6:24
awaitsPromise (test/promise.js:5:7)
_tryBlock (node_modules/core-assert/index.js:311:5)
_throws (node_modules/core-assert/index.js:330:12)
Function.assert.throws (node_modules/core-assert/index.js:360:3)
Test.<anonymous> (test/promise.js:20:25)
Test.__dirname [as fn] (test/promise.js:19:1)
2 tests failed [16:55:55]
2 unhandled rejections
1. throws works with functions that return promises (this fails)
AssertionError: Missing expected exception..
Test.<anonymous> (test/promise.js:15:25)
Test.__dirname [as fn] (test/promise.js:14:1)
2. throws works with async functions that await promises (this fails)
AssertionError: Missing expected exception..
Test.<anonymous> (test/promise.js:20:25)
Test.__dirname [as fn] (test/promise.js:19:1)
如您所见,第一个测试通过,但其他两个测试失败。
如何使用 t.throws 来测试 returns Promise 函数的错误消息?
通过的测试格式为:
test('...', async t => {
const error = await t.throws(<Promise>);
t.is(error.message, '...');
});
失败的两个测试的形式为:
test('...', async t => {
const error = await t.throws(<Function>);
t.is(error.message, '...');
});
尝试调用函数并将返回的任何内容传递给 t.throws()
。
例如。 :
test('throws works with functions that return promises (this fails)', async t => {
const error = await t.throws(returnsPromise());
t.is(error.message, 'foo');
});
你可以向 t.throws
传递一个 promise 并立即检查错误消息。
const foo = Promise.reject(new Error('hello world'));
test(async t => {
await t.throws(foo, 'hello world');
});
您的示例将如下所示。
test('throws works with functions that return promises', async t => {
await t.throws(returnsPromise(), 'foo');
});
我知道这已经很老了,但是如果有人像我一样偶然发现这个,Ava 现在有一个 built in method throwsAsync
来处理这个:
test('throws', async t => {
await t.throwsAsync(async () => {
throw new TypeError('');
}, {instanceOf: TypeError, message: ''});
});