Skip to content

Commit 6e9f7c4

Browse files
chore(tests): wrap fetchMock.restore()/reset() in arrow functions for Jest hooks
1 parent 2843eec commit 6e9f7c4

5 files changed

Lines changed: 115 additions & 122 deletions

File tree

package-lock.json

Lines changed: 40 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
"eslint-plugin-prettier": "^5.0.1",
102102
"expo": "^52.0.40",
103103
"faker": "^4.1.0",
104-
"fetch-mock": "^7.3.9",
104+
"fetch-mock": "^9.1.0",
105105
"husky": "^4.2.5",
106106
"jest": "^29.7.0",
107107
"jest-environment-jsdom": "^29.5.0",

src/auth/__tests__/index.spec.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('auth', () => {
4242
jest.useFakeTimers().setSystemTime(new Date('2023-01-01'));
4343
});
4444

45-
beforeEach(fetchMock.restore);
45+
beforeEach(() => fetchMock.restore());
4646

4747
describe('constructor', () => {
4848
it('should build with domain', () => {
@@ -57,7 +57,7 @@ describe('auth', () => {
5757
it('should fail without domain', () => {
5858
expect(() => new Auth({ clientId })).toThrowErrorMatchingSnapshot();
5959
});
60-
60+
6161
it('should accept custom headers', () => {
6262
const headers = { 'X-Custom-Header': 'custom-value' };
6363
const auth = new Auth({ baseUrl, clientId, headers });
@@ -1061,48 +1061,48 @@ describe('auth', () => {
10611061
});
10621062

10631063
describe('method-specific custom headers', () => {
1064-
it('should accept and use custom headers in passwordRealm that don\'t conflict with defaults', async () => {
1064+
it("should accept and use custom headers in passwordRealm that don't conflict with defaults", async () => {
10651065
fetchMock.postOnce('https://samples.auth0.com/oauth/token', tokens);
10661066
const customHeaders = { 'X-Custom-Header': 'custom-value' };
1067-
1067+
10681068
await auth.passwordRealm({
10691069
username: 'info@auth0.com',
10701070
password: 'secret pass',
10711071
realm: 'Username-Password-Authentication',
1072-
headers: customHeaders
1072+
headers: customHeaders,
10731073
});
1074-
1074+
10751075
const [_, fetchOptions] = fetchMock.lastCall();
10761076
expect(fetchOptions.headers.get('X-Custom-Header')).toBe('custom-value');
10771077
});
1078-
1079-
it('should accept and use custom headers in userInfo that don\'t conflict with defaults', async () => {
1078+
1079+
it("should accept and use custom headers in userInfo that don't conflict with defaults", async () => {
10801080
const success = {
10811081
status: 200,
10821082
body: { sub: 'auth0|1029837475' },
10831083
headers: { 'Content-Type': 'application/json' },
10841084
};
10851085
fetchMock.getOnce('https://samples.auth0.com/userinfo', success);
10861086
const customHeaders = { 'X-Custom-Header': 'custom-value' };
1087-
1088-
await auth.userInfo({
1087+
1088+
await auth.userInfo({
10891089
token: 'an access token of a user',
1090-
headers: customHeaders
1090+
headers: customHeaders,
10911091
});
1092-
1092+
10931093
const [_, fetchOptions] = fetchMock.lastCall();
10941094
expect(fetchOptions.headers.get('X-Custom-Header')).toBe('custom-value');
10951095
});
1096-
1097-
it('should accept and use custom headers in refreshToken that don\'t conflict with defaults', async () => {
1096+
1097+
it("should accept and use custom headers in refreshToken that don't conflict with defaults", async () => {
10981098
fetchMock.postOnce('https://samples.auth0.com/oauth/token', tokens);
10991099
const customHeaders = { 'X-Custom-Header': 'custom-value' };
1100-
1100+
11011101
await auth.refreshToken({
11021102
refreshToken: 'a refresh token of a user',
1103-
headers: customHeaders
1103+
headers: customHeaders,
11041104
});
1105-
1105+
11061106
const [_, fetchOptions] = fetchMock.lastCall();
11071107
expect(fetchOptions.headers.get('X-Custom-Header')).toBe('custom-value');
11081108
});

src/management/__tests__/users.spec.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import fetchMock from 'fetch-mock';
33

44
describe('users', () => {
55
const baseUrl = 'samples.auth0.com';
6-
const telemetry = {name: 'react-native-auth0', version: '1.0.0'};
6+
const telemetry = { name: 'react-native-auth0', version: '1.0.0' };
77
const token = 'a.token.from.the.user';
88
const unexpectedError = {
99
status: 500,
1010
body: 'Internal Server Error....',
11-
headers: {'Content-Type': 'text/plain'},
11+
headers: { 'Content-Type': 'text/plain' },
1212
};
1313
const auth0Error = {
1414
status: 403,
@@ -18,25 +18,25 @@ describe('users', () => {
1818
message: 'User to be acted on does not match subject in bearer token.',
1919
statusCode: 403,
2020
},
21-
headers: {'Content-Type': 'application/json'},
21+
headers: { 'Content-Type': 'application/json' },
2222
};
2323

24-
const users = new Users({baseUrl, telemetry, token});
24+
const users = new Users({ baseUrl, telemetry, token });
2525

26-
beforeEach(fetchMock.restore);
26+
beforeEach(() => fetchMock.restore());
2727

2828
describe('constructor', () => {
2929
it('should build with domain', () => {
30-
const users = new Users({baseUrl, token});
30+
const users = new Users({ baseUrl, token });
3131
expect(users.client.bearer).toContain(token);
3232
});
3333

3434
it('should fail without token', () => {
35-
expect(() => new Users({baseUrl})).toThrowErrorMatchingSnapshot();
35+
expect(() => new Users({ baseUrl })).toThrowErrorMatchingSnapshot();
3636
});
3737

3838
it('should fail without domain', () => {
39-
expect(() => new Users({token})).toThrowErrorMatchingSnapshot();
39+
expect(() => new Users({ token })).toThrowErrorMatchingSnapshot();
4040
});
4141
});
4242

@@ -46,83 +46,83 @@ describe('users', () => {
4646
it('should send correct payload', async () => {
4747
fetchMock.getOnce(
4848
`https://samples.auth0.com/api/v2/users/${encodeURIComponent(userId)}`,
49-
user,
49+
user
5050
);
5151
expect.assertions(1);
52-
await users.getUser({id: userId});
52+
await users.getUser({ id: userId });
5353
expect(fetchMock.lastCall()).toMatchSnapshot();
5454
});
5555

5656
it('should return successful response', async () => {
5757
fetchMock.getOnce(
5858
`https://samples.auth0.com/api/v2/users/${encodeURIComponent(userId)}`,
59-
user,
59+
user
6060
);
6161
expect.assertions(1);
62-
await expect(users.getUser({id: userId})).resolves.toMatchSnapshot();
62+
await expect(users.getUser({ id: userId })).resolves.toMatchSnapshot();
6363
});
6464

6565
it('should handle oauth error', async () => {
6666
fetchMock.getOnce(
6767
`https://samples.auth0.com/api/v2/users/${encodeURIComponent(userId)}`,
68-
auth0Error,
68+
auth0Error
6969
);
7070
expect.assertions(1);
71-
await expect(users.getUser({id: userId})).rejects.toMatchSnapshot();
71+
await expect(users.getUser({ id: userId })).rejects.toMatchSnapshot();
7272
});
7373

7474
it('should handle unexpected error', async () => {
7575
fetchMock.getOnce(
7676
`https://samples.auth0.com/api/v2/users/${encodeURIComponent(userId)}`,
77-
unexpectedError,
77+
unexpectedError
7878
);
7979
expect.assertions(1);
80-
await expect(users.getUser({id: userId})).rejects.toMatchSnapshot();
80+
await expect(users.getUser({ id: userId })).rejects.toMatchSnapshot();
8181
});
8282
});
8383

8484
describe('PATCH user', () => {
85-
const metadata = {first_name: 'Mike', lastName: 'Doe'};
85+
const metadata = { first_name: 'Mike', lastName: 'Doe' };
8686
it('should send correct payload', async () => {
8787
fetchMock.patchOnce(
8888
`https://samples.auth0.com/api/v2/users/${encodeURIComponent(userId)}`,
89-
user,
89+
user
9090
);
9191
expect.assertions(1);
92-
await users.patchUser({id: userId, metadata});
92+
await users.patchUser({ id: userId, metadata });
9393
expect(fetchMock.lastCall()).toMatchSnapshot();
9494
});
9595

9696
it('should return successful response', async () => {
9797
fetchMock.patchOnce(
9898
`https://samples.auth0.com/api/v2/users/${encodeURIComponent(userId)}`,
99-
user,
99+
user
100100
);
101101
expect.assertions(1);
102102
await expect(
103-
users.patchUser({id: userId, metadata}),
103+
users.patchUser({ id: userId, metadata })
104104
).resolves.toMatchSnapshot();
105105
});
106106

107107
it('should handle oauth error', async () => {
108108
fetchMock.patchOnce(
109109
`https://samples.auth0.com/api/v2/users/${encodeURIComponent(userId)}`,
110-
auth0Error,
110+
auth0Error
111111
);
112112
expect.assertions(1);
113113
await expect(
114-
users.patchUser({id: userId, metadata}),
114+
users.patchUser({ id: userId, metadata })
115115
).rejects.toMatchSnapshot();
116116
});
117117

118118
it('should handle unexpected error', async () => {
119119
fetchMock.patchOnce(
120120
`https://samples.auth0.com/api/v2/users/${encodeURIComponent(userId)}`,
121-
unexpectedError,
121+
unexpectedError
122122
);
123123
expect.assertions(1);
124124
await expect(
125-
users.patchUser({id: userId, metadata}),
125+
users.patchUser({ id: userId, metadata })
126126
).rejects.toMatchSnapshot();
127127
});
128128
});

0 commit comments

Comments
 (0)