mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-29 23:58:06 +01:00
f47cfbbb3e
The `assert` module has two modes: "Legacy" and "strict". When using strict mode, the "strict" version of each assertion method is implied. Whereas in legacy mode, by default it will use the deprecated, "loose" version of each assertion. We now use strict mode everywhere. A few tests required updates where they were asserting the wrong thing, and it was passing beforehand due to the loose matching.
62 lines
1.8 KiB
JavaScript
62 lines
1.8 KiB
JavaScript
import { strict as assert } from 'assert';
|
|
import nock from 'nock';
|
|
|
|
import getFetchWithTimeout from './fetch-with-timeout';
|
|
|
|
describe('getFetchWithTimeout', function () {
|
|
it('fetches a url', async function () {
|
|
nock('https://api.infura.io').get('/money').reply(200, '{"hodl": false}');
|
|
|
|
const fetchWithTimeout = getFetchWithTimeout(30000);
|
|
const response = await (
|
|
await fetchWithTimeout('https://api.infura.io/money')
|
|
).json();
|
|
assert.deepEqual(response, {
|
|
hodl: false,
|
|
});
|
|
});
|
|
|
|
it('throws when the request hits a custom timeout', async function () {
|
|
nock('https://api.infura.io')
|
|
.get('/moon')
|
|
.delay(2000)
|
|
.reply(200, '{"moon": "2012-12-21T11:11:11Z"}');
|
|
|
|
const fetchWithTimeout = getFetchWithTimeout(123);
|
|
|
|
try {
|
|
await fetchWithTimeout('https://api.infura.io/moon').then((r) =>
|
|
r.json(),
|
|
);
|
|
assert.fail('Request should throw');
|
|
} catch (e) {
|
|
assert.ok(e);
|
|
}
|
|
});
|
|
|
|
it('should abort the request when the custom timeout is hit', async function () {
|
|
nock('https://api.infura.io')
|
|
.get('/moon')
|
|
.delay(2000)
|
|
.reply(200, '{"moon": "2012-12-21T11:11:11Z"}');
|
|
|
|
const fetchWithTimeout = getFetchWithTimeout(123);
|
|
|
|
try {
|
|
await fetchWithTimeout('https://api.infura.io/moon').then((r) =>
|
|
r.json(),
|
|
);
|
|
assert.fail('Request should be aborted');
|
|
} catch (e) {
|
|
assert.deepEqual(e.message, 'Aborted');
|
|
}
|
|
});
|
|
|
|
it('throws on invalid timeout', async function () {
|
|
assert.throws(() => getFetchWithTimeout(), 'should throw');
|
|
assert.throws(() => getFetchWithTimeout(-1), 'should throw');
|
|
assert.throws(() => getFetchWithTimeout({}), 'should throw');
|
|
assert.throws(() => getFetchWithTimeout(true), 'should throw');
|
|
});
|
|
});
|