Skip to content

Commit 9a83cbc

Browse files
committed
Remove mock in unit tests
1 parent c2e1584 commit 9a83cbc

4 files changed

Lines changed: 6 additions & 70 deletions

File tree

.github/workflows/unittest.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
node-version: [10.x, 12.x, 14.x, 15.x]
1212

1313
env:
14-
EMAIL_VALIDATION_API_KEY: ${{ secrets.API_KEY }}
14+
IP_GEOLOCATION_API_KEY: ${{ secrets.API_KEY }}
1515

1616
steps:
1717
- uses: actions/checkout@v2

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,5 +156,7 @@ To build the lib, you need to run `npm run build:lib`.
156156

157157
### Test
158158

159-
To run the test suite, you need to run: `npm run test`.
159+
To run the test suite, you need the API key from the abstract website and you can run:
160+
161+
IP_GEOLOCATION_API_KEY=(your key here) npm run test
160162

test/index.test.js

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,7 @@
11
import 'regenerator-runtime/runtime';
22
import {AbstractIpGeolocation} from '../src'
3-
const mocks = require('./mocks.js')
43

5-
function mockFetch(status, data) {
6-
const xhrMockObj = {
7-
open: jest.fn(),
8-
send: jest.fn(),
9-
setRequestHeader: jest.fn(),
10-
onreadystatechange: jest.fn(),
11-
readyState: 4,
12-
status,
13-
responseText: JSON.stringify(data),
14-
};
15-
16-
const xhrMockClass = () => xhrMockObj;
17-
18-
global.XMLHttpRequest = jest.fn().mockImplementation(xhrMockClass);
19-
20-
setTimeout(() => {
21-
xhrMockObj.onreadystatechange();
22-
}, 0);
23-
}
4+
const API_KEY = process.env.IP_GEOLOCATION_API_KEY;
245

256
test('Should throw error when no key is configured', () => {
267
let thrownError;
@@ -36,9 +17,7 @@ test('Should throw error when no key is configured', () => {
3617
});
3718

3819
test('Request OK when everything is set up correctly', async () => {
39-
mockFetch(200, mocks);
40-
41-
AbstractIpGeolocation.configure('API_KEY');
20+
AbstractIpGeolocation.configure(API_KEY);
4221
let response = await AbstractIpGeolocation.look_up('166.171.248.255')
4322
expect(response.ip_address).toBe('166.171.248.255')
4423
});

test/mocks.js

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)