Skip to content

Commit 0de230c

Browse files
committed
Support exclude
1 parent 301bdd7 commit 0de230c

File tree

5 files changed

+486
-21
lines changed

5 files changed

+486
-21
lines changed

.babelrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"presets": [ "react" ]
2+
"presets": [ "react", "env" ]
33
}

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@
3535
"babel-core": "^6.26.0",
3636
"babel-eslint": "^8.0.1",
3737
"babel-polyfill": "^6.26.0",
38+
"babel-preset-env": "^1.6.1",
3839
"babel-preset-react": "^6.24.1",
3940
"eslint": "^4.10.0",
4041
"expect.js": "^0.3.1",
42+
"isomorphic-fetch": "^2.2.1",
4143
"mocha": "^4.0.1"
4244
},
4345
"dependencies": {

src/index.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@ import { parseRequest, matchUrl, isNull } from './util';
22
import Response from './response';
33

44
class FetchMock {
5-
constructor(required) {
5+
constructor(required, options = {
6+
fetch: () => {},
7+
exclude: [],
8+
}) {
69
if ('object' !== typeof required) {
710
throw new Error('There is no required defined.');
811
}
912

1013
this.urls = [];
14+
this.raw = options.fetch;
15+
this.exclude = options.exclude;
16+
1117
this.loadMocks = this.loadMocks.bind(this);
1218
this.loadMock = this.loadMock.bind(this);
1319
this.matchReqUrl = this.matchReqUrl.bind(this);
@@ -62,7 +68,21 @@ class FetchMock {
6268
};
6369
}
6470

71+
isExclude(url) {
72+
for (let i = 0; i < this.exclude.length; i++) {
73+
const excludeUrl = this.exclude[i];
74+
return excludeUrl === url || (excludeUrl instanceof RegExp && excludeUrl.exec(url) !== null);
75+
}
76+
return false;
77+
}
78+
6579
fetch(url, options) {
80+
// using raw fetch while match exclude
81+
if (this.isExclude(url)) {
82+
// using raw fetch
83+
return this.raw(url, options);
84+
}
85+
6686
const { request, mock } = this.matchReqUrl(parseRequest(url, options));
6787
if ('function' !== typeof mock.func) {
6888
throw new Error('There is no url defined in __mocks__');

test/index.test.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import 'babel-polyfill';
22
import expect from 'expect.js';
33
import FetchMock, { Mock } from '../';
44

5-
const fetch = new FetchMock(require('../__mocks__')).fetch;
5+
const fetch = new FetchMock(require('../__mocks__'), {
6+
fetch: require('isomorphic-fetch'),
7+
exclude: [
8+
'http://www.baidu.com',
9+
/^foo(bar)?$/i,
10+
],
11+
}).fetch;
612
describe('test fetch mock', () => {
713
it('fetch /api/users data', async () => {
814
const response = await fetch('/api/users');
@@ -99,6 +105,16 @@ describe('test fetch mock', () => {
99105
expect(data).to.be.property('userId', '121');
100106
});
101107

108+
it('fetch exclude path', async () => {
109+
const response = await fetch('http://www.baidu.com');
110+
const { status } = response;
111+
expect(status).to.be.eql(200);
112+
const html = await response.text();
113+
expect(html).not.to.be(undefined);
114+
expect(html).not.to.be.empty();
115+
expect(html).to.be.an('string');
116+
});
117+
102118
it('post /api/users', async () => {
103119
const { status } = await fetch('/api/users', {
104120
method: 'POST',

0 commit comments

Comments
 (0)