exportdefaultfunctionrequest(url) { returnnewPromise((resolve, reject) => { const userID = parseInt(url.substr('/users/'.length), 10); process.nextTick(() => { if (users[userID]) { returnresolve(users[userID]); } returnreject({ error: `User with ${userID} not found.`, }); }); }); }
现在为这个异步的功能编写一个测试。src/__tests__/user_async.test.js
1 2 3 4 5 6 7 8 9 10
importUsersfrom'../lib/user';
jest.mock('../lib/request');
// The assertion for a promise must be returned. it('works with promises', () => // expect.assertions(1); // 当前版本加了这行总是报错,暂时未注释 Users .getUserName(4) .then(data =>expect(data).toEqual('Mark')));
jest.mock('../lib/request'); it('tests error with rejects', () => { expect.assertions(1); returnexpect(Users.getUserName(3)).rejects.toEqual({ error: 'User with 3 not found.', }); });
it('tests error with async/await and rejects', async () => { expect.assertions(1); awaitexpect(Users.getUserName(3)).rejects.toEqual({ error: 'User with 3 not found.', }); });
Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 1 passed, 1 total Time: 1.713s Ran all test suites matching /src\/__tests__\/react_link.test.jsx/i.
Active Filters: filename /src/__tests__/react_link.test.jsx/ › Press c to clear filters.
Watch Usage › Press a to run all tests. › Press f to run only failed tests. › Press o to only run tests related to changed files. › Press p to filter by a filename regex pattern. › Press t to filter by a test name regex pattern. › Press q to quit watch mode. › Press Enter to trigger a test run.
Test Suites: 1 passed, 1 total Tests: 3 passed, 3 total Snapshots: 0 total Time: 2.341s Ran all test suites matching /src\/__tests__\/jest_use_mock_function.test.js/i.
1 | test.only('this will be the only test that runs', () => { > 2 | expect(true).toBe(false); | ^ 3 | }); 4 | 5 | test('this test will not run', () => {
at Object.<anonymous> (src/__tests__/jest_setup_test_only.js:2:16)
Test Suites: 1 failed, 1 total Tests: 1 failed, 1 skipped, 2 total Snapshots: 0 total Time: 2.344s Ran all test suites matching /src\/__tests__\/jest_setup_test_only.js/i.
从这里可以看出
1 2
✕ this will be the only test that runs (10ms) ○ skipped 1 test
--2018-06-19 11:27:21-- https://centos7.iuscommunity.org/ius-release.rpm Resolving centos7.iuscommunity.org (centos7.iuscommunity.org)... 162.242.221.48, 2001:4802:7801:102:be76:4eff:fe21:14aa Connecting to centos7.iuscommunity.org (centos7.iuscommunity.org)|162.242.221.48|:443... connected. ERROR: cannot verify centos7.iuscommunity.org's certificate, issued by ‘/C=US/O=DigiCert Inc/OU=www.digicert.com/CN=Thawte TLS RSA CA G1’: Unable to locally verify the issuer's authority. To connect to centos7.iuscommunity.org insecurely, use `--no-check-certificate'.
Could not retrieve mirrorlist https://mirrors.iuscommunity.org/mirrorlist?repo=ius-centos7&arch=x86_64&protocol=http error was 14: curl#60 - "Peer's Certificate issuer is not recognized."
One of the configured repositories failed (Unknown), and yum doesn't have enough cached data to continue. At this point the only safe thing yum can do is fail. There are a few ways to work "fix" this: 1. Contact the upstream for the repository and get them to fix the problem. 2. Reconfigure the baseurl/etc. for the repository, to point to a working upstream. This is most often useful if you are using a newer distribution release than is supported by the repository (and the packages for the previous distribution release still work). 3. Disable the repository, so yum won't use it by default. Yum will then just ignore the repository until you permanently enable it again or use --enablerepo for temporary usage:
yum-config-manager --disable <repoid>
4. Configure the failing repository to be skipped, if it is unavailable. Note that yum will try to contact the repo. when it runs most commands, so will have to try and fail each time (and thus. yum will be be much slower). If it is a very temporary problem though, this is often a nice compromise: